mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-20 23:41:57 +02:00
Updated Rector to commit 4f8a0ad5194898f6947276b308c53e391cf524ac
4f8a0ad519
Add new rule to simplify a last useless variable assignment. (#2963)
This commit is contained in:
parent
a018694585
commit
de1025734c
@ -1,4 +1,4 @@
|
||||
# 398 Rules Overview
|
||||
# 399 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
|
||||
- [Arguments](#arguments) (5)
|
||||
|
||||
- [CodeQuality](#codequality) (75)
|
||||
- [CodeQuality](#codequality) (76)
|
||||
|
||||
- [CodingStyle](#codingstyle) (36)
|
||||
|
||||
@ -1478,6 +1478,25 @@ Simplify tautology ternary to value
|
||||
|
||||
<br>
|
||||
|
||||
### SimplifyUselessLastVariableAssignRector
|
||||
|
||||
Removes the latest useless variable assigns before a variable will return.
|
||||
|
||||
- class: [`Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessLastVariableAssignRector`](../rules/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector.php)
|
||||
|
||||
```diff
|
||||
function ($b) {
|
||||
- $a = true;
|
||||
if ($b === 1) {
|
||||
return $b;
|
||||
}
|
||||
- return $a;
|
||||
+ return true;
|
||||
};
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### SimplifyUselessVariableRector
|
||||
|
||||
Removes useless variable assigns
|
||||
|
@ -0,0 +1,272 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\CodeQuality\Rector\FunctionLike;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Array_;
|
||||
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\ExprAnalyzer;
|
||||
use Rector\Core\NodeAnalyzer\VariableAnalyzer;
|
||||
use Rector\Core\NodeManipulator\ArrayManipulator;
|
||||
use Rector\Core\PhpParser\Node\AssignAndBinaryMap;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\CodeQuality\Rector\FunctionLike\SimplifyUselessLastVariableAssignRector\SimplifyUselessLastVariableAssignRectorTest
|
||||
*/
|
||||
final class SimplifyUselessLastVariableAssignRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\Node\AssignAndBinaryMap
|
||||
*/
|
||||
private $assignAndBinaryMap;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeAnalyzer\VariableAnalyzer
|
||||
*/
|
||||
private $variableAnalyzer;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\CodeQuality\NodeAnalyzer\ReturnAnalyzer
|
||||
*/
|
||||
private $returnAnalyzer;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer
|
||||
*/
|
||||
private $exprUsedInNodeAnalyzer;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeManipulator\ArrayManipulator
|
||||
*/
|
||||
private $arrayManipulator;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeAnalyzer\ExprAnalyzer
|
||||
*/
|
||||
private $exprAnalyzer;
|
||||
public function __construct(AssignAndBinaryMap $assignAndBinaryMap, VariableAnalyzer $variableAnalyzer, ReturnAnalyzer $returnAnalyzer, ExprUsedInNodeAnalyzer $exprUsedInNodeAnalyzer, ArrayManipulator $arrayManipulator, ExprAnalyzer $exprAnalyzer)
|
||||
{
|
||||
$this->assignAndBinaryMap = $assignAndBinaryMap;
|
||||
$this->variableAnalyzer = $variableAnalyzer;
|
||||
$this->returnAnalyzer = $returnAnalyzer;
|
||||
$this->exprUsedInNodeAnalyzer = $exprUsedInNodeAnalyzer;
|
||||
$this->arrayManipulator = $arrayManipulator;
|
||||
$this->exprAnalyzer = $exprAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Removes the latest useless variable assigns before a variable will return.', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
function ($b) {
|
||||
$a = true;
|
||||
if ($b === 1) {
|
||||
return $b;
|
||||
}
|
||||
return $a;
|
||||
};
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
function ($b) {
|
||||
if ($b === 1) {
|
||||
return $b;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [StmtsAwareInterface::class];
|
||||
}
|
||||
/**
|
||||
* @param StmtsAwareInterface $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
$stmts = $node->stmts;
|
||||
if ($stmts === null) {
|
||||
return null;
|
||||
}
|
||||
foreach ($stmts as $stmt) {
|
||||
if (!$stmt instanceof Return_) {
|
||||
continue;
|
||||
}
|
||||
if ($this->shouldSkip($stmt)) {
|
||||
return null;
|
||||
}
|
||||
$assignStmt = $this->getLatestVariableAssignment($stmts, $stmt);
|
||||
if (!$assignStmt instanceof Expression) {
|
||||
return null;
|
||||
}
|
||||
if ($this->shouldSkipOnAssignStmt($assignStmt)) {
|
||||
return null;
|
||||
}
|
||||
if ($this->isAssigmentUseless($stmts, $assignStmt, $stmt)) {
|
||||
return null;
|
||||
}
|
||||
$assign = $assignStmt->expr;
|
||||
if (!$assign instanceof Assign && !$assign instanceof AssignOp) {
|
||||
return null;
|
||||
}
|
||||
$this->removeNode($assignStmt);
|
||||
return $this->processSimplifyUselessVariable($node, $stmt, $assign);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private function shouldSkip(Return_ $return) : bool
|
||||
{
|
||||
$variable = $return->expr;
|
||||
if (!$variable instanceof Variable) {
|
||||
return \true;
|
||||
}
|
||||
if ($this->returnAnalyzer->hasByRefReturn($return)) {
|
||||
return \true;
|
||||
}
|
||||
if ($this->variableAnalyzer->isStaticOrGlobal($variable)) {
|
||||
return \true;
|
||||
}
|
||||
if ($this->variableAnalyzer->isUsedByReference($variable)) {
|
||||
return \true;
|
||||
}
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($return);
|
||||
return !$phpDocInfo->getVarType() instanceof MixedType;
|
||||
}
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
*/
|
||||
private function getLatestVariableAssignment(array $stmts, Return_ $return) : ?Expression
|
||||
{
|
||||
$returnVariable = $return->expr;
|
||||
if (!$returnVariable instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
//Search for the latest variable assigment
|
||||
foreach (\array_reverse($stmts) as $stmt) {
|
||||
if (!$stmt instanceof Expression) {
|
||||
continue;
|
||||
}
|
||||
$assignNode = $stmt->expr;
|
||||
if (!$assignNode instanceof Assign && !$assignNode instanceof AssignOp) {
|
||||
continue;
|
||||
}
|
||||
$currentVariableNode = $assignNode->var;
|
||||
if (!$currentVariableNode instanceof Variable) {
|
||||
continue;
|
||||
}
|
||||
if ($this->nodeNameResolver->areNamesEqual($returnVariable, $currentVariableNode)) {
|
||||
return $stmt;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private function shouldSkipOnAssignStmt(Expression $assignStmt) : bool
|
||||
{
|
||||
if ($this->hasSomeComment($assignStmt)) {
|
||||
return \true;
|
||||
}
|
||||
$assign = $assignStmt->expr;
|
||||
if (!$assign instanceof Assign && !$assign instanceof AssignOp) {
|
||||
return \true;
|
||||
}
|
||||
$variable = $assign->var;
|
||||
if (!$variable instanceof Variable) {
|
||||
return \true;
|
||||
}
|
||||
$value = $assign->expr;
|
||||
if ($this->exprAnalyzer->isDynamicExpr($value)) {
|
||||
return \true;
|
||||
}
|
||||
if ($value instanceof Array_ && $this->arrayManipulator->isDynamicArray($value)) {
|
||||
return \true;
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
private function hasSomeComment(Expression $stmt) : bool
|
||||
{
|
||||
if ($stmt->getComments() !== []) {
|
||||
return \true;
|
||||
}
|
||||
return $stmt->getDocComment() !== null;
|
||||
}
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
*/
|
||||
private function isAssigmentUseless(array $stmts, Expression $assignStmt, Return_ $return) : bool
|
||||
{
|
||||
$assign = $assignStmt->expr;
|
||||
if (!$assign instanceof Assign && !$assign instanceof AssignOp) {
|
||||
return \false;
|
||||
}
|
||||
$variable = $assign->var;
|
||||
if (!$variable instanceof Variable) {
|
||||
return \false;
|
||||
}
|
||||
$nodesInRange = $this->getNodesInRange($stmts, $assignStmt, $return);
|
||||
if ($nodesInRange === null) {
|
||||
return \false;
|
||||
}
|
||||
//Find the variable usage
|
||||
$variableUsageNodes = $this->betterNodeFinder->find($nodesInRange, function (Node $node) use($variable) : bool {
|
||||
return $this->exprUsedInNodeAnalyzer->isUsed($node, $variable);
|
||||
});
|
||||
//Should be exactly used 2 times (assignment + return)
|
||||
return \count($variableUsageNodes) !== 2;
|
||||
}
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
* @return Stmt[]|null
|
||||
*/
|
||||
private function getNodesInRange(array $stmts, Expression $start, Return_ $end) : ?array
|
||||
{
|
||||
$resultStmts = [];
|
||||
$wasStarted = \false;
|
||||
foreach ($stmts as $stmt) {
|
||||
if ($stmt === $start) {
|
||||
$wasStarted = \true;
|
||||
}
|
||||
if ($wasStarted) {
|
||||
$resultStmts[] = $stmt;
|
||||
}
|
||||
if ($stmt === $end) {
|
||||
return $resultStmts;
|
||||
}
|
||||
}
|
||||
if ($wasStarted) {
|
||||
// This should not happen, if you land here check your given parameter
|
||||
return null;
|
||||
}
|
||||
return $resultStmts;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\Assign|\PhpParser\Node\Expr\AssignOp $assign
|
||||
*/
|
||||
private function processSimplifyUselessVariable(StmtsAwareInterface $stmtsAware, Return_ $return, $assign) : ?StmtsAwareInterface
|
||||
{
|
||||
if (!$assign instanceof Assign) {
|
||||
$binaryClass = $this->assignAndBinaryMap->getAlternative($assign);
|
||||
if ($binaryClass === null) {
|
||||
return null;
|
||||
}
|
||||
$return->expr = new $binaryClass($assign->var, $assign->expr);
|
||||
} else {
|
||||
$return->expr = $assign->expr;
|
||||
}
|
||||
return $stmtsAware;
|
||||
}
|
||||
}
|
@ -17,12 +17,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'ee04ee1dc59ff0a6c3ff5d6558612dce7757a8f2';
|
||||
public const PACKAGE_VERSION = '4f8a0ad5194898f6947276b308c53e391cf524ac';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2022-10-14 16:46:51';
|
||||
public const RELEASE_DATE = '2022-10-15 17:33:13';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
||||
namespace Rector\Core\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\AssignRef;
|
||||
use PhpParser\Node\Expr\ClosureUse;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Param;
|
||||
@ -66,10 +67,10 @@ final class VariableAnalyzer
|
||||
return \false;
|
||||
}
|
||||
$parentNode = $subNode->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (!$parentNode instanceof ClosureUse) {
|
||||
return \false;
|
||||
if ($parentNode instanceof ClosureUse) {
|
||||
return $parentNode->byRef;
|
||||
}
|
||||
return $parentNode->byRef;
|
||||
return $parentNode instanceof AssignRef;
|
||||
});
|
||||
}
|
||||
private function isParentStaticOrGlobal(Variable $variable) : bool
|
||||
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit5c1fdb34e3926a7a0bf66efb0dc94722::getLoader();
|
||||
return ComposerAutoloaderInit600b7d14138d91aff84e652518e1e350::getLoader();
|
||||
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
@ -1193,6 +1193,7 @@ return array(
|
||||
'Rector\\CodeQuality\\Rector\\FuncCall\\StrvalToTypeCastRector' => $baseDir . '/rules/CodeQuality/Rector/FuncCall/StrvalToTypeCastRector.php',
|
||||
'Rector\\CodeQuality\\Rector\\FuncCall\\UnwrapSprintfOneArgumentRector' => $baseDir . '/rules/CodeQuality/Rector/FuncCall/UnwrapSprintfOneArgumentRector.php',
|
||||
'Rector\\CodeQuality\\Rector\\FunctionLike\\RemoveAlwaysTrueConditionSetInConstructorRector' => $baseDir . '/rules/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector.php',
|
||||
'Rector\\CodeQuality\\Rector\\FunctionLike\\SimplifyUselessLastVariableAssignRector' => $baseDir . '/rules/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector.php',
|
||||
'Rector\\CodeQuality\\Rector\\FunctionLike\\SimplifyUselessVariableRector' => $baseDir . '/rules/CodeQuality/Rector/FunctionLike/SimplifyUselessVariableRector.php',
|
||||
'Rector\\CodeQuality\\Rector\\Identical\\BooleanNotIdenticalToNotIdenticalRector' => $baseDir . '/rules/CodeQuality/Rector/Identical/BooleanNotIdenticalToNotIdenticalRector.php',
|
||||
'Rector\\CodeQuality\\Rector\\Identical\\FlipTypeControlToUseExclusiveTypeRector' => $baseDir . '/rules/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector.php',
|
||||
|
14
vendor/composer/autoload_real.php
vendored
14
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit5c1fdb34e3926a7a0bf66efb0dc94722
|
||||
class ComposerAutoloaderInit600b7d14138d91aff84e652518e1e350
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,19 +22,19 @@ class ComposerAutoloaderInit5c1fdb34e3926a7a0bf66efb0dc94722
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit5c1fdb34e3926a7a0bf66efb0dc94722', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit600b7d14138d91aff84e652518e1e350', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit5c1fdb34e3926a7a0bf66efb0dc94722', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit600b7d14138d91aff84e652518e1e350', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit5c1fdb34e3926a7a0bf66efb0dc94722::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit600b7d14138d91aff84e652518e1e350::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit5c1fdb34e3926a7a0bf66efb0dc94722::$files;
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit600b7d14138d91aff84e652518e1e350::$files;
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequire5c1fdb34e3926a7a0bf66efb0dc94722($fileIdentifier, $file);
|
||||
composerRequire600b7d14138d91aff84e652518e1e350($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
@ -46,7 +46,7 @@ class ComposerAutoloaderInit5c1fdb34e3926a7a0bf66efb0dc94722
|
||||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
function composerRequire5c1fdb34e3926a7a0bf66efb0dc94722($fileIdentifier, $file)
|
||||
function composerRequire600b7d14138d91aff84e652518e1e350($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
9
vendor/composer/autoload_static.php
vendored
9
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit5c1fdb34e3926a7a0bf66efb0dc94722
|
||||
class ComposerStaticInit600b7d14138d91aff84e652518e1e350
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
@ -1448,6 +1448,7 @@ class ComposerStaticInit5c1fdb34e3926a7a0bf66efb0dc94722
|
||||
'Rector\\CodeQuality\\Rector\\FuncCall\\StrvalToTypeCastRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FuncCall/StrvalToTypeCastRector.php',
|
||||
'Rector\\CodeQuality\\Rector\\FuncCall\\UnwrapSprintfOneArgumentRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FuncCall/UnwrapSprintfOneArgumentRector.php',
|
||||
'Rector\\CodeQuality\\Rector\\FunctionLike\\RemoveAlwaysTrueConditionSetInConstructorRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector.php',
|
||||
'Rector\\CodeQuality\\Rector\\FunctionLike\\SimplifyUselessLastVariableAssignRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector.php',
|
||||
'Rector\\CodeQuality\\Rector\\FunctionLike\\SimplifyUselessVariableRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FunctionLike/SimplifyUselessVariableRector.php',
|
||||
'Rector\\CodeQuality\\Rector\\Identical\\BooleanNotIdenticalToNotIdenticalRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Identical/BooleanNotIdenticalToNotIdenticalRector.php',
|
||||
'Rector\\CodeQuality\\Rector\\Identical\\FlipTypeControlToUseExclusiveTypeRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector.php',
|
||||
@ -3091,9 +3092,9 @@ class ComposerStaticInit5c1fdb34e3926a7a0bf66efb0dc94722
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit5c1fdb34e3926a7a0bf66efb0dc94722::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit5c1fdb34e3926a7a0bf66efb0dc94722::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit5c1fdb34e3926a7a0bf66efb0dc94722::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit600b7d14138d91aff84e652518e1e350::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit600b7d14138d91aff84e652518e1e350::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit600b7d14138d91aff84e652518e1e350::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user