2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\FunctionLike;
|
2018-10-25 21:59:19 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\AssignOp;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
|
use PHPStan\Type\MixedType;
|
|
|
|
use Rector\CodeQuality\NodeAnalyzer\ReturnAnalyzer;
|
|
|
|
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
|
|
|
|
use Rector\Core\NodeAnalyzer\CallAnalyzer;
|
|
|
|
use Rector\Core\NodeAnalyzer\VariableAnalyzer;
|
|
|
|
use Rector\Core\PhpParser\Node\AssignAndBinaryMap;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2022-06-07 09:18:30 +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
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SimplifyUselessVariableRector extends 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;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(AssignAndBinaryMap $assignAndBinaryMap, VariableAnalyzer $variableAnalyzer, CallAnalyzer $callAnalyzer, 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
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-10-25 21:59:19 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Removes useless variable assigns', [new 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-06-07 08:22:29 +00:00
|
|
|
return [StmtsAwareInterface::class];
|
2018-10-25 21:59:19 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-05-14 09:33:59 +00:00
|
|
|
* @param StmtsAwareInterface $node
|
2018-10-25 21:59:19 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-10-25 21:59:19 +00:00
|
|
|
{
|
2022-05-14 09:33:59 +00:00
|
|
|
$stmts = $node->stmts;
|
2022-04-10 18:18:46 +00:00
|
|
|
if ($stmts === null) {
|
2018-10-25 21:59:19 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-05-14 07:38:00 +00:00
|
|
|
foreach ($stmts as $key => $stmt) {
|
|
|
|
if (!isset($stmts[$key - 1])) {
|
2022-04-10 18:18:46 +00:00
|
|
|
continue;
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$stmt instanceof Return_) {
|
2022-05-14 07:38:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$previousStmt = $stmts[$key - 1];
|
2022-04-10 18:18:46 +00:00
|
|
|
if ($this->shouldSkipStmt($stmt, $previousStmt)) {
|
2022-05-14 09:33:59 +00:00
|
|
|
return null;
|
2018-10-25 21:59:19 +00:00
|
|
|
}
|
2022-05-14 07:38:00 +00:00
|
|
|
if ($this->isReturnWithVarAnnotation($stmt)) {
|
2022-05-14 09:33:59 +00:00
|
|
|
return null;
|
2022-02-05 11:57:25 +00:00
|
|
|
}
|
2022-05-14 07:38:00 +00:00
|
|
|
/**
|
|
|
|
* @var Expression $previousStmt
|
|
|
|
* @var Assign|AssignOp $assign
|
|
|
|
*/
|
|
|
|
$assign = $previousStmt->expr;
|
|
|
|
return $this->processSimplifyUselessVariable($node, $stmt, $assign, $key);
|
2022-04-10 18:18:46 +00:00
|
|
|
}
|
|
|
|
return null;
|
2021-05-12 13:15:45 +00:00
|
|
|
}
|
2022-05-14 07:38:00 +00:00
|
|
|
/**
|
|
|
|
* @param \PhpParser\Node\Expr\Assign|\PhpParser\Node\Expr\AssignOp $assign
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processSimplifyUselessVariable(StmtsAwareInterface $stmtsAware, Return_ $return, $assign, int $key) : ?StmtsAwareInterface
|
2022-05-14 07:38:00 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$assign instanceof Assign) {
|
2022-05-14 07:38:00 +00:00
|
|
|
$binaryClass = $this->assignAndBinaryMap->getAlternative($assign);
|
|
|
|
if ($binaryClass === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$return->expr = new $binaryClass($assign->var, $assign->expr);
|
|
|
|
} else {
|
|
|
|
$return->expr = $assign->expr;
|
|
|
|
}
|
2022-05-14 09:33:59 +00:00
|
|
|
unset($stmtsAware->stmts[$key - 1]);
|
|
|
|
return $stmtsAware;
|
2022-05-14 07:38:00 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkipStmt(Return_ $return, 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;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$return->expr instanceof 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;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$previousStmt instanceof 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;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$previousNode instanceof AssignOp && !$previousNode instanceof 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-06-07 08:22:29 +00:00
|
|
|
private function hasSomeComment(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
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isReturnWithVarAnnotation(Return_ $return) : bool
|
2020-09-01 17:56:30 +00:00
|
|
|
{
|
2021-01-18 21:45:13 +00:00
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($return);
|
2022-06-07 08:22:29 +00:00
|
|
|
return !$phpDocInfo->getVarType() instanceof MixedType;
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|
2018-10-25 21:59:19 +00:00
|
|
|
}
|