Skip to content

Commit

Permalink
Promoted properties with hooks do not need visibility modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 11, 2024
1 parent 469377f commit afa9577
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/PhpParser/Node/Param.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,20 @@ public function getType(): string {
* Whether this parameter uses constructor property promotion.
*/
public function isPromoted(): bool {
return $this->flags !== 0;
return $this->flags !== 0 || $this->hooks !== [];
}

public function isPublic(): bool {
return (bool) ($this->flags & Modifiers::PUBLIC);
$public = (bool) ($this->flags & Modifiers::PUBLIC);
if ($public) {
return true;
}

if ($this->hooks === []) {
return false;
}

return ($this->flags & Modifiers::VISIBILITY_MASK) === 0;
}

public function isProtected(): bool {
Expand Down
14 changes: 14 additions & 0 deletions test/PhpParser/Node/ParamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,18 @@ public function testSetVisibility() {
$node->flags = Modifiers::PUBLIC_SET;
$this->assertTrue($node->isPublicSet());
}

public function testPromotedPropertyWithoutVisibilityModifier(): void {
$node = new Param(new Variable('foo'));
$get = new PropertyHook('get', null);
$node->hooks[] = $get;

$this->assertTrue($node->isPromoted());
$this->assertTrue($node->isPublic());
}

public function testNonPromotedPropertyIsNotPublic(): void {
$node = new Param(new Variable('foo'));
$this->assertFalse($node->isPublic());
}
}

0 comments on commit afa9577

Please sign in to comment.