Skip to content

Commit

Permalink
Support for final modifiers in traits
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Feb 2, 2024
1 parent ec8ab86 commit 4ffa5d3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Better Reflection - an improved code reflection API",
"license": "MIT",
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"php": "~8.1.0 || ~8.2.0 || ~8.3.2",
"ext-json": "*",
"jetbrains/phpstorm-stubs": "2023.3",
"nikic/php-parser": "^4.18.0",
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

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

8 changes: 7 additions & 1 deletion src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,13 @@ private function createMethodsFromTrait(ReflectionMethod $method): array

if (array_key_exists($methodHash, $this->traitsData['modifiers'])) {
// PhpParser modifiers are compatible with PHP reflection modifiers
$methodModifiers = ($methodModifiers & ~ Node\Stmt\Class_::VISIBILITY_MODIFIER_MASK) | $this->traitsData['modifiers'][$methodHash];
if ($this->traitsData['modifiers'][$methodHash] & Node\Stmt\Class_::VISIBILITY_MODIFIER_MASK) {
$methodModifiers = ($methodModifiers & ~ Node\Stmt\Class_::VISIBILITY_MODIFIER_MASK) | $this->traitsData['modifiers'][$methodHash];
}

if ($this->traitsData['modifiers'][$methodHash] & Node\Stmt\Class_::MODIFIER_FINAL) {
$methodModifiers |= Node\Stmt\Class_::MODIFIER_FINAL;
}
}

$createMethod = function (string|null $aliasMethodName) use ($method, $methodModifiers): ReflectionMethod {
Expand Down
21 changes: 21 additions & 0 deletions test/unit/Fixture/TraitFixtureWithFinal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

trait SimpleTrait
{
protected function foo() {}
final protected function boo() {}
}

class TraitFixtureWithFinal
{
use SimpleTrait {
foo as final;
}
}

class TraitFixtureWithPublic
{
use SimpleTrait {
boo as public;
}
}
26 changes: 26 additions & 0 deletions test/unit/Reflection/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\TestCase;
use Qux;
use ReflectionClass as CoreReflectionClass;
Expand Down Expand Up @@ -1617,6 +1618,31 @@ public function testMethodsFromTraits(): void
self::assertSame('TraitFixtureTraitC2', $classInfo->getMethod('d_renamed')->getDeclaringClass()->getName());
}

#[RequiresPhp('8.3')]
public function testMethodsFromTraitsWithFinal(): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(
__DIR__ . '/../Fixture/TraitFixtureWithFinal.php',
$this->astLocator,
));

$classInfo = $reflector->reflectClass('TraitFixtureWithFinal');

self::assertTrue($classInfo->hasMethod('foo'));
self::assertTrue($classInfo->getMethod('foo')->isFinal());
self::assertFalse($classInfo->getMethod('foo')->isPrivate());
self::assertTrue($classInfo->getMethod('foo')->isProtected());
self::assertFalse($classInfo->getMethod('foo')->isPublic());

$classInfo = $reflector->reflectClass('TraitFixtureWithPublic');

self::assertTrue($classInfo->hasMethod('boo'));
self::assertTrue($classInfo->getMethod('boo')->isFinal());
self::assertFalse($classInfo->getMethod('boo')->isPrivate());
self::assertFalse($classInfo->getMethod('boo')->isProtected());
self::assertTrue($classInfo->getMethod('boo')->isPublic());
}

public function testMethodsFromTraitsWithConflicts(): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(
Expand Down

0 comments on commit 4ffa5d3

Please sign in to comment.