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

Clean MethodToMethodCallRector #2554

Merged
merged 22 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

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

use Rector\Tests\Transform\Rector\MethodCall\MethodCallToMethodCallRector\Source\FirstDependency;

function aFunctionReturningFirstDependency() : FirstDependency {
return new FirstDependency();
}

aFunctionReturningFirstDependency()->go();
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

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

use Rector\Tests\Transform\Rector\MethodCall\MethodCallToMethodCallRector\Source\FirstDependency;

function aFunctionReturningFirstDependency() : FirstDependency {
return new FirstDependency();
}

class AClass {
public function aMethod()
{
aFunctionReturningFirstDependency()->go();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

use Rector\Tests\Transform\Rector\MethodCall\MethodCallToMethodCallRector\Source\FirstDependency;

class AClass extends FirstDependency{

public function aMethod()
{
$this->go();
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

final class FirstDependency
class FirstDependency
SelrahcD marked this conversation as resolved.
Show resolved Hide resolved
{
public function go()
{
Expand Down
24 changes: 12 additions & 12 deletions rules/Transform/Rector/MethodCall/MethodCallToMethodCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,26 @@ public function refactor(Node $node): ?Node
}

$isMethodCallCurrentClass = $this->isMethodCallCurrentClass($node);

if(!$node->var instanceof PropertyFetch && !$node->var instanceof Variable) {
SelrahcD marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

foreach ($this->methodCallsToMethodsCalls as $methodCallToMethodCall) {
if (! $node->var instanceof PropertyFetch && ! $isMethodCallCurrentClass) {
SelrahcD marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

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);
if ($newPropertyName === null) {
continue;
}

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

$propertyMetadata = new PropertyMetadata($newPropertyName, $newObjectType, Class_::MODIFIER_PRIVATE);
$this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata);

Expand Down Expand Up @@ -153,15 +155,13 @@ private function isMatch(
): bool {
$oldTypeObject = new ObjectType($methodCallToMethodCall->getOldType());

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

if (! $objectType->equals($oldTypeObject)) {
SelrahcD marked this conversation as resolved.
Show resolved Hide resolved
if(!$currentClassObjectType->equals($oldTypeObject)) {
return false;
}

return $this->isName($methodCall->name, $methodCallToMethodCall->getOldMethod());
SelrahcD marked this conversation as resolved.
Show resolved Hide resolved
}

if (! $this->isObjectType($methodCall->var, $oldTypeObject)) {
Expand Down