rector/rules/Naming/VariableRenamer.php

127 lines
4.7 KiB
PHP
Raw Normal View History

2020-07-19 10:07:45 +00:00
<?php
declare (strict_types=1);
namespace Rector\Naming;
2020-07-19 10:07:45 +00:00
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverser;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Naming\PhpDoc\VarTagValueNodeRenamer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
2020-07-19 10:07:45 +00:00
final class VariableRenamer
{
/**
* @readonly
* @var \Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser
2020-07-19 10:07:45 +00:00
*/
private $simpleCallableNodeTraverser;
2020-07-19 10:07:45 +00:00
/**
* @readonly
* @var \Rector\NodeNameResolver\NodeNameResolver
2020-07-19 10:07:45 +00:00
*/
private $nodeNameResolver;
2020-07-19 18:19:25 +00:00
/**
* @readonly
* @var \Rector\Naming\PhpDoc\VarTagValueNodeRenamer
2020-07-19 18:19:25 +00:00
*/
private $varTagValueNodeRenamer;
2021-01-19 23:29:52 +00:00
/**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
2021-01-19 23:29:52 +00:00
*/
private $phpDocInfoFactory;
public function __construct(SimpleCallableNodeTraverser $simpleCallableNodeTraverser, NodeNameResolver $nodeNameResolver, VarTagValueNodeRenamer $varTagValueNodeRenamer, PhpDocInfoFactory $phpDocInfoFactory)
{
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
2020-07-19 10:07:45 +00:00
$this->nodeNameResolver = $nodeNameResolver;
2020-07-19 18:19:25 +00:00
$this->varTagValueNodeRenamer = $varTagValueNodeRenamer;
2021-01-19 23:29:52 +00:00
$this->phpDocInfoFactory = $phpDocInfoFactory;
2020-07-19 10:07:45 +00:00
}
public function renameVariableInFunctionLike(FunctionLike $functionLike, string $oldName, string $expectedName, ?Assign $assign = null) : bool
{
$isRenamingActive = \false;
if (!$assign instanceof Assign) {
$isRenamingActive = \true;
}
$hasRenamed = \false;
$currentStmt = null;
$currentClosure = null;
$this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $functionLike->getStmts(), function (Node $node) use($oldName, $expectedName, $assign, &$isRenamingActive, &$hasRenamed, &$currentStmt, &$currentClosure) {
// skip param names
if ($node instanceof Param) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if ($assign instanceof Assign && $node === $assign) {
$isRenamingActive = \true;
return null;
}
if ($node instanceof Stmt) {
$currentStmt = $node;
}
if ($node instanceof Closure) {
$currentClosure = $node;
}
if (!$node instanceof Variable) {
return null;
}
// TODO: Should be implemented in BreakingVariableRenameGuard::shouldSkipParam()
if ($this->isParamInParentFunction($node, $currentClosure)) {
return null;
2020-07-19 10:07:45 +00:00
}
if (!$isRenamingActive) {
return null;
}
$variable = $this->renameVariableIfMatchesName($node, $oldName, $expectedName, $currentStmt);
if ($variable instanceof Variable) {
$hasRenamed = \true;
}
return $variable;
});
return $hasRenamed;
2020-07-19 10:07:45 +00:00
}
private function isParamInParentFunction(Variable $variable, ?Closure $closure) : bool
{
if (!$closure instanceof Closure) {
return \false;
}
$variableName = $this->nodeNameResolver->getName($variable);
if ($variableName === null) {
return \false;
}
foreach ($closure->params as $param) {
if ($this->nodeNameResolver->isName($param, $variableName)) {
return \true;
}
}
return \false;
}
private function renameVariableIfMatchesName(Variable $variable, string $oldName, string $expectedName, ?Stmt $currentStmt) : ?Variable
2020-07-19 23:23:11 +00:00
{
if (!$this->nodeNameResolver->isName($variable, $oldName)) {
2020-07-19 23:23:11 +00:00
return null;
}
$variable->name = $expectedName;
$variablePhpDocInfo = $this->resolvePhpDocInfo($variable, $currentStmt);
2021-01-19 23:29:52 +00:00
$this->varTagValueNodeRenamer->renameAssignVarTagVariableName($variablePhpDocInfo, $oldName, $expectedName);
2020-07-19 23:23:11 +00:00
return $variable;
}
2021-01-19 23:29:52 +00:00
/**
* Expression doc block has higher priority
*/
private function resolvePhpDocInfo(Variable $variable, ?Stmt $currentStmt) : PhpDocInfo
2021-01-19 23:29:52 +00:00
{
if ($currentStmt instanceof Stmt) {
return $this->phpDocInfoFactory->createFromNodeOrEmpty($currentStmt);
2021-01-19 23:29:52 +00:00
}
return $this->phpDocInfoFactory->createFromNodeOrEmpty($variable);
}
2020-07-19 10:07:45 +00:00
}