mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-29 19:37:55 +01:00
make BreakingVariableRenameGuard work with Function_ to
This commit is contained in:
parent
b9a925a5cf
commit
5b614ef6a6
@ -70,7 +70,7 @@ final class BreakingVariableRenameGuard
|
||||
public function shouldSkipVariable(
|
||||
string $currentName,
|
||||
string $expectedName,
|
||||
ClassMethod $classMethod,
|
||||
Node\FunctionLike $functionLike,
|
||||
Variable $variable
|
||||
): bool {
|
||||
// is the suffix? → also accepted
|
||||
@ -78,11 +78,11 @@ final class BreakingVariableRenameGuard
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->conflictingNameResolver->checkNameIsInClassMethod($expectedName, $classMethod)) {
|
||||
if ($this->conflictingNameResolver->checkNameIsInFunctionLike($expectedName, $functionLike)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->overridenExistingNamesResolver->checkNameInClassMethodForNew($currentName, $classMethod)) {
|
||||
if ($this->overridenExistingNamesResolver->checkNameInClassMethodForNew($currentName, $functionLike)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -115,7 +115,7 @@ final class BreakingVariableRenameGuard
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->conflictingNameResolver->checkNameIsInClassMethod($expectedName, $classMethod)) {
|
||||
if ($this->conflictingNameResolver->checkNameIsInFunctionLike($expectedName, $classMethod)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace Rector\Naming\Naming;
|
||||
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
@ -87,27 +88,27 @@ final class ConflictingNameResolver
|
||||
return $this->arrayFilter->filterWithAtLeastTwoOccurences($expectedNames);
|
||||
}
|
||||
|
||||
public function checkNameIsInClassMethod(string $variableName, ClassMethod $classMethod): bool
|
||||
public function checkNameIsInFunctionLike(string $variableName, FunctionLike $functionLike): bool
|
||||
{
|
||||
$conflictingVariableNames = $this->resolveConflictingVariableNamesForNew($classMethod);
|
||||
$conflictingVariableNames = $this->resolveConflictingVariableNamesForNew($functionLike);
|
||||
return in_array($variableName, $conflictingVariableNames, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function resolveConflictingVariableNamesForNew(ClassMethod $classMethod): array
|
||||
private function resolveConflictingVariableNamesForNew(FunctionLike $functionLike): array
|
||||
{
|
||||
// cache it!
|
||||
$classMethodHash = spl_object_hash($classMethod);
|
||||
$classMethodHash = spl_object_hash($functionLike);
|
||||
|
||||
if (isset($this->conflictingVariableNamesByClassMethod[$classMethodHash])) {
|
||||
return $this->conflictingVariableNamesByClassMethod[$classMethodHash];
|
||||
}
|
||||
|
||||
$paramNames = $this->collectParamNames($classMethod);
|
||||
$newAssignNames = $this->resolveForNewAssigns($classMethod);
|
||||
$nonNewAssignNames = $this->resolveForNonNewAssigns($classMethod);
|
||||
$paramNames = $this->collectParamNames($functionLike);
|
||||
$newAssignNames = $this->resolveForNewAssigns($functionLike);
|
||||
$nonNewAssignNames = $this->resolveForNonNewAssigns($functionLike);
|
||||
|
||||
$protectedNames = array_merge($paramNames, $newAssignNames, $nonNewAssignNames);
|
||||
|
||||
@ -120,12 +121,12 @@ final class ConflictingNameResolver
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function collectParamNames(ClassMethod $classMethod): array
|
||||
private function collectParamNames(FunctionLike $functionLike): array
|
||||
{
|
||||
$paramNames = [];
|
||||
|
||||
// params
|
||||
foreach ($classMethod->params as $param) {
|
||||
foreach ($functionLike->params as $param) {
|
||||
/** @var string $paramName */
|
||||
$paramName = $this->nodeNameResolver->getName($param);
|
||||
$paramNames[] = $paramName;
|
||||
@ -137,12 +138,12 @@ final class ConflictingNameResolver
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function resolveForNewAssigns(ClassMethod $classMethod): array
|
||||
private function resolveForNewAssigns(FunctionLike $functionLike): array
|
||||
{
|
||||
$names = [];
|
||||
|
||||
/** @var Assign[] $assigns */
|
||||
$assigns = $this->betterNodeFinder->findInstanceOf((array) $classMethod->stmts, Assign::class);
|
||||
$assigns = $this->betterNodeFinder->findInstanceOf((array) $functionLike->stmts, Assign::class);
|
||||
foreach ($assigns as $assign) {
|
||||
$name = $this->expectedNameResolver->resolveForAssignNew($assign);
|
||||
if ($name === null) {
|
||||
@ -155,12 +156,15 @@ final class ConflictingNameResolver
|
||||
return $names;
|
||||
}
|
||||
|
||||
private function resolveForNonNewAssigns(ClassMethod $classMethod)
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function resolveForNonNewAssigns(FunctionLike $functionLike): array
|
||||
{
|
||||
$names = [];
|
||||
|
||||
/** @var Assign[] $assigns */
|
||||
$assigns = $this->betterNodeFinder->findInstanceOf((array) $classMethod->stmts, Assign::class);
|
||||
$assigns = $this->betterNodeFinder->findInstanceOf((array) $functionLike->stmts, Assign::class);
|
||||
foreach ($assigns as $assign) {
|
||||
$name = $this->expectedNameResolver->resolveForAssignNonNew($assign);
|
||||
if ($name === null) {
|
||||
|
@ -6,6 +6,7 @@ namespace Rector\Naming\Naming;
|
||||
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\Naming\PhpArray\ArrayFilter;
|
||||
@ -43,9 +44,9 @@ final class OverridenExistingNamesResolver
|
||||
$this->arrayFilter = $arrayFilter;
|
||||
}
|
||||
|
||||
public function checkNameInClassMethodForNew(string $variableName, ClassMethod $classMethod): bool
|
||||
public function checkNameInClassMethodForNew(string $variableName, FunctionLike $functionLie): bool
|
||||
{
|
||||
$overridenVariableNames = $this->resolveOveriddenNamesForNew($classMethod);
|
||||
$overridenVariableNames = $this->resolveOveriddenNamesForNew($functionLie);
|
||||
return in_array($variableName, $overridenVariableNames, true);
|
||||
}
|
||||
|
||||
@ -73,9 +74,9 @@ final class OverridenExistingNamesResolver
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function resolveOveriddenNamesForNew(ClassMethod $classMethod): array
|
||||
private function resolveOveriddenNamesForNew(FunctionLike $functionLike): array
|
||||
{
|
||||
$classMethodHash = spl_object_hash($classMethod);
|
||||
$classMethodHash = spl_object_hash($functionLike);
|
||||
|
||||
if (isset($this->overridenExistingVariableNamesByClassMethod[$classMethodHash])) {
|
||||
return $this->overridenExistingVariableNamesByClassMethod[$classMethodHash];
|
||||
@ -84,7 +85,7 @@ final class OverridenExistingNamesResolver
|
||||
$currentlyUsedNames = [];
|
||||
|
||||
/** @var Assign[] $assigns */
|
||||
$assigns = $this->betterNodeFinder->findInstanceOf((array) $classMethod->stmts, Assign::class);
|
||||
$assigns = $this->betterNodeFinder->findInstanceOf((array) $functionLike->stmts, Assign::class);
|
||||
|
||||
foreach ($assigns as $assign) {
|
||||
/** @var Variable $assignVariable */
|
||||
|
@ -20,6 +20,7 @@ use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\Naming\Guard\BreakingVariableRenameGuard;
|
||||
use Rector\Naming\Naming\ExpectedNameResolver;
|
||||
use Rector\Naming\VariableRenamer;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
@ -39,10 +40,19 @@ final class RenameVariableToMatchGetMethodNameRector extends AbstractRector
|
||||
*/
|
||||
private $variableRenamer;
|
||||
|
||||
public function __construct(ExpectedNameResolver $expectedNameResolver, VariableRenamer $variableRenamer)
|
||||
{
|
||||
/**
|
||||
* @var BreakingVariableRenameGuard
|
||||
*/
|
||||
private $breakingVariableRenameGuard;
|
||||
|
||||
public function __construct(
|
||||
ExpectedNameResolver $expectedNameResolver,
|
||||
VariableRenamer $variableRenamer,
|
||||
BreakingVariableRenameGuard $breakingVariableRenameGuard
|
||||
) {
|
||||
$this->expectedNameResolver = $expectedNameResolver;
|
||||
$this->variableRenamer = $variableRenamer;
|
||||
$this->breakingVariableRenameGuard = $breakingVariableRenameGuard;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
@ -83,9 +93,7 @@ PHP
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (
|
||||
$node->expr instanceof ArrowFunction || ! $node->var instanceof Variable
|
||||
) {
|
||||
if ($node->expr instanceof ArrowFunction || ! $node->var instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -94,16 +102,33 @@ PHP
|
||||
return null;
|
||||
}
|
||||
|
||||
$currentName = $this->getName($node->var);
|
||||
if ($currentName === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$classMethodOrFunction = $this->getCurrentFunctionLike($node);
|
||||
if ($classMethodOrFunction === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->breakingVariableRenameGuard->shouldSkipVariable(
|
||||
$currentName,
|
||||
$newName,
|
||||
$classMethodOrFunction,
|
||||
$node->var
|
||||
)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->shouldSkipForNameConflict($node, $newName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->renameVariable($node, $newName);
|
||||
|
||||
return $node;
|
||||
return $this->renameVariable($node, $newName);
|
||||
}
|
||||
|
||||
private function renameVariable(Assign $assign, string $newName): void
|
||||
private function renameVariable(Assign $assign, string $newName): Assign
|
||||
{
|
||||
/** @var Variable $variableNode */
|
||||
$variableNode = $assign->var;
|
||||
@ -115,7 +140,7 @@ PHP
|
||||
|
||||
$functionLike = $this->getCurrentFunctionLike($assign);
|
||||
if ($functionLike === null) {
|
||||
return;
|
||||
return $assign;
|
||||
}
|
||||
|
||||
$this->variableRenamer->renameVariableInClassMethodOrFunction(
|
||||
@ -124,6 +149,8 @@ PHP
|
||||
$originalName,
|
||||
$newName
|
||||
);
|
||||
|
||||
return $assign;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user