From 21198a4dc45e9f8fa4ca596580946a3c740ce3d9 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 11 Dec 2024 10:55:30 +0100 Subject: [PATCH 1/3] Add flags helper methods `Property::isAbstract()` and `Property::isFinal()` --- lib/PhpParser/Node/Stmt/Property.php | 14 ++++++++++++++ test/PhpParser/Node/Stmt/PropertyTest.php | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/PhpParser/Node/Stmt/Property.php b/lib/PhpParser/Node/Stmt/Property.php index 3b238c76a9..03e45a9526 100644 --- a/lib/PhpParser/Node/Stmt/Property.php +++ b/lib/PhpParser/Node/Stmt/Property.php @@ -80,6 +80,20 @@ public function isReadonly(): bool { return (bool) ($this->flags & Modifiers::READONLY); } + /** + * Whether the property is abstract. + */ + public function isAbstract(): bool { + return (bool) ($this->flags & Modifiers::ABSTRACT); + } + + /** + * Whether the property is final. + */ + public function isFinal(): bool { + return (bool) ($this->flags & Modifiers::FINAL); + } + /** * Whether the property has explicit public(set) visibility. */ diff --git a/test/PhpParser/Node/Stmt/PropertyTest.php b/test/PhpParser/Node/Stmt/PropertyTest.php index 3b4f31c730..4eb252e008 100644 --- a/test/PhpParser/Node/Stmt/PropertyTest.php +++ b/test/PhpParser/Node/Stmt/PropertyTest.php @@ -57,4 +57,14 @@ public function testSetVisibility() { $node = new Property(Modifiers::PUBLIC_SET, []); $this->assertTrue($node->isPublicSet()); } + + public function testIsFinal() { + $node = new Property(Modifiers::FINAL, []); + $this->assertTrue($node->isFinal()); + } + + public function testIsAbstract() { + $node = new Property(Modifiers::ABSTRACT, []); + $this->assertTrue($node->isAbstract()); + } } From 96d40b0a8bafee2e02486f9aec606a57437ff600 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 11 Dec 2024 11:04:32 +0100 Subject: [PATCH 2/3] Missing flags subNode description in PropertyHook constructor PHPDoc --- lib/PhpParser/Node/PropertyHook.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/PhpParser/Node/PropertyHook.php b/lib/PhpParser/Node/PropertyHook.php index a8cde850f7..9ae77a2b6e 100644 --- a/lib/PhpParser/Node/PropertyHook.php +++ b/lib/PhpParser/Node/PropertyHook.php @@ -30,6 +30,7 @@ class PropertyHook extends NodeAbstract implements FunctionLike { * params?: Param[], * attrGroups?: AttributeGroup[], * } $subNodes Array of the following optional subnodes: + * 'flags => 0 : Flags * 'byRef' => false : Whether hook returns by reference * 'params' => array(): Parameters * 'attrGroups' => array(): PHP attribute groups From ee4742959f921492f8030ba76bcc76c56d54b187 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 11 Dec 2024 11:04:44 +0100 Subject: [PATCH 3/3] Add `PropertyHook::isFinal()` helper method with tests --- lib/PhpParser/Node/PropertyHook.php | 8 ++++++ test/PhpParser/Node/PropertyHookTest.php | 34 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 test/PhpParser/Node/PropertyHookTest.php diff --git a/lib/PhpParser/Node/PropertyHook.php b/lib/PhpParser/Node/PropertyHook.php index 9ae77a2b6e..fab0e9d39a 100644 --- a/lib/PhpParser/Node/PropertyHook.php +++ b/lib/PhpParser/Node/PropertyHook.php @@ -2,6 +2,7 @@ namespace PhpParser\Node; +use PhpParser\Modifiers; use PhpParser\Node\Stmt\Return_; use PhpParser\NodeAbstract; @@ -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)]; diff --git a/test/PhpParser/Node/PropertyHookTest.php b/test/PhpParser/Node/PropertyHookTest.php new file mode 100644 index 0000000000..c8bd07cffb --- /dev/null +++ b/test/PhpParser/Node/PropertyHookTest.php @@ -0,0 +1,34 @@ + 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'], + ]; + } +}