Skip to content

Commit

Permalink
[Transform] Handle with this->method() from current class on MethodCa…
Browse files Browse the repository at this point in the history
…llToMethodCallRector (#2531)

* Add a test for MethodCallToMethodCall issue with replacing current class method

* Add a test for MethodCallToMethodCall issue with replacing current class method

* Closes #2530 Fixes rectorphp/rector#7245

* phpstan

* clean up

* [ci-review] Rector Rectify

* clean up

Co-authored-by: SelrahcD <c.desneuf@gmail.com>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people committed Jun 18, 2022
1 parent c80d7e1 commit 1a3288b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToMethodCallRector\Fixture;

class AClass
{
public function run()
{
$this->methodFromAClass();
}

public function methodFromAClass()
{
}
}

?>
-----
<?php

namespace Rector\Tests\Transform\Rector\MethodCall\MethodCallToMethodCallRector\Fixture;

class AClass
{
public function __construct(private \Rector\Tests\Transform\Rector\MethodCall\MethodCallToMethodCallRector\Fixture\AnotherClass $anotherClass)
{
}
public function run()
{
$this->anotherClass->methodFromAnotherClass();
}

public function methodFromAClass()
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Tests\Transform\Rector\MethodCall\MethodCallToMethodCallRector\Fixture\AClass;
use Rector\Tests\Transform\Rector\MethodCall\MethodCallToMethodCallRector\Fixture\AnotherClass;
use Rector\Tests\Transform\Rector\MethodCall\MethodCallToMethodCallRector\Source\FirstDependency;
use Rector\Tests\Transform\Rector\MethodCall\MethodCallToMethodCallRector\Source\SecondDependency;
use Rector\Transform\Rector\MethodCall\MethodCallToMethodCallRector;
Expand All @@ -12,6 +14,14 @@
$rectorConfig
->ruleWithConfiguration(
MethodCallToMethodCallRector::class,
[new MethodCallToMethodCall(FirstDependency::class, 'go', SecondDependency::class, 'away')]
[
new MethodCallToMethodCall(FirstDependency::class, 'go', SecondDependency::class, 'away'),
new MethodCallToMethodCall(
AClass::class,
'methodFromAClass',
AnotherClass::class,
'methodFromAnotherClass'
),
]
);
};
46 changes: 35 additions & 11 deletions rules/Transform/Rector/MethodCall/MethodCallToMethodCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -91,22 +92,23 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
foreach ($this->methodCallsToMethodsCalls as $methodCallToMethodCall) {
if (! $node->var instanceof PropertyFetch) {
continue;
}
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
if (! $class instanceof Class_) {
return null;
}

if (! $this->isMatch($node, $methodCallToMethodCall)) {
$isMethodCallCurrentClass = $this->isMethodCallCurrentClass($node);
foreach ($this->methodCallsToMethodsCalls as $methodCallToMethodCall) {
if (! $node->var instanceof PropertyFetch && ! $isMethodCallCurrentClass) {
continue;
}

$propertyFetch = $node->var;

$class = $this->betterNodeFinder->findParentType($node, Class_::class);
if (! $class instanceof Class_) {
if (! $this->isMatch($node, $methodCallToMethodCall, $isMethodCallCurrentClass, $class)) {
continue;
}

/** @var PropertyFetch $propertyFetch */
$propertyFetch = $isMethodCallCurrentClass ? $node : $node->var;
$newObjectType = new ObjectType($methodCallToMethodCall->getNewType());

$newPropertyName = $this->matchNewPropertyName($methodCallToMethodCall, $class);
Expand Down Expand Up @@ -138,9 +140,31 @@ public function configure(array $configuration): void
$this->methodCallsToMethodsCalls = $configuration;
}

private function isMatch(MethodCall $methodCall, MethodCallToMethodCall $methodCallToMethodCall): bool
private function isMethodCallCurrentClass(MethodCall $methodCall): bool
{
if (! $this->isObjectType($methodCall->var, new ObjectType($methodCallToMethodCall->getOldType()))) {
return $methodCall->var instanceof Variable && $methodCall->var->name === 'this';
}

private function isMatch(
MethodCall $methodCall,
MethodCallToMethodCall $methodCallToMethodCall,
bool $isMethodCallCurrentClass,
Class_ $class
): bool {
$oldTypeObject = new ObjectType($methodCallToMethodCall->getOldType());

if ($isMethodCallCurrentClass) {
$className = (string) $this->nodeNameResolver->getName($class);
$objectType = new ObjectType($className);

if (! $objectType->equals($oldTypeObject)) {
return false;
}

return $this->isName($methodCall->name, $methodCallToMethodCall->getOldMethod());
}

if (! $this->isObjectType($methodCall->var, $oldTypeObject)) {
return false;
}

Expand Down

0 comments on commit 1a3288b

Please sign in to comment.