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

Support for final modifier in traits #1383

Merged
merged 1 commit into from
Mar 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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