Updated Rector to commit 246a5863ed

246a5863ed [DeadCode] Prevent RemoveUnusedAssignVariableRector removing variables used in compact() (#238)
This commit is contained in:
Tomas Votruba 2021-06-17 16:01:07 +00:00
parent a042ce3afa
commit 3fdc777bf5
6 changed files with 47 additions and 20 deletions

View File

@ -5,11 +5,14 @@ namespace Rector\DeadCode\Rector\Assign;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\If_;
use PHPStan\Analyser\Scope;
use Rector\Core\NodeAnalyzer\CompactFuncCallAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\NodeAnalyzer\UsedVariableNameAnalyzer;
use Rector\DeadCode\NodeFinder\NextVariableUsageNodeFinder;
use Rector\DeadCode\NodeFinder\PreviousVariableAssignNodeFinder;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
@ -22,6 +25,10 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
*/
final class RemoveUnusedAssignVariableRector extends \Rector\Core\Rector\AbstractRector
{
/**
* @var \Rector\Core\NodeAnalyzer\CompactFuncCallAnalyzer
*/
private $compactFuncCallAnalyzer;
/**
* @var \Rector\DeadCode\NodeFinder\NextVariableUsageNodeFinder
*/
@ -38,12 +45,18 @@ final class RemoveUnusedAssignVariableRector extends \Rector\Core\Rector\Abstrac
* @var \Rector\DeadCode\SideEffect\SideEffectNodeDetector
*/
private $sideEffectNodeDetector;
public function __construct(\Rector\DeadCode\NodeFinder\NextVariableUsageNodeFinder $nextVariableUsageNodeFinder, \Rector\DeadCode\NodeFinder\PreviousVariableAssignNodeFinder $previousVariableAssignNodeFinder, \Rector\NodeNestingScope\ScopeNestingComparator $scopeNestingComparator, \Rector\DeadCode\SideEffect\SideEffectNodeDetector $sideEffectNodeDetector)
/**
* @var \Rector\DeadCode\NodeAnalyzer\UsedVariableNameAnalyzer
*/
private $usedVariableNameAnalyzer;
public function __construct(\Rector\Core\NodeAnalyzer\CompactFuncCallAnalyzer $compactFuncCallAnalyzer, \Rector\DeadCode\NodeFinder\NextVariableUsageNodeFinder $nextVariableUsageNodeFinder, \Rector\DeadCode\NodeFinder\PreviousVariableAssignNodeFinder $previousVariableAssignNodeFinder, \Rector\NodeNestingScope\ScopeNestingComparator $scopeNestingComparator, \Rector\DeadCode\SideEffect\SideEffectNodeDetector $sideEffectNodeDetector, \Rector\DeadCode\NodeAnalyzer\UsedVariableNameAnalyzer $usedVariableNameAnalyzer)
{
$this->compactFuncCallAnalyzer = $compactFuncCallAnalyzer;
$this->nextVariableUsageNodeFinder = $nextVariableUsageNodeFinder;
$this->previousVariableAssignNodeFinder = $previousVariableAssignNodeFinder;
$this->scopeNestingComparator = $scopeNestingComparator;
$this->sideEffectNodeDetector = $sideEffectNodeDetector;
$this->usedVariableNameAnalyzer = $usedVariableNameAnalyzer;
}
/**
* @return array<class-string<Node>>
@ -97,6 +110,9 @@ CODE_SAMPLE
if ($this->isVariableTypeInScope($node) && !$this->isPreviousVariablePartOfOverridingAssign($node)) {
return null;
}
if ($this->isInCompact($node)) {
return null;
}
// is scalar assign? remove whole
if (!$this->sideEffectNodeDetector->detect($node->expr)) {
$this->removeNode($node);
@ -156,4 +172,15 @@ CODE_SAMPLE
$parent = $assign->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
return $parent instanceof \PhpParser\Node\Expr\Assign;
}
private function isInCompact(\PhpParser\Node\Expr\Assign $assign) : bool
{
/** @var Variable $variable */
$variable = $assign->var;
return (bool) $this->betterNodeFinder->findFirstNext($variable, function (\PhpParser\Node $node) use($variable) : bool {
if ($node instanceof \PhpParser\Node\Expr\FuncCall) {
return $this->compactFuncCallAnalyzer->isInCompact($node, $variable);
}
return \false;
});
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '028c1064eb85ace6e4f7639c3db2a3dbdc3d4c53';
public const PACKAGE_VERSION = '246a5863eda22e02a1081b576344eb6e2dda9395';
/**
* @var string
*/
public const RELEASE_DATE = '2021-06-17 17:47:39';
public const RELEASE_DATE = '2021-06-17 17:49:34';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20210617\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

2
vendor/autoload.php vendored
View File

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInitca41cbbc961ba87b8916970d20a6084a::getLoader();
return ComposerAutoloaderInitd785366774feb1ac9d3efd38a84da3f2::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitca41cbbc961ba87b8916970d20a6084a
class ComposerAutoloaderInitd785366774feb1ac9d3efd38a84da3f2
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInitca41cbbc961ba87b8916970d20a6084a
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitca41cbbc961ba87b8916970d20a6084a', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitd785366774feb1ac9d3efd38a84da3f2', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInitca41cbbc961ba87b8916970d20a6084a', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitd785366774feb1ac9d3efd38a84da3f2', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitca41cbbc961ba87b8916970d20a6084a::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitd785366774feb1ac9d3efd38a84da3f2::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,19 +42,19 @@ class ComposerAutoloaderInitca41cbbc961ba87b8916970d20a6084a
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInitca41cbbc961ba87b8916970d20a6084a::$files;
$includeFiles = Composer\Autoload\ComposerStaticInitd785366774feb1ac9d3efd38a84da3f2::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequireca41cbbc961ba87b8916970d20a6084a($fileIdentifier, $file);
composerRequired785366774feb1ac9d3efd38a84da3f2($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequireca41cbbc961ba87b8916970d20a6084a($fileIdentifier, $file)
function composerRequired785366774feb1ac9d3efd38a84da3f2($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInitca41cbbc961ba87b8916970d20a6084a
class ComposerStaticInitd785366774feb1ac9d3efd38a84da3f2
{
public static $files = array (
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
@ -3874,9 +3874,9 @@ class ComposerStaticInitca41cbbc961ba87b8916970d20a6084a
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitca41cbbc961ba87b8916970d20a6084a::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitca41cbbc961ba87b8916970d20a6084a::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitca41cbbc961ba87b8916970d20a6084a::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitd785366774feb1ac9d3efd38a84da3f2::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitd785366774feb1ac9d3efd38a84da3f2::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitd785366774feb1ac9d3efd38a84da3f2::$classMap;
}, null, ClassLoader::class);
}

View File

@ -21,8 +21,8 @@ if (!class_exists('SomeTestCase', false) && !interface_exists('SomeTestCase', fa
if (!class_exists('CheckoutEntityFactory', false) && !interface_exists('CheckoutEntityFactory', false) && !trait_exists('CheckoutEntityFactory', false)) {
spl_autoload_call('RectorPrefix20210617\CheckoutEntityFactory');
}
if (!class_exists('ComposerAutoloaderInitca41cbbc961ba87b8916970d20a6084a', false) && !interface_exists('ComposerAutoloaderInitca41cbbc961ba87b8916970d20a6084a', false) && !trait_exists('ComposerAutoloaderInitca41cbbc961ba87b8916970d20a6084a', false)) {
spl_autoload_call('RectorPrefix20210617\ComposerAutoloaderInitca41cbbc961ba87b8916970d20a6084a');
if (!class_exists('ComposerAutoloaderInitd785366774feb1ac9d3efd38a84da3f2', false) && !interface_exists('ComposerAutoloaderInitd785366774feb1ac9d3efd38a84da3f2', false) && !trait_exists('ComposerAutoloaderInitd785366774feb1ac9d3efd38a84da3f2', false)) {
spl_autoload_call('RectorPrefix20210617\ComposerAutoloaderInitd785366774feb1ac9d3efd38a84da3f2');
}
if (!class_exists('Doctrine\Inflector\Inflector', false) && !interface_exists('Doctrine\Inflector\Inflector', false) && !trait_exists('Doctrine\Inflector\Inflector', false)) {
spl_autoload_call('RectorPrefix20210617\Doctrine\Inflector\Inflector');
@ -3323,9 +3323,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20210617\print_node(...func_get_args());
}
}
if (!function_exists('composerRequireca41cbbc961ba87b8916970d20a6084a')) {
function composerRequireca41cbbc961ba87b8916970d20a6084a() {
return \RectorPrefix20210617\composerRequireca41cbbc961ba87b8916970d20a6084a(...func_get_args());
if (!function_exists('composerRequired785366774feb1ac9d3efd38a84da3f2')) {
function composerRequired785366774feb1ac9d3efd38a84da3f2() {
return \RectorPrefix20210617\composerRequired785366774feb1ac9d3efd38a84da3f2(...func_get_args());
}
}
if (!function_exists('parseArgs')) {