Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing flag helper methods to Property and PropertyHook #1051

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 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 @@ -30,6 +31,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
Expand Down Expand Up @@ -57,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
14 changes: 14 additions & 0 deletions lib/PhpParser/Node/Stmt/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit complicated to test a single modifier?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just followed the structure of other similar tests. Maybe when in the future more modifiers are allowed, it will be easier to add tests for them.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. If we want to keep this we should drop the separate testIsFinal test though, because the two of them are redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, thanks.

$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'],
];
}
}
10 changes: 10 additions & 0 deletions test/PhpParser/Node/Stmt/PropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Loading