rector/rules/Naming/Rector/ClassMethod/RenameVariableToMatchNewTypeRector.php

132 lines
4.1 KiB
PHP
Raw Normal View History

<?php
declare (strict_types=1);
namespace Rector\Naming\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Naming\Guard\BreakingVariableRenameGuard;
use Rector\Naming\Naming\ExpectedNameResolver;
use Rector\Naming\VariableRenamer;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Naming\Rector\ClassMethod\RenameVariableToMatchNewTypeRector\RenameVariableToMatchNewTypeRectorTest
*/
final class RenameVariableToMatchNewTypeRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\Naming\Guard\BreakingVariableRenameGuard
*/
private $breakingVariableRenameGuard;
/**
* @readonly
* @var \Rector\Naming\Naming\ExpectedNameResolver
*/
private $expectedNameResolver;
2020-07-19 10:07:45 +00:00
/**
* @readonly
* @var \Rector\Naming\VariableRenamer
2020-07-19 10:07:45 +00:00
*/
private $variableRenamer;
/**
* @readonly
* @var \Rector\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
public function __construct(BreakingVariableRenameGuard $breakingVariableRenameGuard, ExpectedNameResolver $expectedNameResolver, VariableRenamer $variableRenamer, BetterNodeFinder $betterNodeFinder)
{
$this->breakingVariableRenameGuard = $breakingVariableRenameGuard;
$this->expectedNameResolver = $expectedNameResolver;
2020-07-19 10:07:45 +00:00
$this->variableRenamer = $variableRenamer;
$this->betterNodeFinder = $betterNodeFinder;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Rename variable to match new ClassType', [new CodeSample(<<<'CODE_SAMPLE'
final class SomeClass
{
public function run()
{
$search = new DreamSearch();
$search->advance();
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeClass
{
public function run()
{
$dreamSearch = new DreamSearch();
$dreamSearch->advance();
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node) : ?Node
{
$hasChanged = \false;
$assignsOfNew = $this->getAssignsOfNew($node);
[Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set (#5696) * [Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set * fix property fetch not from this * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fix * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fixture fix * phpstan * [ci-review] Rector Rectify * phpstan * extract to separate method for collect assigns by name * adding InflectorSingularResolver service * skip single prefix and length >= 40 * add failing fixture to skip plural camel case * use regex to get camel cases * implemented singularize camel cased * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * handle singular verb news -> new * [ci-review] Rector Rectify * fixture fix * handle has * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * phpstan * handle left side By * [ci-review] Rector Rectify * re-use resolve call * [ci-review] Rector Rectify * phpstan * [ci-review] Rector Rectify * final touch * final touch * [ci-review] Rector Rectify * [ci-review] Rector Rectify * use previous By in the middle * update $childClassReflection->hasProperty($propertyName) * [ci-review] Rector Rectify * catchKeys * regex fix * fixture * [ci-review] Rector Rectify * try use check array * Revert "try use check array" This reverts commit adb9f767f20ea2544e5ccfc9cfe361ecc929912a. * use files Co-authored-by: kaizen-ci <info@kaizen-ci.org>
2021-03-05 10:55:40 +00:00
foreach ($assignsOfNew as $assignOfNew) {
$expectedName = $this->expectedNameResolver->resolveForAssignNew($assignOfNew);
// skip self name as not useful
if ($expectedName === 'self') {
continue;
}
/** @var Variable $variable */
[Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set (#5696) * [Naming] Register RenameForeachValueVariableToMatchExprVariableRector to naming config set * fix property fetch not from this * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fix * [ci-review] Rector Rectify * [ci-review] Rector Rectify * fixture fix * phpstan * [ci-review] Rector Rectify * phpstan * extract to separate method for collect assigns by name * adding InflectorSingularResolver service * skip single prefix and length >= 40 * add failing fixture to skip plural camel case * use regex to get camel cases * implemented singularize camel cased * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * handle singular verb news -> new * [ci-review] Rector Rectify * fixture fix * handle has * [ci-review] Rector Rectify * [ci-review] Rector Rectify * [ci-review] Rector Rectify * phpstan * phpstan * handle left side By * [ci-review] Rector Rectify * re-use resolve call * [ci-review] Rector Rectify * phpstan * [ci-review] Rector Rectify * final touch * final touch * [ci-review] Rector Rectify * [ci-review] Rector Rectify * use previous By in the middle * update $childClassReflection->hasProperty($propertyName) * [ci-review] Rector Rectify * catchKeys * regex fix * fixture * [ci-review] Rector Rectify * try use check array * Revert "try use check array" This reverts commit adb9f767f20ea2544e5ccfc9cfe361ecc929912a. * use files Co-authored-by: kaizen-ci <info@kaizen-ci.org>
2021-03-05 10:55:40 +00:00
$variable = $assignOfNew->var;
if ($expectedName === null) {
continue;
}
if ($this->isName($variable, $expectedName)) {
continue;
}
$currentName = $this->getName($variable);
if ($currentName === null) {
continue;
}
2020-06-29 21:19:37 +00:00
if ($this->breakingVariableRenameGuard->shouldSkipVariable($currentName, $expectedName, $node, $variable)) {
continue;
}
$hasChanged = \true;
// 1. rename assigned variable
$assignOfNew->var = new Variable($expectedName);
// 2. rename variable in the
$this->variableRenamer->renameVariableInFunctionLike($node, $currentName, $expectedName, $assignOfNew);
}
if (!$hasChanged) {
return null;
}
return $node;
}
/**
* @return Assign[]
*/
private function getAssignsOfNew(ClassMethod $classMethod) : array
{
/** @var Assign[] $assigns */
$assigns = $this->betterNodeFinder->findInstanceOf((array) $classMethod->stmts, Assign::class);
return \array_filter($assigns, static function (Assign $assign) : bool {
return $assign->expr instanceof New_;
});
}
}