mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 17:00:51 +00:00
Updated Rector to commit 764ef74406bf794b30f64d6c5a94e6abfebc4833
764ef74406
[DeadCode] Reduce findFirstPrevious() on RemoveUnusedvariableAssignRector (#3789)
This commit is contained in:
parent
653075e6d9
commit
039864701c
|
@ -18,9 +18,10 @@ use PhpParser\Node\FunctionLike;
|
|||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use Rector\Core\Php\ReservedKeywordAnalyzer;
|
||||
use Rector\Core\PhpParser\Comparing\ConditionSearcher;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\Rector\AbstractScopeAwareRector;
|
||||
use Rector\DeadCode\NodeAnalyzer\ExprUsedInNextNodeAnalyzer;
|
||||
use Rector\DeadCode\NodeAnalyzer\UsedVariableNameAnalyzer;
|
||||
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
|
||||
|
@ -31,7 +32,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
/**
|
||||
* @see \Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\RemoveUnusedVariableAssignRectorTest
|
||||
*/
|
||||
final class RemoveUnusedVariableAssignRector extends AbstractRector
|
||||
final class RemoveUnusedVariableAssignRector extends AbstractScopeAwareRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
|
@ -103,7 +104,7 @@ CODE_SAMPLE
|
|||
/**
|
||||
* @param Assign $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
||||
{
|
||||
if ($this->shouldSkip($node)) {
|
||||
return null;
|
||||
|
@ -117,8 +118,8 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
// variable is used
|
||||
if ($this->isUsed($node, $variable)) {
|
||||
return $this->refactorUsedVariable($node);
|
||||
if ($this->isUsed($node, $variable, $scope)) {
|
||||
return $this->refactorUsedVariable($node, $scope);
|
||||
}
|
||||
if ($this->hasCallLikeInAssignExpr($node->expr)) {
|
||||
// keep the expr, can have side effect
|
||||
|
@ -166,9 +167,9 @@ CODE_SAMPLE
|
|||
return $node instanceof Variable;
|
||||
});
|
||||
}
|
||||
private function isUsed(Assign $assign, Variable $variable) : bool
|
||||
private function isUsed(Assign $assign, Variable $variable, Scope $scope) : bool
|
||||
{
|
||||
$isUsedPrev = $this->isUsedInPreviousNode($variable);
|
||||
$isUsedPrev = $scope->hasVariableType((string) $this->getName($variable))->yes();
|
||||
if ($isUsedPrev) {
|
||||
return \true;
|
||||
}
|
||||
|
@ -180,34 +181,28 @@ CODE_SAMPLE
|
|||
if (!$this->sideEffectNodeDetector->detectCallExpr($expr)) {
|
||||
return \false;
|
||||
}
|
||||
return $this->isUsedInAssignExpr($expr, $assign);
|
||||
}
|
||||
private function isUsedInPreviousNode(Variable $variable) : bool
|
||||
{
|
||||
return (bool) $this->betterNodeFinder->findFirstPrevious($variable, function (Node $node) use($variable) : bool {
|
||||
return $this->usedVariableNameAnalyzer->isVariableNamed($node, $variable);
|
||||
});
|
||||
return $this->isUsedInAssignExpr($expr, $assign, $scope);
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\CallLike|\PhpParser\Node\Expr $expr
|
||||
*/
|
||||
private function isUsedInAssignExpr($expr, Assign $assign) : bool
|
||||
private function isUsedInAssignExpr($expr, Assign $assign, Scope $scope) : bool
|
||||
{
|
||||
if (!$expr instanceof CallLike) {
|
||||
return $this->isUsedInPreviousAssign($assign, $expr);
|
||||
return $this->isUsedInPreviousAssign($assign, $expr, $scope);
|
||||
}
|
||||
if ($expr->isFirstClassCallable()) {
|
||||
return \false;
|
||||
}
|
||||
foreach ($expr->getArgs() as $arg) {
|
||||
$variable = $arg->value;
|
||||
if ($this->isUsedInPreviousAssign($assign, $variable)) {
|
||||
if ($this->isUsedInPreviousAssign($assign, $variable, $scope)) {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
private function isUsedInPreviousAssign(Assign $assign, Expr $expr) : bool
|
||||
private function isUsedInPreviousAssign(Assign $assign, Expr $expr, Scope $scope) : bool
|
||||
{
|
||||
if (!$expr instanceof Variable) {
|
||||
return \false;
|
||||
|
@ -216,11 +211,11 @@ CODE_SAMPLE
|
|||
return $node instanceof Assign && $this->usedVariableNameAnalyzer->isVariableNamed($node->var, $expr);
|
||||
});
|
||||
if ($previousAssign instanceof Assign) {
|
||||
return $this->isUsed($assign, $expr);
|
||||
return $this->isUsed($assign, $expr, $scope);
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
private function refactorUsedVariable(Assign $assign) : ?\PhpParser\Node\Expr
|
||||
private function refactorUsedVariable(Assign $assign, Scope $scope) : ?\PhpParser\Node\Expr
|
||||
{
|
||||
$parentNode = $assign->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (!$parentNode instanceof Expression) {
|
||||
|
@ -229,7 +224,7 @@ CODE_SAMPLE
|
|||
$if = $parentNode->getAttribute(AttributeKey::NEXT_NODE);
|
||||
// check if next node is if
|
||||
if (!$if instanceof If_) {
|
||||
if ($assign->var instanceof Variable && !$this->isUsedInPreviousNode($assign->var) && !$this->exprUsedInNextNodeAnalyzer->isUsed($assign->var)) {
|
||||
if ($assign->var instanceof Variable && !$scope->hasVariableType((string) $this->getName($assign->var))->yes() && !$this->exprUsedInNextNodeAnalyzer->isUsed($assign->var)) {
|
||||
return $this->cleanCastedExpr($assign->expr);
|
||||
}
|
||||
return null;
|
||||
|
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'd87113675bbaaf8de972069779b48f706d2c73d6';
|
||||
public const PACKAGE_VERSION = '764ef74406bf794b30f64d6c5a94e6abfebc4833';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-05-10 11:04:49';
|
||||
public const RELEASE_DATE = '2023-05-10 15:33:04';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
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 ComposerAutoloaderInit0b8c71507f4dac74cba338c5677c745b::getLoader();
|
||||
return ComposerAutoloaderInit7c65664a1e749b16c56435f7b9a0edca::getLoader();
|
||||
|
|
10
vendor/composer/autoload_real.php
vendored
10
vendor/composer/autoload_real.php
vendored
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit0b8c71507f4dac74cba338c5677c745b
|
||||
class ComposerAutoloaderInit7c65664a1e749b16c56435f7b9a0edca
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,17 +22,17 @@ class ComposerAutoloaderInit0b8c71507f4dac74cba338c5677c745b
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit0b8c71507f4dac74cba338c5677c745b', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit7c65664a1e749b16c56435f7b9a0edca', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit0b8c71507f4dac74cba338c5677c745b', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit7c65664a1e749b16c56435f7b9a0edca', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit0b8c71507f4dac74cba338c5677c745b::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit7c65664a1e749b16c56435f7b9a0edca::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit0b8c71507f4dac74cba338c5677c745b::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit7c65664a1e749b16c56435f7b9a0edca::$files;
|
||||
$requireFile = \Closure::bind(static function ($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 ComposerStaticInit0b8c71507f4dac74cba338c5677c745b
|
||||
class ComposerStaticInit7c65664a1e749b16c56435f7b9a0edca
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
|
@ -3111,9 +3111,9 @@ class ComposerStaticInit0b8c71507f4dac74cba338c5677c745b
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit0b8c71507f4dac74cba338c5677c745b::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit0b8c71507f4dac74cba338c5677c745b::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit0b8c71507f4dac74cba338c5677c745b::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit7c65664a1e749b16c56435f7b9a0edca::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit7c65664a1e749b16c56435f7b9a0edca::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit7c65664a1e749b16c56435f7b9a0edca::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user