Skip to content

Commit

Permalink
Add PropertyHook::isFinal() helper method with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 11, 2024
1 parent 96d40b0 commit aaab8ec
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/PhpParser/Node/PropertyHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpParser\Node;

use PhpParser\Modifiers;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeAbstract;

Expand Down Expand Up @@ -58,6 +59,13 @@ public function getReturnType() {
return null;
}

/**
* Whether the property hook is final.
*/
public function isFinal(): bool {
return (bool) ($this->flags & Modifiers::FINAL);
}

public function getStmts(): ?array {
if ($this->body instanceof Expr) {
return [new Return_($this->body)];
Expand Down
45 changes: 45 additions & 0 deletions test/PhpParser/Node/PropertyHookTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace PhpParser\Node;

use PhpParser\Modifiers;

class PropertyHookTest extends \PHPUnit\Framework\TestCase {
/**
* @dataProvider provideModifiers
*/
public function testModifiers($modifier): void {
$node = new PropertyHook(
'get',
null,
[
'flags' => constant(Modifiers::class . '::' . strtoupper($modifier)),
]
);

$this->assertTrue($node->{'is' . $modifier}());
}

public function testNoModifiers(): void {
$node = new PropertyHook('get', null);

$this->assertFalse($node->isFinal());
}

public static function provideModifiers() {
return [
['final'],
];
}

public function testIsFinal() {
$node = new PropertyHook(
'get',
null,
[
'flags' => Modifiers::FINAL,
]
);
$this->assertTrue($node->isFinal());
}
}

0 comments on commit aaab8ec

Please sign in to comment.