Skip to content

Commit

Permalink
[Defluent] Skip clone uses trait on DefluentReturnMethodCallRector (#558
Browse files Browse the repository at this point in the history
)

* [Defluent] Skip clone uses trait on DefluentReturnMethodCallRector

* debug

* Fixed 🎉

* phpstan

* final touch

* revert return null on parseFileNameToDecoratedNodes()

* phpstan

* phpstan

* phpstan

* phpstan
  • Loading branch information
samsonasik committed Aug 1, 2021
1 parent 0a30f6f commit b34ff8c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Defluent\Rector\Return_\DefluentReturnMethodCallRector\Fixture;

use Rector\Tests\Defluent\Rector\Return_\DefluentReturnMethodCallRector\Source\UsesTrait;

final class SkipCloneObjectAssignUseTrait
{
public function run(UsesTrait $usesTrait)
{
return $usesTrait->withStatus(500);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Defluent\Rector\Return_\DefluentReturnMethodCallRector\Source;

trait SelfButCloneAssignVersionTrait
{
public $status;

public function withStatus($status): self
{
$self = clone $this;
$self->status = $status;

return $self;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Defluent\Rector\Return_\DefluentReturnMethodCallRector\Source;

class UsesTrait
{
use SelfButCloneAssignVersionTrait;
}
23 changes: 22 additions & 1 deletion src/PhpParser/AstResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@ public function resolveClassMethod(string $className, string $methodName): ?Clas
return null;
}

return $this->resolveClassMethodFromMethodReflection($methodReflection);
$classMethod = $this->resolveClassMethodFromMethodReflection($methodReflection);

if (! $classMethod instanceof ClassMethod) {
return $this->locateClassMethodInTrait($methodName, $methodReflection);
}

return $classMethod;
}

public function resolveClassMethodFromMethodCall(MethodCall $methodCall): ?ClassMethod
Expand Down Expand Up @@ -340,6 +346,21 @@ public function resolvePropertyFromPropertyReflection(
return $this->findPromotedPropertyByName($nodes, $desiredPropertyName);
}

private function locateClassMethodInTrait(string $methodName, MethodReflection $methodReflection): ?ClassMethod
{
$classReflection = $methodReflection->getDeclaringClass();
$traits = $this->parseClassReflectionTraits($classReflection);

/** @var ClassMethod|null $classMethod */
$classMethod = $this->betterNodeFinder->findFirst(
$traits,
fn (Node $node): bool => $node instanceof ClassMethod && $this->nodeNameResolver->isName($node, $methodName)
);
$this->classMethodsByClassAndMethod[$classReflection->getName()][$methodName] = $classMethod;

return $classMethod;
}

/**
* @return Stmt[]|null
*/
Expand Down

0 comments on commit b34ff8c

Please sign in to comment.