2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-04-10 18:18:46 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\FunctionLike;
|
2018-10-25 21:59:19 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
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;
|
2022-04-10 18:18:46 +00:00
|
|
|
use PhpParser\Node\Stmt;
|
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;
|
2022-04-10 18:18:46 +00:00
|
|
|
use Rector\CodeQuality\NodeAnalyzer\ReturnAnalyzer;
|
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;
|
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
|
2022-04-10 18:18:46 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\FunctionLike\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;
|
2022-04-10 18:18:46 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\CodeQuality\NodeAnalyzer\ReturnAnalyzer
|
|
|
|
*/
|
|
|
|
private $returnAnalyzer;
|
|
|
|
public function __construct(\Rector\Core\PhpParser\Node\AssignAndBinaryMap $assignAndBinaryMap, \Rector\Core\NodeAnalyzer\VariableAnalyzer $variableAnalyzer, \Rector\Core\NodeAnalyzer\CallAnalyzer $callAnalyzer, \Rector\CodeQuality\NodeAnalyzer\ReturnAnalyzer $returnAnalyzer)
|
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;
|
2022-04-10 18:18:46 +00:00
|
|
|
$this->returnAnalyzer = $returnAnalyzer;
|
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
|
|
|
{
|
2022-04-10 18:18:46 +00:00
|
|
|
return [\PhpParser\Node\FunctionLike::class];
|
2018-10-25 21:59:19 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-04-10 18:18:46 +00:00
|
|
|
* @param FunctionLike $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
|
|
|
{
|
2022-04-10 18:18:46 +00:00
|
|
|
$stmts = $node->getStmts();
|
|
|
|
if ($stmts === null) {
|
2018-10-25 21:59:19 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-04-10 18:18:46 +00:00
|
|
|
$previousStmt = null;
|
|
|
|
$hasChanged = \false;
|
|
|
|
foreach ($stmts as $stmt) {
|
|
|
|
if ($previousStmt === null || !$stmt instanceof \PhpParser\Node\Stmt\Return_) {
|
|
|
|
$previousStmt = $stmt;
|
|
|
|
continue;
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|
2022-04-10 18:18:46 +00:00
|
|
|
if ($this->shouldSkipStmt($stmt, $previousStmt)) {
|
|
|
|
$previousStmt = $stmt;
|
|
|
|
continue;
|
2018-10-25 21:59:19 +00:00
|
|
|
}
|
2022-04-10 18:18:46 +00:00
|
|
|
/** @var Expression $previousStmt */
|
|
|
|
/** @var Assign|AssignOp $previousNode */
|
|
|
|
$previousNode = $previousStmt->expr;
|
|
|
|
/** @var Return_ $stmt */
|
|
|
|
if ($previousNode instanceof \PhpParser\Node\Expr\Assign) {
|
|
|
|
if ($this->isReturnWithVarAnnotation($stmt)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$stmt->expr = $previousNode->expr;
|
|
|
|
} else {
|
|
|
|
$binaryClass = $this->assignAndBinaryMap->getAlternative($previousNode);
|
|
|
|
if ($binaryClass === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$stmt->expr = new $binaryClass($previousNode->var, $previousNode->expr);
|
2022-02-05 11:57:25 +00:00
|
|
|
}
|
2022-04-10 18:18:46 +00:00
|
|
|
$this->removeNode($previousStmt);
|
|
|
|
$hasChanged = \true;
|
|
|
|
$previousStmt = $stmt;
|
2021-05-12 13:15:45 +00:00
|
|
|
}
|
2022-04-10 18:18:46 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
return null;
|
2021-05-12 13:15:45 +00:00
|
|
|
}
|
2022-04-10 18:18:46 +00:00
|
|
|
private function shouldSkipStmt(\PhpParser\Node\Stmt\Return_ $return, \PhpParser\Node\Stmt $previousStmt) : bool
|
2018-10-25 21:59:19 +00:00
|
|
|
{
|
2022-04-10 18:18:46 +00:00
|
|
|
if ($this->hasSomeComment($previousStmt)) {
|
|
|
|
return \true;
|
|
|
|
}
|
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
|
|
|
}
|
2022-04-10 18:18:46 +00:00
|
|
|
if ($this->returnAnalyzer->hasByRefReturn($return)) {
|
2021-05-12 13:15:45 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2022-04-10 18:18:46 +00:00
|
|
|
/** @var Variable $variable */
|
|
|
|
$variable = $return->expr;
|
|
|
|
if (!$previousStmt 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
|
2022-04-10 18:18:46 +00:00
|
|
|
$previousNode = $previousStmt->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
|
2022-04-10 18:18:46 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($previousNode->var, $variable)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2018-12-13 21:07:27 +00:00
|
|
|
}
|
2022-04-10 18:18:46 +00:00
|
|
|
if ($this->variableAnalyzer->isStaticOrGlobal($variable)) {
|
2021-10-26 09:28:24 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2022-02-25 08:28:29 +00:00
|
|
|
if ($this->callAnalyzer->isNewInstance($previousNode->var)) {
|
|
|
|
return \true;
|
|
|
|
}
|
2022-04-10 18:18:46 +00:00
|
|
|
return $this->variableAnalyzer->isUsedByReference($variable);
|
2018-12-13 21:07:27 +00:00
|
|
|
}
|
2022-04-10 18:18:46 +00:00
|
|
|
private function hasSomeComment(\PhpParser\Node\Stmt $stmt) : bool
|
2020-12-24 16:28:56 +00:00
|
|
|
{
|
2022-04-10 18:18:46 +00:00
|
|
|
if ($stmt->getComments() !== []) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-12-24 16:28:56 +00:00
|
|
|
}
|
2022-04-10 18:18:46 +00:00
|
|
|
return $stmt->getDocComment() !== null;
|
2020-12-24 16:28:56 +00:00
|
|
|
}
|
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-10-25 21:59:19 +00:00
|
|
|
}
|