From 5d23e18608dd1c53900db4b52105d5b5dc98fe73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Fri, 17 Sep 2021 14:01:48 +0200 Subject: [PATCH] Fixed trait method rename with multiple traits in one "use" statements --- src/Reflection/ReflectionClass.php | 3 +- test/unit/Reflection/ReflectionClassTest.php | 59 ++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/Reflection/ReflectionClass.php b/src/Reflection/ReflectionClass.php index 6d6f693c8..de92ae71e 100644 --- a/src/Reflection/ReflectionClass.php +++ b/src/Reflection/ReflectionClass.php @@ -45,6 +45,7 @@ use function array_slice; use function array_values; use function assert; +use function end; use function implode; use function in_array; use function ltrim; @@ -1038,7 +1039,7 @@ private function parseTraitUsages(): void foreach ($adaptations as $adaptation) { $usedTrait = $adaptation->trait; if ($usedTrait === null) { - $usedTrait = $traitNames[0]; + $usedTrait = end($traitNames); } if ($adaptation instanceof Node\Stmt\TraitUseAdaptation\Alias && $adaptation->newName) { diff --git a/test/unit/Reflection/ReflectionClassTest.php b/test/unit/Reflection/ReflectionClassTest.php index 4a0d093c0..04f0818a5 100644 --- a/test/unit/Reflection/ReflectionClassTest.php +++ b/test/unit/Reflection/ReflectionClassTest.php @@ -1911,4 +1911,63 @@ public function sayHello(int $date): void $reflection = (new ClassReflector(new StringSourceLocator($php, $this->astLocator)))->reflect('HelloWorld'); self::assertTrue($reflection->hasMethod('myRenamedMethod')); } + + public function testTraitSeparateUsesWithMethodRename(): void + { + $php = <<<'PHP' + astLocator)))->reflect('HelloWorld'); + self::assertTrue($reflection->hasMethod('hello')); + } + + public function testTraitMultipleUsesWithMethodRename(): void + { + $php = <<<'PHP' + astLocator)))->reflect('HelloWorld'); + self::assertTrue($reflection->hasMethod('hello')); + } }