2020-07-13 21:13:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Naming\Rector\Assign;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2020-07-19 13:39:13 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2020-07-13 21:13:40 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
2020-07-19 12:30:07 +00:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
2020-07-19 18:19:25 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassLike;
|
2020-07-19 16:02:11 +00:00
|
|
|
use PHPStan\Type\TypeWithClassName;
|
2020-07-13 21:13:40 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-07-19 16:02:11 +00:00
|
|
|
use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer;
|
2020-07-19 10:25:24 +00:00
|
|
|
use Rector\Naming\Guard\BreakingVariableRenameGuard;
|
2020-07-19 16:11:59 +00:00
|
|
|
use Rector\Naming\Matcher\VariableAndCallAssignMatcher;
|
2020-07-13 21:13:40 +00:00
|
|
|
use Rector\Naming\Naming\ExpectedNameResolver;
|
2020-07-19 17:24:46 +00:00
|
|
|
use Rector\Naming\NamingConvention\NamingConventionAnalyzer;
|
2020-07-19 17:42:16 +00:00
|
|
|
use Rector\Naming\PhpDoc\VarTagValueNodeRenamer;
|
|
|
|
use Rector\Naming\ValueObject\VariableAndCallAssign;
|
2020-07-19 10:16:20 +00:00
|
|
|
use Rector\Naming\VariableRenamer;
|
2020-12-18 22:18:17 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-07-13 21:13:40 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-30 19:01:33 +00:00
|
|
|
* @see \Rector\Naming\Tests\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector\RenameVariableToMatchMethodCallReturnTypeRectorTest
|
2020-07-13 21:13:40 +00:00
|
|
|
*/
|
2020-08-30 19:01:33 +00:00
|
|
|
final class RenameVariableToMatchMethodCallReturnTypeRector extends AbstractRector
|
2020-07-13 21:13:40 +00:00
|
|
|
{
|
2020-07-19 18:19:25 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private const ALLOWED_PARENT_TYPES = [ClassLike::class];
|
|
|
|
|
2020-07-13 21:13:40 +00:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
|
2020-07-19 16:02:11 +00:00
|
|
|
/**
|
|
|
|
* @var FamilyRelationsAnalyzer
|
|
|
|
*/
|
|
|
|
private $familyRelationsAnalyzer;
|
|
|
|
|
2020-07-19 16:11:59 +00:00
|
|
|
/**
|
|
|
|
* @var VariableAndCallAssignMatcher
|
|
|
|
*/
|
|
|
|
private $variableAndCallAssignMatcher;
|
|
|
|
|
2020-07-19 17:24:46 +00:00
|
|
|
/**
|
|
|
|
* @var NamingConventionAnalyzer
|
|
|
|
*/
|
|
|
|
private $namingConventionAnalyzer;
|
|
|
|
|
2020-07-19 17:42:16 +00:00
|
|
|
/**
|
|
|
|
* @var VarTagValueNodeRenamer
|
|
|
|
*/
|
|
|
|
private $varTagValueNodeRenamer;
|
|
|
|
|
2020-07-19 10:25:24 +00:00
|
|
|
public function __construct(
|
2020-07-19 16:02:11 +00:00
|
|
|
BreakingVariableRenameGuard $breakingVariableRenameGuard,
|
2020-07-26 07:49:22 +00:00
|
|
|
ExpectedNameResolver $expectedNameResolver,
|
2020-07-19 16:11:59 +00:00
|
|
|
FamilyRelationsAnalyzer $familyRelationsAnalyzer,
|
2020-07-19 17:42:16 +00:00
|
|
|
NamingConventionAnalyzer $namingConventionAnalyzer,
|
2020-07-19 18:19:25 +00:00
|
|
|
VarTagValueNodeRenamer $varTagValueNodeRenamer,
|
2020-07-26 07:49:22 +00:00
|
|
|
VariableAndCallAssignMatcher $variableAndCallAssignMatcher,
|
|
|
|
VariableRenamer $variableRenamer
|
2020-07-19 10:25:24 +00:00
|
|
|
) {
|
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-19 16:02:11 +00:00
|
|
|
$this->familyRelationsAnalyzer = $familyRelationsAnalyzer;
|
2020-07-19 16:11:59 +00:00
|
|
|
$this->variableAndCallAssignMatcher = $variableAndCallAssignMatcher;
|
2020-07-19 17:24:46 +00:00
|
|
|
$this->namingConventionAnalyzer = $namingConventionAnalyzer;
|
2020-07-19 17:42:16 +00:00
|
|
|
$this->varTagValueNodeRenamer = $varTagValueNodeRenamer;
|
2020-07-13 21:13:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2020-07-13 21:13:40 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
|
|
|
'Rename variable to match method return type',
|
|
|
|
[
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
2020-07-19 11:52:01 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
2020-07-13 21:13:40 +00:00
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$a = $this->getRunner();
|
|
|
|
}
|
2020-08-30 19:01:33 +00:00
|
|
|
|
|
|
|
public function getRunner(): Runner
|
|
|
|
{
|
|
|
|
return new Runner();
|
|
|
|
}
|
2020-07-13 21:13:40 +00:00
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-07-13 21:13:40 +00:00
|
|
|
,
|
2020-11-16 17:50:38 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2020-07-19 11:52:01 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
2020-07-13 21:13:40 +00:00
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$runner = $this->getRunner();
|
|
|
|
}
|
2020-08-30 19:01:33 +00:00
|
|
|
|
|
|
|
public function getRunner(): Runner
|
|
|
|
{
|
|
|
|
return new Runner();
|
|
|
|
}
|
2020-07-13 21:13:40 +00:00
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-11-16 17:50:38 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
]);
|
2020-07-13 21:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [Assign::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Assign $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2020-09-09 08:52:43 +00:00
|
|
|
/** @var VariableAndCallAssign|null $variableAndCallAssign */
|
2020-07-19 16:11:59 +00:00
|
|
|
$variableAndCallAssign = $this->variableAndCallAssignMatcher->match($node);
|
|
|
|
if ($variableAndCallAssign === null) {
|
2020-07-13 21:13:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-18 22:18:17 +00:00
|
|
|
$call = $variableAndCallAssign->getCall();
|
|
|
|
if ($this->isMultipleCall($call)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$expectedName = $this->expectedNameResolver->resolveForCall($call);
|
2020-07-27 09:26:41 +00:00
|
|
|
if ($expectedName === null || $this->isName($node->var, $expectedName)) {
|
2020-07-13 21:13:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-19 17:42:16 +00:00
|
|
|
if ($this->shouldSkip($variableAndCallAssign, $expectedName)) {
|
2020-07-19 10:25:24 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-19 18:19:25 +00:00
|
|
|
$this->renameVariable($variableAndCallAssign, $expectedName);
|
|
|
|
|
|
|
|
return $node;
|
2020-07-13 21:13:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 22:18:17 +00:00
|
|
|
/**
|
|
|
|
* @param FuncCall|StaticCall|MethodCall $node
|
|
|
|
*/
|
|
|
|
private function isMultipleCall(Node $node): bool
|
|
|
|
{
|
|
|
|
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
|
|
|
while ($parentNode) {
|
|
|
|
$countUsed = count($this->betterNodeFinder->find($parentNode, function (Node $n) use ($node): bool {
|
|
|
|
if (get_class($node) !== get_class($n)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-21 02:12:42 +00:00
|
|
|
/** @var FuncCall|StaticCall|MethodCall $n */
|
2020-12-18 22:18:17 +00:00
|
|
|
$passedNode = clone $n;
|
2020-12-21 02:12:42 +00:00
|
|
|
|
|
|
|
/** @var FuncCall|StaticCall|MethodCall $node */
|
2020-12-18 22:18:17 +00:00
|
|
|
$usedNode = clone $node;
|
2020-12-21 02:12:42 +00:00
|
|
|
|
|
|
|
/** @var FuncCall|StaticCall|MethodCall $passedNode */
|
2020-12-18 22:18:17 +00:00
|
|
|
$passedNode->args = [];
|
2020-12-21 02:12:42 +00:00
|
|
|
|
|
|
|
/** @var FuncCall|StaticCall|MethodCall $usedNode */
|
2020-12-18 22:18:17 +00:00
|
|
|
$usedNode->args = [];
|
|
|
|
|
|
|
|
return $this->areNodesEqual($passedNode, $usedNode);
|
|
|
|
}));
|
|
|
|
|
|
|
|
if ($countUsed > 1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-05 20:45:36 +00:00
|
|
|
private function shouldSkip(VariableAndCallAssign $variableAndCallAssign, string $expectedName): bool
|
|
|
|
{
|
|
|
|
if ($this->namingConventionAnalyzer->isCallMatchingVariableName(
|
|
|
|
$variableAndCallAssign->getCall(),
|
|
|
|
$variableAndCallAssign->getVariableName(),
|
|
|
|
$expectedName
|
|
|
|
)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isClassTypeWithChildren($variableAndCallAssign->getCall())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->breakingVariableRenameGuard->shouldSkipVariable(
|
|
|
|
$variableAndCallAssign->getVariableName(),
|
|
|
|
$expectedName,
|
|
|
|
$variableAndCallAssign->getFunctionLike(),
|
|
|
|
$variableAndCallAssign->getVariable()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-19 18:19:25 +00:00
|
|
|
private function renameVariable(VariableAndCallAssign $variableAndCallAssign, string $expectedName): void
|
2020-07-13 21:13:40 +00:00
|
|
|
{
|
2020-07-19 17:42:16 +00:00
|
|
|
$this->varTagValueNodeRenamer->renameAssignVarTagVariableName(
|
|
|
|
$variableAndCallAssign->getAssign(),
|
|
|
|
$variableAndCallAssign->getVariableName(),
|
2020-07-19 18:19:25 +00:00
|
|
|
$expectedName
|
2020-07-19 17:42:16 +00:00
|
|
|
);
|
2020-07-19 09:53:35 +00:00
|
|
|
|
2020-07-19 17:42:16 +00:00
|
|
|
$this->variableRenamer->renameVariableInFunctionLike(
|
|
|
|
$variableAndCallAssign->getFunctionLike(),
|
|
|
|
$variableAndCallAssign->getAssign(),
|
|
|
|
$variableAndCallAssign->getVariableName(),
|
2020-07-19 18:19:25 +00:00
|
|
|
$expectedName
|
2020-07-19 17:42:16 +00:00
|
|
|
);
|
2020-07-13 21:13:40 +00:00
|
|
|
}
|
2020-07-19 13:39:13 +00:00
|
|
|
|
|
|
|
/**
|
2020-07-19 17:24:46 +00:00
|
|
|
* @param StaticCall|MethodCall|FuncCall $expr
|
2020-07-19 13:39:13 +00:00
|
|
|
*/
|
2020-07-19 17:24:46 +00:00
|
|
|
private function isClassTypeWithChildren(Expr $expr): bool
|
2020-07-19 13:39:13 +00:00
|
|
|
{
|
2020-07-19 17:24:46 +00:00
|
|
|
$callStaticType = $this->getStaticType($expr);
|
2020-12-06 00:27:00 +00:00
|
|
|
$callStaticType = $this->typeUnwrapper->unwrapNullableType($callStaticType);
|
2020-07-19 18:19:25 +00:00
|
|
|
|
2020-07-19 17:24:46 +00:00
|
|
|
if (! $callStaticType instanceof TypeWithClassName) {
|
|
|
|
return false;
|
2020-07-19 13:39:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 18:19:25 +00:00
|
|
|
if (in_array($callStaticType->getClassName(), self::ALLOWED_PARENT_TYPES, true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-19 17:24:46 +00:00
|
|
|
return $this->familyRelationsAnalyzer->isParentClass($callStaticType->getClassName());
|
2020-07-19 13:39:13 +00:00
|
|
|
}
|
2020-07-13 21:13:40 +00:00
|
|
|
}
|