Skip to content

Commit

Permalink
Fixed trait method rename with multiple traits in one "use" statements
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Sep 17, 2021
1 parent 0ba851c commit 5d23e18
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
59 changes: 59 additions & 0 deletions test/unit/Reflection/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<?php
trait HelloWorldTraitTest
{
}
trait HelloWorldTrait
{
public function sayHello(): void
{
}
}
class HelloWorld
{
use HelloWorldTraitTest;
use HelloWorldTrait {
sayHello as hello;
}
}
PHP;

$reflection = (new ClassReflector(new StringSourceLocator($php, $this->astLocator)))->reflect('HelloWorld');
self::assertTrue($reflection->hasMethod('hello'));
}

public function testTraitMultipleUsesWithMethodRename(): void
{
$php = <<<'PHP'
<?php
trait HelloWorldTraitTest
{
}
trait HelloWorldTrait
{
public function sayHello(): void
{
}
}
class HelloWorld
{
use HelloWorldTraitTest, HelloWorldTrait {
sayHello as hello;
}
}
PHP;

$reflection = (new ClassReflector(new StringSourceLocator($php, $this->astLocator)))->reflect('HelloWorld');
self::assertTrue($reflection->hasMethod('hello'));
}
}

0 comments on commit 5d23e18

Please sign in to comment.