2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2018-10-25 21:59:19 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\Return_;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2020-12-24 16:28:56 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2018-10-25 21:59:19 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\AssignOp;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
2021-05-12 13:15:45 +00:00
|
|
|
use PhpParser\Node\FunctionLike;
|
2018-10-25 21:59:19 +00:00
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2020-09-01 17:56:30 +00:00
|
|
|
use PHPStan\Type\MixedType;
|
2021-10-26 09:28:24 +00:00
|
|
|
use Rector\Core\NodeAnalyzer\CallAnalyzer;
|
2021-08-09 13:33:35 +00:00
|
|
|
use Rector\Core\NodeAnalyzer\VariableAnalyzer;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\PhpParser\Node\AssignAndBinaryMap;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2019-04-13 09:20:27 +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;
|
2018-10-25 21:59:19 +00:00
|
|
|
/**
|
|
|
|
* @see Based on https://github.com/slevomat/coding-standard/blob/master/SlevomatCodingStandard/Sniffs/Variables/UselessVariableSniff.php
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\Return_\SimplifyUselessVariableRector\SimplifyUselessVariableRectorTest
|
2018-10-25 21:59:19 +00:00
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
final class SimplifyUselessVariableRector extends \Rector\Core\Rector\AbstractRector
|
2018-10-25 21:59:19 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\AssignAndBinaryMap
|
2018-10-25 21:59:19 +00:00
|
|
|
*/
|
2018-10-27 14:02:49 +00:00
|
|
|
private $assignAndBinaryMap;
|
2021-08-09 13:33:35 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-08-09 13:33:35 +00:00
|
|
|
* @var \Rector\Core\NodeAnalyzer\VariableAnalyzer
|
|
|
|
*/
|
|
|
|
private $variableAnalyzer;
|
2021-10-26 09:28:24 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-10-26 09:28:24 +00:00
|
|
|
* @var \Rector\Core\NodeAnalyzer\CallAnalyzer
|
|
|
|
*/
|
|
|
|
private $callAnalyzer;
|
|
|
|
public function __construct(\Rector\Core\PhpParser\Node\AssignAndBinaryMap $assignAndBinaryMap, \Rector\Core\NodeAnalyzer\VariableAnalyzer $variableAnalyzer, \Rector\Core\NodeAnalyzer\CallAnalyzer $callAnalyzer)
|
2018-10-25 21:59:19 +00:00
|
|
|
{
|
2018-10-27 14:02:49 +00:00
|
|
|
$this->assignAndBinaryMap = $assignAndBinaryMap;
|
2021-08-09 13:33:35 +00:00
|
|
|
$this->variableAnalyzer = $variableAnalyzer;
|
2021-10-26 09:28:24 +00:00
|
|
|
$this->callAnalyzer = $callAnalyzer;
|
2018-10-25 21:59:19 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
2018-10-25 21:59:19 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Removes useless variable assigns', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
2018-10-25 21:59:19 +00:00
|
|
|
function () {
|
|
|
|
$a = true;
|
|
|
|
return $a;
|
|
|
|
};
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-10-25 21:59:19 +00:00
|
|
|
function () {
|
|
|
|
return true;
|
|
|
|
};
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-10-25 21:59:19 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-25 21:59:19 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-10-25 21:59:19 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return [\PhpParser\Node\Stmt\Return_::class];
|
2018-10-25 21:59:19 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Return_ $node
|
2018-10-25 21:59:19 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
2018-10-25 21:59:19 +00:00
|
|
|
{
|
|
|
|
if ($this->shouldSkip($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-10-26 09:28:24 +00:00
|
|
|
/** @var Expression $previousExpression */
|
|
|
|
$previousExpression = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PREVIOUS_NODE);
|
|
|
|
/** @var Assign|AssignOp $previousNode */
|
|
|
|
$previousNode = $previousExpression->expr;
|
2018-10-25 21:59:19 +00:00
|
|
|
$previousVariableNode = $previousNode->var;
|
2020-12-24 16:28:56 +00:00
|
|
|
if ($this->hasSomeComment($previousVariableNode)) {
|
2018-10-25 21:59:19 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($previousNode instanceof \PhpParser\Node\Expr\Assign) {
|
2020-09-01 17:56:30 +00:00
|
|
|
if ($this->isReturnWithVarAnnotation($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-10-25 21:59:19 +00:00
|
|
|
$node->expr = $previousNode->expr;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($previousNode instanceof \PhpParser\Node\Expr\AssignOp) {
|
2018-10-27 14:02:49 +00:00
|
|
|
$binaryClass = $this->assignAndBinaryMap->getAlternative($previousNode);
|
2018-11-07 19:39:33 +00:00
|
|
|
if ($binaryClass === null) {
|
2018-10-25 21:59:19 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$node->expr = new $binaryClass($previousNode->var, $previousNode->expr);
|
|
|
|
}
|
|
|
|
$this->removeNode($previousNode);
|
|
|
|
return $node;
|
|
|
|
}
|
2021-07-25 16:57:05 +00:00
|
|
|
private function hasByRefReturn(\PhpParser\Node\Stmt\Return_ $return) : bool
|
2021-05-12 13:15:45 +00:00
|
|
|
{
|
|
|
|
$node = $return;
|
|
|
|
while ($node = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE)) {
|
|
|
|
if ($node instanceof \PhpParser\Node\FunctionLike) {
|
|
|
|
return $node->returnsByRef();
|
|
|
|
}
|
2022-02-05 11:57:25 +00:00
|
|
|
if (!$node instanceof \PhpParser\Node) {
|
|
|
|
break;
|
|
|
|
}
|
2021-05-12 13:15:45 +00:00
|
|
|
}
|
|
|
|
return \false;
|
|
|
|
}
|
2021-07-05 22:50:18 +00:00
|
|
|
private function shouldSkip(\PhpParser\Node\Stmt\Return_ $return) : bool
|
2018-10-25 21:59:19 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$return->expr instanceof \PhpParser\Node\Expr\Variable) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2018-10-25 21:59:19 +00:00
|
|
|
}
|
2021-07-25 16:57:05 +00:00
|
|
|
if ($this->hasByRefReturn($return)) {
|
2021-05-12 13:15:45 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2021-08-09 13:33:35 +00:00
|
|
|
/** @var Variable $variableNode */
|
2020-06-29 21:19:37 +00:00
|
|
|
$variableNode = $return->expr;
|
2021-05-10 22:23:08 +00:00
|
|
|
$previousExpression = $return->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PREVIOUS_NODE);
|
|
|
|
if (!$previousExpression instanceof \PhpParser\Node\Stmt\Expression) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2018-10-25 21:59:19 +00:00
|
|
|
}
|
|
|
|
// is variable part of single assign
|
|
|
|
$previousNode = $previousExpression->expr;
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$previousNode instanceof \PhpParser\Node\Expr\AssignOp && !$previousNode instanceof \PhpParser\Node\Expr\Assign) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2018-10-25 21:59:19 +00:00
|
|
|
}
|
|
|
|
// is the same variable
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($previousNode->var, $variableNode)) {
|
|
|
|
return \true;
|
2018-12-13 21:07:27 +00:00
|
|
|
}
|
2021-08-09 13:33:35 +00:00
|
|
|
if ($this->isPreviousExpressionVisuallySimilar($previousExpression, $previousNode)) {
|
|
|
|
return \true;
|
|
|
|
}
|
2021-10-26 09:28:24 +00:00
|
|
|
if ($this->variableAnalyzer->isStaticOrGlobal($variableNode)) {
|
|
|
|
return \true;
|
|
|
|
}
|
2022-02-25 08:28:29 +00:00
|
|
|
if ($this->callAnalyzer->isNewInstance($previousNode->var)) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
return $this->variableAnalyzer->isUsedByReference($variableNode);
|
2018-12-13 21:07:27 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function hasSomeComment(\PhpParser\Node\Expr $expr) : bool
|
2020-12-24 16:28:56 +00:00
|
|
|
{
|
|
|
|
if ($expr->getComments() !== []) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-12-24 16:28:56 +00:00
|
|
|
}
|
|
|
|
return $expr->getDocComment() !== null;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function isReturnWithVarAnnotation(\PhpParser\Node\Stmt\Return_ $return) : bool
|
2020-09-01 17:56:30 +00:00
|
|
|
{
|
2021-01-18 21:45:13 +00:00
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($return);
|
2021-05-10 22:23:08 +00:00
|
|
|
return !$phpDocInfo->getVarType() instanceof \PHPStan\Type\MixedType;
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|
2018-12-13 21:07:27 +00:00
|
|
|
/**
|
2021-10-30 14:18:31 +00:00
|
|
|
* @param \PhpParser\Node\Expr\Assign|\PhpParser\Node\Expr\AssignOp $previousNode
|
2018-12-13 21:07:27 +00:00
|
|
|
*/
|
2021-06-29 14:24:45 +00:00
|
|
|
private function isPreviousExpressionVisuallySimilar(\PhpParser\Node\Stmt\Expression $previousExpression, $previousNode) : bool
|
2018-12-13 21:07:27 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$prePreviousExpression = $previousExpression->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PREVIOUS_STATEMENT);
|
|
|
|
if (!$prePreviousExpression instanceof \PhpParser\Node\Stmt\Expression) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-12-19 15:24:53 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$prePreviousExpression->expr instanceof \PhpParser\Node\Expr\AssignOp) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-12-19 15:24:53 +00:00
|
|
|
}
|
2021-02-19 12:01:23 +00:00
|
|
|
return $this->nodeComparator->areNodesEqual($prePreviousExpression->expr->var, $previousNode->var);
|
2018-10-25 21:59:19 +00:00
|
|
|
}
|
|
|
|
}
|