rector/src/Rector/Assign/PropertyAssignToMethodCallRector.php

125 lines
3.6 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace Rector\Rector\Assign;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
2017-11-06 11:48:43 +00:00
use PhpParser\Node\Expr\PropertyFetch;
2017-11-06 13:15:35 +00:00
use PhpParser\Node\Expr\Variable;
2017-10-29 12:13:40 +00:00
use Rector\Node\MethodCallNodeFactory;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Rector\AbstractRector;
2018-08-01 16:15:06 +00:00
use Rector\RectorDefinition\ConfiguredCodeSample;
2018-04-07 23:35:59 +00:00
use Rector\RectorDefinition\RectorDefinition;
final class PropertyAssignToMethodCallRector extends AbstractRector
{
/**
* @var PropertyFetchAnalyzer
*/
private $propertyFetchAnalyzer;
/**
2017-10-29 12:13:40 +00:00
* @var MethodCallNodeFactory
*/
2017-10-29 12:13:40 +00:00
private $methodCallNodeFactory;
/**
* @var string[][]
*/
private $oldPropertiesToNewMethodCallsByType = [];
/**
* @param string[][] $oldPropertiesToNewMethodCallsByType
*/
2017-10-29 12:13:40 +00:00
public function __construct(
PropertyFetchAnalyzer $propertyFetchAnalyzer,
MethodCallNodeFactory $methodCallNodeFactory,
array $oldPropertiesToNewMethodCallsByType
2017-10-29 12:13:40 +00:00
) {
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
2017-10-29 12:13:40 +00:00
$this->methodCallNodeFactory = $methodCallNodeFactory;
$this->oldPropertiesToNewMethodCallsByType = $oldPropertiesToNewMethodCallsByType;
}
2018-04-07 23:35:59 +00:00
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Turns property assign of specific type and property name to method call', [
2018-08-01 16:15:06 +00:00
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
$someObject = new SomeClass;
$someObject->oldProperty = false;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$someObject = new SomeClass;
$someObject->newMethodCall(false);
CODE_SAMPLE
,
[
'$oldPropertiesToNewMethodCallsByType' => [
'SomeClass' => [
'oldPropertyName' => 'oldProperty',
'newMethodName' => 'newMethodCall',
],
],
2018-08-01 16:15:06 +00:00
]
),
2018-04-07 23:35:59 +00:00
]);
}
2018-08-14 22:12:41 +00:00
/**
* @return string[]
*/
public function getNodeTypes(): array
{
2018-08-14 22:12:41 +00:00
return [Assign::class];
}
/**
* @param Assign $assignNode
*/
public function refactor(Node $assignNode): ?Node
{
foreach ($this->oldPropertiesToNewMethodCallsByType as $type => $oldPropertiesToNewMethodCalls) {
if (! $this->propertyFetchAnalyzer->isTypeAndProperties(
$assignNode->var,
$type,
array_keys($oldPropertiesToNewMethodCalls)
)) {
continue;
}
/** @var PropertyFetch $propertyFetchNode */
$propertyFetchNode = $assignNode->var;
return $this->processPropertyFetch($propertyFetchNode, $oldPropertiesToNewMethodCalls, $assignNode->expr);
}
2018-08-14 22:12:41 +00:00
return $assignNode;
}
2017-11-06 11:48:43 +00:00
/**
* @param string[] $oldPropertiesToNewMethodCalls
*/
private function processPropertyFetch(
PropertyFetch $propertyFetchNode,
array $oldPropertiesToNewMethodCalls,
Node $assignedNode
): MethodCall {
2017-11-06 13:15:35 +00:00
/** @var Variable $propertyNode */
2017-11-06 11:48:43 +00:00
$propertyNode = $propertyFetchNode->var;
foreach ($oldPropertiesToNewMethodCalls as $oldProperty => $newMethodCall) {
if ((string) $propertyFetchNode->name === $oldProperty) {
return $this->methodCallNodeFactory->createWithVariableMethodNameAndArguments(
$propertyNode,
$newMethodCall,
[$assignedNode]
);
}
}
}
}