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 7b2d5b9 commit 4978997
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 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;
}
}
13 changes: 1 addition & 12 deletions lib/PhpParser/Parser/Php7.php
Expand Up @@ -2557,19 +2557,9 @@ protected function initReduceCallbacks() {
512 => function ($stackPos) {
$this->semValue = new Expr\ClassConstFetch($this->semStack[$stackPos-(3-1)], new Expr\Error($this->startAttributeStack[$stackPos-(3-3)] + $this->endAttributeStack[$stackPos-(3-3)]), $this->startAttributeStack[$stackPos-(3-1)] + $this->endAttributes); $this->errorState = 2;
},
<<<<<<< HEAD
512 => function ($stackPos) {
$this->semValue = Scalar\String_::fromString($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
=======
513 => function ($stackPos) {
<<<<<<< HEAD
$attrs = $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes; $attrs['kind'] = ($this->semStack[$stackPos-(1-1)][0] === "'" || ($this->semStack[$stackPos-(1-1)][1] === "'" && ($this->semStack[$stackPos-(1-1)][0] === 'b' || $this->semStack[$stackPos-(1-1)][0] === 'B')) ? Scalar\String_::KIND_SINGLE_QUOTED : Scalar\String_::KIND_DOUBLE_QUOTED);
$this->semValue = new Scalar\String_(Scalar\String_::parse($this->semStack[$stackPos-(1-1)]), $attrs);
>>>>>>> add readonly class support
=======
$attrs = $this->startAttributeStack[$stackPos-(3-1)] + $this->endAttributes; $attrs['kind'] = Expr\Array_::KIND_SHORT;
$this->semValue = new Expr\Array_($this->semStack[$stackPos-(3-2)], $attrs);
>>>>>>> fix tests
},
514 => function ($stackPos) {
$attrs = $this->startAttributeStack[$stackPos-(4-1)] + $this->endAttributes; $attrs['kind'] = Expr\Array_::KIND_LONG;
Expand All @@ -2579,8 +2569,7 @@ protected function initReduceCallbacks() {
$this->semValue = $this->semStack[$stackPos-(1-1)];
},
516 => function ($stackPos) {
$attrs = $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes; $attrs['kind'] = ($this->semStack[$stackPos-(1-1)][0] === "'" || ($this->semStack[$stackPos-(1-1)][1] === "'" && ($this->semStack[$stackPos-(1-1)][0] === 'b' || $this->semStack[$stackPos-(1-1)][0] === 'B')) ? Scalar\String_::KIND_SINGLE_QUOTED : Scalar\String_::KIND_DOUBLE_QUOTED);
$this->semValue = new Scalar\String_(Scalar\String_::parse($this->semStack[$stackPos-(1-1)]), $attrs);
$this->semValue = Scalar\String_::fromString($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
},
517 => function ($stackPos) {
$attrs = $this->startAttributeStack[$stackPos-(3-1)] + $this->endAttributes; $attrs['kind'] = Scalar\String_::KIND_DOUBLE_QUOTED;
Expand Down
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 4978997

Please sign in to comment.