Skip to content

Commit

Permalink
add BuilderHelpers::addClassModifier()
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 15, 2022
1 parent 05018b8 commit e1526ce
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/PhpParser/Builder/Class_.php
Expand Up @@ -67,7 +67,7 @@ public function implement(...$interfaces) {
* @return $this The builder instance (for fluid interface)
*/
public function makeAbstract() {
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);
$this->flags = BuilderHelpers::addClassModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);

return $this;
}
Expand All @@ -78,7 +78,13 @@ public function makeAbstract() {
* @return $this The builder instance (for fluid interface)
*/
public function makeFinal() {
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
$this->flags = BuilderHelpers::addClassModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);

return $this;
}

public function makeReadonly() {
$this->flags = BuilderHelpers::addClassModifier($this->flags, Stmt\Class_::MODIFIER_READONLY);

return $this;
}
Expand Down
9 changes: 9 additions & 0 deletions lib/PhpParser/BuilderHelpers.php
Expand Up @@ -310,4 +310,13 @@ public static function addModifier(int $modifiers, int $modifier) : int {
Stmt\Class_::verifyModifier($modifiers, $modifier);
return $modifiers | $modifier;
}

/**
* Adds a modifier and returns new modifier bitmask.
* @return int New modifiers
*/
public static function addClassModifier(int $existingModifiers, int $modifierToSet) : int {
Stmt\Class_::verifyClassModifier($existingModifiers, $modifierToSet);
return $existingModifiers | $modifierToSet;
}
}
14 changes: 14 additions & 0 deletions test/PhpParser/Builder/ClassTest.php
Expand Up @@ -68,6 +68,20 @@ public function testFinal() {
);
}

public function testReadonly() {
$node = $this->createClassBuilder('Test')
->makeReadonly()
->getNode()
;

$this->assertEquals(
new Stmt\Class_('Test', [
'flags' => Stmt\Class_::MODIFIER_READONLY
]),
$node
);
}

public function testStatementOrder() {
$method = new Stmt\ClassMethod('testMethod');
$property = new Stmt\Property(
Expand Down

0 comments on commit e1526ce

Please sign in to comment.