Skip to content

Commit 166e4ca

Browse files
committed
Property hook can be final
1 parent 9e682f8 commit 166e4ca

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

psalm-baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
<code><![CDATA[classExists]]></code>
167167
<code><![CDATA[isAbstract]]></code>
168168
<code><![CDATA[isFinal]]></code>
169+
<code><![CDATA[isFinal]]></code>
169170
<code><![CDATA[isPrivate]]></code>
170171
<code><![CDATA[isProtected]]></code>
171172
<code><![CDATA[isPublic]]></code>

src/Reflection/ReflectionMethod.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private function __construct(
5353
assert($node instanceof MethodNode || $node instanceof Node\PropertyHook);
5454

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

5858
$this->fillFromNode($node);
5959
}
@@ -305,13 +305,18 @@ public function getModifiers(): int
305305
}
306306

307307
/** @return int-mask-of<ReflectionMethodAdapter::IS_*> */
308-
private function computeModifiers(MethodNode $node): int
308+
private function computeModifiers(MethodNode|Node\PropertyHook $node): int
309309
{
310-
$modifiers = $node->isStatic() ? CoreReflectionMethod::IS_STATIC : 0;
311-
$modifiers += $node->isPublic() ? CoreReflectionMethod::IS_PUBLIC : 0;
312-
$modifiers += $node->isProtected() ? CoreReflectionMethod::IS_PROTECTED : 0;
313-
$modifiers += $node->isPrivate() ? CoreReflectionMethod::IS_PRIVATE : 0;
314-
$modifiers += $node->isAbstract() ? CoreReflectionMethod::IS_ABSTRACT : 0;
310+
$modifiers = 0;
311+
312+
if ($node instanceof MethodNode) {
313+
$modifiers += $node->isStatic() ? CoreReflectionMethod::IS_STATIC : 0;
314+
$modifiers += $node->isPublic() ? CoreReflectionMethod::IS_PUBLIC : 0;
315+
$modifiers += $node->isProtected() ? CoreReflectionMethod::IS_PROTECTED : 0;
316+
$modifiers += $node->isPrivate() ? CoreReflectionMethod::IS_PRIVATE : 0;
317+
$modifiers += $node->isAbstract() ? CoreReflectionMethod::IS_ABSTRACT : 0;
318+
}
319+
315320
$modifiers += $node->isFinal() ? CoreReflectionMethod::IS_FINAL : 0;
316321

317322
return $modifiers;

test/unit/Fixture/PropertyHooks.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,14 @@ class GetPropertyHooksReturnTypes
153153
public $hookWithoutType { get => 'string'; }
154154

155155
}
156+
157+
class FinalPropertyHooks
158+
{
159+
public string $notFinalHook {
160+
set => strtolower($value);
161+
}
162+
163+
public string $finalHook {
164+
final set => strtolower($value);
165+
}
166+
}

test/unit/Reflection/ReflectionMethodTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,4 +882,25 @@ public function testGetPropertyHookReturnType(string $propertyName, string|null
882882
self::assertNotNull($getHookReflection);
883883
self::assertSame($returnType, $getHookReflection->getReturnType()?->__toString());
884884
}
885+
886+
/** @return list<array{0: non-empty-string, 1: bool}> */
887+
public static function finalPropertyHookProvider(): array
888+
{
889+
return [
890+
['notFinalHook', false],
891+
['finalHook', true],
892+
];
893+
}
894+
895+
#[DataProvider('finalPropertyHookProvider')]
896+
public function testFinalPropertyHook(string $propertyName, bool $isFinal): void
897+
{
898+
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../Fixture/PropertyHooks.php', $this->astLocator));
899+
$classInfo = $reflector->reflectClass('Roave\BetterReflectionTest\Fixture\FinalPropertyHooks');
900+
901+
$hookProperty = $classInfo->getProperty($propertyName);
902+
$hookReflection = $hookProperty->getHook(ReflectionPropertyHookType::Set);
903+
self::assertNotNull($hookReflection);
904+
self::assertSame($isFinal, $hookReflection->isFinal());
905+
}
885906
}

0 commit comments

Comments
 (0)