rector/src/Rector/Assign/PropertyAssignToMethodCallRector.php

110 lines
2.8 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace Rector\Rector\Assign;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
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[]
*/
2018-06-06 13:12:31 +00:00
private $types = [];
/**
* @var string
*/
private $oldPropertyName;
/**
* @var string
*/
private $newMethodName;
/**
2018-08-01 16:15:06 +00:00
* @todo check via https://github.com/rectorphp/rector/issues/548
2018-06-06 13:12:31 +00:00
* @param string[] $types
*/
2017-10-29 12:13:40 +00:00
public function __construct(
PropertyFetchAnalyzer $propertyFetchAnalyzer,
MethodCallNodeFactory $methodCallNodeFactory,
2018-08-01 18:21:09 +00:00
array $types,
string $oldPropertyName,
string $newMethodName
2017-10-29 12:13:40 +00:00
) {
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
2017-10-29 12:13:40 +00:00
$this->methodCallNodeFactory = $methodCallNodeFactory;
$this->types = $types;
$this->oldPropertyName = $oldPropertyName;
$this->newMethodName = $newMethodName;
}
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
,
[
'$types' => ['SomeClass'],
'$oldPropertyName' => 'oldProperty',
'$newMethodName' => 'newMethodCall',
]
),
2018-04-07 23:35:59 +00:00
]);
}
public function isCandidate(Node $node): bool
{
if (! $node instanceof Assign) {
return false;
}
2018-06-06 13:12:31 +00:00
return $this->propertyFetchAnalyzer->isTypesAndProperty($node->var, $this->types, $this->oldPropertyName);
}
/**
* @param Assign $assignNode
*/
public function refactor(Node $assignNode): ?Node
{
2017-11-06 11:48:43 +00:00
/** @var PropertyFetch $propertyFetchNode */
$propertyFetchNode = $assignNode->var;
2017-11-06 13:15:35 +00:00
/** @var Variable $propertyNode */
2017-11-06 11:48:43 +00:00
$propertyNode = $propertyFetchNode->var;
2017-10-29 12:16:01 +00:00
return $this->methodCallNodeFactory->createWithVariableMethodNameAndArguments(
$propertyNode,
$this->newMethodName,
[$assignNode->expr]
);
}
}