mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
Updated Rector to commit dfc6e1d9c05a4ab0c861332298d3184763d9ebfb
dfc6e1d9c0
[DeadCode] Handle edge cases on RemoveJustVariableAssignRector (#2621)
This commit is contained in:
parent
e2494b520d
commit
76fb7eb0e9
@ -45,6 +45,7 @@ use Rector\DeadCode\Rector\Return_\RemoveDeadConditionAboveReturnRector;
|
||||
use Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector;
|
||||
use Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector;
|
||||
use Rector\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchForAssignRector;
|
||||
use Rector\DeadCode\Rector\StmtsAwareInterface\RemoveJustVariableAssignRector;
|
||||
use Rector\DeadCode\Rector\Switch_\RemoveDuplicatedCaseInSwitchRector;
|
||||
use Rector\DeadCode\Rector\Ternary\TernaryToBooleanOrFalseToBooleanAndRector;
|
||||
use Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector;
|
||||
@ -97,5 +98,6 @@ return static function (RectorConfig $rectorConfig) : void {
|
||||
RemoveUnusedPromotedPropertyRector::class,
|
||||
RemoveLastReturnRector::class,
|
||||
RemoveJustPropertyFetchForAssignRector::class,
|
||||
RemoveJustVariableAssignRector::class,
|
||||
]);
|
||||
};
|
||||
|
@ -4,7 +4,6 @@ declare (strict_types=1);
|
||||
namespace RectorPrefix202207;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\DeadCode\Rector\StmtsAwareInterface\RemoveJustVariableAssignRector;
|
||||
use Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;
|
||||
use Rector\EarlyReturn\Rector\Foreach_\ReturnAfterToEarlyOnBreakRector;
|
||||
use Rector\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector;
|
||||
@ -18,5 +17,5 @@ use Rector\EarlyReturn\Rector\Return_\ReturnBinaryAndToEarlyReturnRector;
|
||||
use Rector\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector;
|
||||
use Rector\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector;
|
||||
return static function (RectorConfig $rectorConfig) : void {
|
||||
$rectorConfig->rules([ChangeNestedForeachIfsToEarlyContinueRector::class, ChangeAndIfToEarlyReturnRector::class, ChangeIfElseValueAssignToEarlyReturnRector::class, ChangeNestedIfsToEarlyReturnRector::class, RemoveAlwaysElseRector::class, ReturnBinaryAndToEarlyReturnRector::class, ChangeOrIfReturnToEarlyReturnRector::class, ChangeOrIfContinueToMultiContinueRector::class, ReturnAfterToEarlyOnBreakRector::class, PreparedValueToEarlyReturnRector::class, ReturnBinaryOrToEarlyReturnRector::class, ReturnEarlyIfVariableRector::class, RemoveJustVariableAssignRector::class]);
|
||||
$rectorConfig->rules([ChangeNestedForeachIfsToEarlyContinueRector::class, ChangeAndIfToEarlyReturnRector::class, ChangeIfElseValueAssignToEarlyReturnRector::class, ChangeNestedIfsToEarlyReturnRector::class, RemoveAlwaysElseRector::class, ReturnBinaryAndToEarlyReturnRector::class, ChangeOrIfReturnToEarlyReturnRector::class, ChangeOrIfContinueToMultiContinueRector::class, ReturnAfterToEarlyOnBreakRector::class, PreparedValueToEarlyReturnRector::class, ReturnBinaryOrToEarlyReturnRector::class, ReturnEarlyIfVariableRector::class]);
|
||||
};
|
||||
|
@ -11,9 +11,12 @@ use PhpParser\Node\Expr\Ternary;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
||||
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
|
||||
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
|
||||
use Rector\Core\NodeAnalyzer\VariableAnalyzer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\DeadCode\NodeAnalyzer\ExprUsedInNextNodeAnalyzer;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
@ -26,9 +29,21 @@ final class RemoveJustVariableAssignRector extends AbstractRector
|
||||
* @var \Rector\Core\NodeAnalyzer\VariableAnalyzer
|
||||
*/
|
||||
private $variableAnalyzer;
|
||||
public function __construct(VariableAnalyzer $variableAnalyzer)
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\DeadCode\NodeAnalyzer\ExprUsedInNextNodeAnalyzer
|
||||
*/
|
||||
private $exprUsedInNextNodeAnalyzer;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\BetterPhpDocParser\Comment\CommentsMerger
|
||||
*/
|
||||
private $commentsMerger;
|
||||
public function __construct(VariableAnalyzer $variableAnalyzer, ExprUsedInNextNodeAnalyzer $exprUsedInNextNodeAnalyzer, CommentsMerger $commentsMerger)
|
||||
{
|
||||
$this->variableAnalyzer = $variableAnalyzer;
|
||||
$this->exprUsedInNextNodeAnalyzer = $exprUsedInNextNodeAnalyzer;
|
||||
$this->commentsMerger = $commentsMerger;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
@ -80,6 +95,10 @@ CODE_SAMPLE
|
||||
if (!$currentAssign instanceof Assign) {
|
||||
continue;
|
||||
}
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($stmt);
|
||||
if ($phpDocInfo->getVarTagValueNode() instanceof VarTagValueNode) {
|
||||
continue;
|
||||
}
|
||||
$nextAssign = $this->matchExpressionAssign($nextStmt);
|
||||
if (!$nextAssign instanceof Assign) {
|
||||
continue;
|
||||
@ -87,11 +106,12 @@ CODE_SAMPLE
|
||||
if ($this->areTooComplexAssignsToShorten($currentAssign, $nextAssign)) {
|
||||
continue;
|
||||
}
|
||||
if (!$this->areTwoVariablesCrossAssign($currentAssign, $nextAssign, $node)) {
|
||||
if (!$this->areTwoVariablesCrossAssign($currentAssign, $nextAssign)) {
|
||||
continue;
|
||||
}
|
||||
// ...
|
||||
$currentAssign->var = $nextAssign->var;
|
||||
$this->commentsMerger->keepComments($stmt, [$stmts[$key + 1]]);
|
||||
unset($stmts[$key + 1]);
|
||||
}
|
||||
if ($originalStmts === $stmts) {
|
||||
@ -108,7 +128,7 @@ CODE_SAMPLE
|
||||
*
|
||||
* + not used $<some> bellow, so removal will not break it
|
||||
*/
|
||||
private function areTwoVariablesCrossAssign(Assign $currentAssign, Assign $nextAssign, StmtsAwareInterface $stmtsAware) : bool
|
||||
private function areTwoVariablesCrossAssign(Assign $currentAssign, Assign $nextAssign) : bool
|
||||
{
|
||||
// is just re-assign to variable
|
||||
if (!$currentAssign->var instanceof Variable) {
|
||||
@ -126,20 +146,7 @@ CODE_SAMPLE
|
||||
if ($this->variableAnalyzer->isUsedByReference($nextAssign->expr)) {
|
||||
return \false;
|
||||
}
|
||||
$currentVariable = $currentAssign->var;
|
||||
$nextVariable = $nextAssign->expr;
|
||||
// is variable used later?
|
||||
$nextUsedVariable = $this->betterNodeFinder->findFirst($stmtsAware, function (Node $node) use($currentVariable, $nextVariable) : bool {
|
||||
if (\in_array($node, [$currentVariable, $nextVariable], \true)) {
|
||||
return \false;
|
||||
}
|
||||
if (!$node instanceof Variable) {
|
||||
return \false;
|
||||
}
|
||||
// is variable name?
|
||||
return $this->nodeNameResolver->areNamesEqual($node, $currentVariable);
|
||||
});
|
||||
return !$nextUsedVariable instanceof Variable;
|
||||
return !$this->exprUsedInNextNodeAnalyzer->isUsed($nextAssign->expr);
|
||||
}
|
||||
/**
|
||||
* Shortening should not make code less readable.
|
||||
|
@ -17,12 +17,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '312099474a70e8ccc361a329bd3ccbc7e4c63a96';
|
||||
public const PACKAGE_VERSION = 'dfc6e1d9c05a4ab0c861332298d3184763d9ebfb';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2022-07-03 16:17:20';
|
||||
public const RELEASE_DATE = '2022-07-03 16:29:14';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitb80e4de87d103422d8d99a5b0c5a86a9::getLoader();
|
||||
return ComposerAutoloaderInita850dba1f031cfbafe4cb9ef8ddc7b73::getLoader();
|
||||
|
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 ComposerAutoloaderInitb80e4de87d103422d8d99a5b0c5a86a9
|
||||
class ComposerAutoloaderInita850dba1f031cfbafe4cb9ef8ddc7b73
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,19 +22,19 @@ class ComposerAutoloaderInitb80e4de87d103422d8d99a5b0c5a86a9
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitb80e4de87d103422d8d99a5b0c5a86a9', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInita850dba1f031cfbafe4cb9ef8ddc7b73', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitb80e4de87d103422d8d99a5b0c5a86a9', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInita850dba1f031cfbafe4cb9ef8ddc7b73', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitb80e4de87d103422d8d99a5b0c5a86a9::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInita850dba1f031cfbafe4cb9ef8ddc7b73::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInitb80e4de87d103422d8d99a5b0c5a86a9::$files;
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInita850dba1f031cfbafe4cb9ef8ddc7b73::$files;
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequireb80e4de87d103422d8d99a5b0c5a86a9($fileIdentifier, $file);
|
||||
composerRequirea850dba1f031cfbafe4cb9ef8ddc7b73($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
@ -46,7 +46,7 @@ class ComposerAutoloaderInitb80e4de87d103422d8d99a5b0c5a86a9
|
||||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
function composerRequireb80e4de87d103422d8d99a5b0c5a86a9($fileIdentifier, $file)
|
||||
function composerRequirea850dba1f031cfbafe4cb9ef8ddc7b73($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
8
vendor/composer/autoload_static.php
vendored
8
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitb80e4de87d103422d8d99a5b0c5a86a9
|
||||
class ComposerStaticInita850dba1f031cfbafe4cb9ef8ddc7b73
|
||||
{
|
||||
public static $files = array (
|
||||
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
|
||||
@ -3416,9 +3416,9 @@ class ComposerStaticInitb80e4de87d103422d8d99a5b0c5a86a9
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitb80e4de87d103422d8d99a5b0c5a86a9::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitb80e4de87d103422d8d99a5b0c5a86a9::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitb80e4de87d103422d8d99a5b0c5a86a9::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInita850dba1f031cfbafe4cb9ef8ddc7b73::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInita850dba1f031cfbafe4cb9ef8ddc7b73::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInita850dba1f031cfbafe4cb9ef8ddc7b73::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user