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

Property hooks can be marked final - expose that in reflection API #1474

Merged
merged 2 commits into from
Dec 30, 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"php": "~8.2.0 || ~8.3.2 || ~8.4.1",
"ext-json": "*",
"jetbrains/phpstorm-stubs": "2024.3",
"nikic/php-parser": "^5.3.1"
"nikic/php-parser": "^5.4.0"
},
"authors": [
{
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
<code><![CDATA[classExists]]></code>
<code><![CDATA[isAbstract]]></code>
<code><![CDATA[isFinal]]></code>
<code><![CDATA[isFinal]]></code>
<code><![CDATA[isPrivate]]></code>
<code><![CDATA[isProtected]]></code>
<code><![CDATA[isPublic]]></code>
Expand Down Expand Up @@ -209,7 +210,8 @@
<code><![CDATA[getEndLine]]></code>
<code><![CDATA[getStartLine]]></code>
<code><![CDATA[getStmts]]></code>
<code><![CDATA[getStmts]]></code>
<code><![CDATA[isAbstract]]></code>
<code><![CDATA[isFinal]]></code>
<code><![CDATA[isPrivate]]></code>
<code><![CDATA[isPrivateSet]]></code>
<code><![CDATA[isProtected]]></code>
Expand Down
19 changes: 12 additions & 7 deletions src/Reflection/ReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private function __construct(
assert($node instanceof MethodNode || $node instanceof Node\PropertyHook);

$this->name = $name;
$this->modifiers = $node instanceof MethodNode ? $this->computeModifiers($node) : 0;
$this->modifiers = $this->computeModifiers($node);

$this->fillFromNode($node);
}
Expand Down Expand Up @@ -305,13 +305,18 @@ public function getModifiers(): int
}

/** @return int-mask-of<ReflectionMethodAdapter::IS_*> */
private function computeModifiers(MethodNode $node): int
private function computeModifiers(MethodNode|Node\PropertyHook $node): int
{
$modifiers = $node->isStatic() ? CoreReflectionMethod::IS_STATIC : 0;
$modifiers += $node->isPublic() ? CoreReflectionMethod::IS_PUBLIC : 0;
$modifiers += $node->isProtected() ? CoreReflectionMethod::IS_PROTECTED : 0;
$modifiers += $node->isPrivate() ? CoreReflectionMethod::IS_PRIVATE : 0;
$modifiers += $node->isAbstract() ? CoreReflectionMethod::IS_ABSTRACT : 0;
$modifiers = 0;

if ($node instanceof MethodNode) {
$modifiers += $node->isStatic() ? CoreReflectionMethod::IS_STATIC : 0;
$modifiers += $node->isPublic() ? CoreReflectionMethod::IS_PUBLIC : 0;
$modifiers += $node->isProtected() ? CoreReflectionMethod::IS_PROTECTED : 0;
$modifiers += $node->isPrivate() ? CoreReflectionMethod::IS_PRIVATE : 0;
$modifiers += $node->isAbstract() ? CoreReflectionMethod::IS_ABSTRACT : 0;
}

$modifiers += $node->isFinal() ? CoreReflectionMethod::IS_FINAL : 0;

return $modifiers;
Comment on lines +310 to 322
Copy link
Member

Choose a reason for hiding this comment

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

BTW, no idea how PHP optimizes this, but it's a giant cascade of ternaries D:

I wonder if this could be a single bitwise or operation:

$modifiers = $modifiers |
    ($node->isStatic() & CoreReflectionMethod::IS_STATIC) |
    ($node->isPublic() & CoreReflectionMethod::IS_PUBLIC) |
    ($node->isProtected() & CoreReflectionMethod::IS_PROTECTED) |
    ($node->isPrivate() & CoreReflectionMethod::IS_PRIVATE) |
    ($node->isAbstract() & CoreReflectionMethod::IS_ABSTRACT);

Not for this patch, just for future use-cases. The method calls here are still the biggest offender.

Expand Down
44 changes: 10 additions & 34 deletions src/Reflection/ReflectionProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Closure;
use Error;
use OutOfBoundsException;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Stmt\Property as PropertyNode;
use PhpParser\NodeTraverser;
Expand Down Expand Up @@ -35,7 +34,6 @@

use function array_map;
use function assert;
use function count;
use function func_num_args;
use function is_object;
use function sprintf;
Expand Down Expand Up @@ -675,8 +673,8 @@ private function computeModifiers(PropertyNode $node): int
$modifiers += $node->isProtected() ? CoreReflectionProperty::IS_PROTECTED : 0;
$modifiers += $node->isProtectedSet() ? ReflectionPropertyAdapter::IS_PROTECTED_SET_COMPATIBILITY : 0;
$modifiers += $node->isPublic() ? CoreReflectionProperty::IS_PUBLIC : 0;
$modifiers += ($node->flags & ReflectionPropertyAdapter::IS_FINAL_COMPATIBILITY) === ReflectionPropertyAdapter::IS_FINAL_COMPATIBILITY ? ReflectionPropertyAdapter::IS_FINAL_COMPATIBILITY : 0;
$modifiers += ($node->flags & Modifiers::ABSTRACT) === Modifiers::ABSTRACT ? ReflectionPropertyAdapter::IS_ABSTRACT_COMPATIBILITY : 0;
$modifiers += $node->isFinal() ? ReflectionPropertyAdapter::IS_FINAL_COMPATIBILITY : 0;
$modifiers += $node->isAbstract() ? ReflectionPropertyAdapter::IS_ABSTRACT_COMPATIBILITY : 0;

/** @phpstan-ignore return.type */
return $modifiers;
Expand All @@ -699,51 +697,29 @@ private function computeImmediateVirtual(PropertyNode $node): bool
}
}

