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 ee47429
Show file tree
Hide file tree
Showing 2 changed files with 42 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
34 changes: 34 additions & 0 deletions test/PhpParser/Node/PropertyHookTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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'],
];
}
}

0 comments on commit ee47429

Please sign in to comment.