rector/rules/naming/src/Rector/Assign/RenameVariableToMatchGetMethodNameRector.php

229 lines
6.4 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Rector\Naming\Rector\Assign;
2020-07-19 13:39:13 +00:00
use Nette\Utils\Strings;
use PhpParser\Node;
2020-07-19 13:39:13 +00:00
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
2020-07-19 12:30:07 +00:00
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
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 PHPStan\Type\TypeWithClassName;
2020-07-19 09:53:35 +00:00
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer;
use Rector\Naming\Guard\BreakingVariableRenameGuard;
use Rector\Naming\Naming\ExpectedNameResolver;
use Rector\Naming\VariableRenamer;
2020-07-19 09:53:35 +00:00
use Rector\NodeTypeResolver\Node\AttributeKey;
/**
* @see \Rector\Naming\Tests\Rector\Assign\RenameVariableToMatchGetMethodNameRector\RenameVariableToMatchGetMethodNameRectorTest
*/
final class RenameVariableToMatchGetMethodNameRector extends AbstractRector
{
/**
* @var ExpectedNameResolver
*/
private $expectedNameResolver;
/**
* @var VariableRenamer
*/
private $variableRenamer;
/**
* @var BreakingVariableRenameGuard
*/
private $breakingVariableRenameGuard;
/**
* @var FamilyRelationsAnalyzer
*/
private $familyRelationsAnalyzer;
public function __construct(
ExpectedNameResolver $expectedNameResolver,
VariableRenamer $variableRenamer,
BreakingVariableRenameGuard $breakingVariableRenameGuard,
FamilyRelationsAnalyzer $familyRelationsAnalyzer
) {
$this->expectedNameResolver = $expectedNameResolver;
$this->variableRenamer = $variableRenamer;
$this->breakingVariableRenameGuard = $breakingVariableRenameGuard;
$this->familyRelationsAnalyzer = $familyRelationsAnalyzer;
}
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
{
// @todo possible matched decouple value object to prevent so many if/elses
2020-07-19 12:30:07 +00:00
if (! $node->expr instanceof MethodCall && ! $node->expr instanceof StaticCall && ! $node->expr instanceof FuncCall) {
return null;
}
if (! $node->var instanceof Variable) {
return null;
}
$newName = $this->expectedNameResolver->resolveForGetCallExpr($node->expr);
if ($newName === null || $this->isName($node, $newName)) {
return null;
}
$currentName = $this->getName($node->var);
if ($currentName === null) {
return null;
}
if ($this->shouldSkipForNamingConvention($node->expr, $currentName, $newName)) {
2020-07-19 12:30:07 +00:00
return null;
}
$callStaticType = $this->getStaticType($node->expr);
if ($callStaticType instanceof TypeWithClassName) {
if ($this->familyRelationsAnalyzer->isParentClass($callStaticType->getClassName())) {
return null;
}
}
$functionLike = $this->getCurrentFunctionLike($node);
if ($functionLike === null) {
return null;
}
if ($this->breakingVariableRenameGuard->shouldSkipVariable(
$currentName,
$newName,
$functionLike,
$node->var
)) {
return null;
}
return $this->renameVariable($node, $newName);
}
private function renameVariable(Assign $assign, string $newName): Assign
{
/** @var Variable $variableNode */
2020-07-19 09:53:35 +00:00
$variableNode = $assign->var;
/** @var string $originalName */
$originalName = $variableNode->name;
$this->renameInDocComment($assign, $originalName, $newName);
$functionLike = $this->getCurrentFunctionLike($assign);
if ($functionLike === null) {
return $assign;
}
$this->variableRenamer->renameVariableInFunctionLike($functionLike, $assign, $originalName, $newName);
return $assign;
}
/**
* @return ClassMethod|Function_|Closure|null
*/
private function getCurrentFunctionLike(Node $node): ?FunctionLike
{
return $node->getAttribute(AttributeKey::CLOSURE_NODE) ??
$node->getAttribute(AttributeKey::METHOD_NODE) ??
$node->getAttribute(AttributeKey::FUNCTION_NODE);
}
/**
* @note variable rename is correct, but node printer doesn't see it as a changed text for some reason
*/
private function renameInDocComment(Node $node, string $originalName, string $newName): void
{
2020-07-19 09:53:35 +00:00
/** @var PhpDocInfo|null $phpDocInfo */
$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-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-19 13:39:13 +00:00
/**
* Keep cases like:
*
* $someNameSuffix = $this->getSomeName();
* $prefixSomeName = $this->getSomeName();
* $someName = $this->getSomeName();
*
* @param FuncCall|StaticCall|MethodCall $expr
*/
private function shouldSkipForNamingConvention(Expr $expr, string $currentName, string $expectedName): bool
{
// skip "$call = $method->call();" based conventions
$callName = $this->getName($expr->name);
if ($currentName === $callName) {
return true;
}
// starts with or ends with
return (bool) Strings::match($currentName, '#^(' . $expectedName . '|' . $expectedName . '$)#i');
}
}