2020-07-13 21:13:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Naming\Rector\Assign;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\ArrowFunction;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\FunctionLike;
|
|
|
|
use PhpParser\Node\Param;
|
2020-07-19 10:16:20 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Function_;
|
2020-07-19 09:53:35 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
2020-07-13 21:13:40 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\RectorDefinition\CodeSample;
|
|
|
|
use Rector\Core\RectorDefinition\RectorDefinition;
|
2020-07-19 10:25:24 +00:00
|
|
|
use Rector\Naming\Guard\BreakingVariableRenameGuard;
|
2020-07-13 21:13:40 +00:00
|
|
|
use Rector\Naming\Naming\ExpectedNameResolver;
|
2020-07-19 10:16:20 +00:00
|
|
|
use Rector\Naming\VariableRenamer;
|
2020-07-19 09:53:35 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2020-07-13 21:13:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \Rector\Naming\Tests\Rector\Assign\RenameVariableToMatchGetMethodNameRector\RenameVariableToMatchGetMethodNameRectorTest
|
|
|
|
*/
|
|
|
|
final class RenameVariableToMatchGetMethodNameRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ExpectedNameResolver
|
|
|
|
*/
|
|
|
|
private $expectedNameResolver;
|
|
|
|
|
2020-07-19 10:16:20 +00:00
|
|
|
/**
|
|
|
|
* @var VariableRenamer
|
|
|
|
*/
|
|
|
|
private $variableRenamer;
|
|
|
|
|
2020-07-19 10:25:24 +00:00
|
|
|
/**
|
|
|
|
* @var BreakingVariableRenameGuard
|
|
|
|
*/
|
|
|
|
private $breakingVariableRenameGuard;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ExpectedNameResolver $expectedNameResolver,
|
|
|
|
VariableRenamer $variableRenamer,
|
|
|
|
BreakingVariableRenameGuard $breakingVariableRenameGuard
|
|
|
|
) {
|
2020-07-13 21:13:40 +00:00
|
|
|
$this->expectedNameResolver = $expectedNameResolver;
|
2020-07-19 10:16:20 +00:00
|
|
|
$this->variableRenamer = $variableRenamer;
|
2020-07-19 10:25:24 +00:00
|
|
|
$this->breakingVariableRenameGuard = $breakingVariableRenameGuard;
|
2020-07-13 21:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition('Rename variable to match get method name', [
|
|
|
|
new CodeSample(
|
|
|
|
<<<'PHP'
|
|
|
|
class SomeClass {
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$a = $this->getRunner();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PHP
|
|
|
|
,
|
|
|
|
<<<'PHP'
|
|
|
|
class SomeClass {
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$runner = $this->getRunner();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PHP
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [Assign::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Assign $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2020-07-19 10:25:24 +00:00
|
|
|
if ($node->expr instanceof ArrowFunction || ! $node->var instanceof Variable) {
|
2020-07-13 21:13:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$newName = $this->expectedNameResolver->resolveForGetCallExpr($node->expr);
|
|
|
|
if ($newName === null || $this->isName($node, $newName)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-19 10:25:24 +00:00
|
|
|
$currentName = $this->getName($node->var);
|
|
|
|
if ($currentName === null) {
|
2020-07-13 21:13:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-19 10:40:36 +00:00
|
|
|
$functionLike = $this->getCurrentFunctionLike($node);
|
|
|
|
if ($functionLike === null) {
|
2020-07-19 10:25:24 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-07-13 21:13:40 +00:00
|
|
|
|
2020-07-19 10:25:24 +00:00
|
|
|
if ($this->breakingVariableRenameGuard->shouldSkipVariable(
|
|
|
|
$currentName,
|
|
|
|
$newName,
|
2020-07-19 10:40:36 +00:00
|
|
|
$functionLike,
|
2020-07-19 10:25:24 +00:00
|
|
|
$node->var
|
|
|
|
)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->renameVariable($node, $newName);
|
2020-07-13 21:13:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 10:25:24 +00:00
|
|
|
private function renameVariable(Assign $assign, string $newName): Assign
|
2020-07-13 21:13:40 +00:00
|
|
|
{
|
|
|
|
/** @var Variable $variableNode */
|
2020-07-19 09:53:35 +00:00
|
|
|
$variableNode = $assign->var;
|
2020-07-13 21:13:40 +00:00
|
|
|
|
|
|
|
/** @var string $originalName */
|
|
|
|
$originalName = $variableNode->name;
|
|
|
|
|
2020-07-19 10:16:20 +00:00
|
|
|
$this->renameInDocComment($assign, $originalName, $newName);
|
2020-07-13 21:13:40 +00:00
|
|
|
|
2020-07-19 10:16:20 +00:00
|
|
|
$functionLike = $this->getCurrentFunctionLike($assign);
|
|
|
|
if ($functionLike === null) {
|
2020-07-19 10:25:24 +00:00
|
|
|
return $assign;
|
2020-07-13 21:13:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 11:22:55 +00:00
|
|
|
$this->variableRenamer->renameVariableInFunctionLike($functionLike, $assign, $originalName, $newName);
|
2020-07-19 10:25:24 +00:00
|
|
|
|
|
|
|
return $assign;
|
2020-07-13 21:13:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 10:16:20 +00:00
|
|
|
/**
|
2020-07-19 10:40:36 +00:00
|
|
|
* @return ClassMethod|Function_|Closure|null
|
2020-07-19 10:16:20 +00:00
|
|
|
*/
|
|
|
|
private function getCurrentFunctionLike(Node $node): ?FunctionLike
|
2020-07-13 21:13:40 +00:00
|
|
|
{
|
2020-07-19 10:40:36 +00:00
|
|
|
return $node->getAttribute(AttributeKey::CLOSURE_NODE) ??
|
|
|
|
$node->getAttribute(AttributeKey::METHOD_NODE) ??
|
|
|
|
$node->getAttribute(AttributeKey::FUNCTION_NODE);
|
2020-07-13 21:13:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 10:16:20 +00:00
|
|
|
private function renameInDocComment(Node $node, string $originalName, string $newName): void
|
2020-07-13 21:13:40 +00:00
|
|
|
{
|
2020-07-19 09:53:35 +00:00
|
|
|
/** @var PhpDocInfo|null $phpDocInfo */
|
2020-07-19 10:16:20 +00:00
|
|
|
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
|
2020-07-19 09:53:35 +00:00
|
|
|
if ($phpDocInfo === null) {
|
2020-07-19 09:41:35 +00:00
|
|
|
return;
|
2020-07-13 21:13:40 +00:00
|
|
|
}
|
2020-07-19 09:41:35 +00:00
|
|
|
|
2020-07-19 09:53:35 +00:00
|
|
|
$varTagValueNode = $phpDocInfo->getByType(VarTagValueNode::class);
|
|
|
|
if ($varTagValueNode === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($varTagValueNode->variableName !== '$' . $originalName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$varTagValueNode->variableName = '$' . $newName;
|
2020-07-13 21:13:40 +00:00
|
|
|
}
|
|
|
|
}
|