if ($setHook !== null && ! $this->computeImmediateVirtualBasedOnSetHook($setHook)) {
if ($setHook !== null && ! $this->computeImmediateVirtualBasedOnHook($setHook)) {
return false;
}

if ($getHook === null) {
return true;
}

return $this->computeImmediateVirtualBasedOnGetHook($getHook);
return $this->computeImmediateVirtualBasedOnHook($getHook);
}

private function computeImmediateVirtualBasedOnGetHook(Node\PropertyHook $getHook): bool
private function computeImmediateVirtualBasedOnHook(Node\PropertyHook $hook): bool
{
$getHookBody = $getHook->getStmts();
$hookBody = $hook->getStmts();

// Abstract property or property in interface
if ($getHookBody === null) {
if ($hookBody === null) {
return true;
}

return ! $this->isHookUsingThisProperty($getHook);
}

private function computeImmediateVirtualBasedOnSetHook(Node\PropertyHook $setHook): bool
{
$setHookBody = $setHook->getStmts();

// Abstract property or property in interface
if ($setHookBody === null) {
return true;
}

// Short syntax
if (count($setHookBody) === 1 && $setHookBody[0] instanceof Node\Stmt\Return_) {
return false;
}

return ! $this->isHookUsingThisProperty($setHook);
}

private function isHookUsingThisProperty(Node\PropertyHook $hook): bool
{
$visitor = new FindingVisitor(static fn (Node $node): bool => $node instanceof Node\Expr\PropertyFetch);
$traverser = new NodeTraverser($visitor);
$traverser->traverse([$hook]);
$traverser->traverse($hookBody);

foreach ($visitor->getFoundNodes() as $propertyFetchNode) {
assert($propertyFetchNode instanceof Node\Expr\PropertyFetch);
Expand All @@ -754,11 +730,11 @@ private function isHookUsingThisProperty(Node\PropertyHook $hook): bool
&& $propertyFetchNode->name instanceof Node\Identifier
&& $propertyFetchNode->name->name === $this->name
) {
return true;
return false;
}
}

return false;
return true;
}

/** @return array{get?: ReflectionMethod, set?: ReflectionMethod} */
Expand Down
11 changes: 11 additions & 0 deletions test/unit/Fixture/PropertyHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,14 @@ class GetPropertyHooksReturnTypes
public $hookWithoutType { get => 'string'; }

}

class FinalPropertyHooks
{
public string $notFinalHook {
set => strtolower($value);
}

public string $finalHook {
final set => strtolower($value);
}
}
21 changes: 21 additions & 0 deletions test/unit/Reflection/ReflectionMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -882,4 +882,25 @@ public function testGetPropertyHookReturnType(string $propertyName, string|null
self::assertNotNull($getHookReflection);
self::assertSame($returnType, $getHookReflection->getReturnType()?->__toString());
}

/** @return list<array{0: non-empty-string, 1: bool}> */
public static function finalPropertyHookProvider(): array
{
return [
['notFinalHook', false],
['finalHook', true],
];
}

#[DataProvider('finalPropertyHookProvider')]
public function testFinalPropertyHook(string $propertyName, bool $isFinal): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../Fixture/PropertyHooks.php', $this->astLocator));
$classInfo = $reflector->reflectClass('Roave\BetterReflectionTest\Fixture\FinalPropertyHooks');

$hookProperty = $classInfo->getProperty($propertyName);
$hookReflection = $hookProperty->getHook(ReflectionPropertyHookType::Set);
self::assertNotNull($hookReflection);
self::assertSame($isFinal, $hookReflection->isFinal());
}
}
Loading