From f415b891e87f7f90d54a6910ed2d62f9d72932c5 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 19 Jan 2022 09:10:13 +0000 Subject: [PATCH] Updated Rector to commit 7d9262746f628e267f6300c4a11b254adad9cb64 https://github.com/rectorphp/rector-src/commit/7d9262746f628e267f6300c4a11b254adad9cb64 [Php56] Skip AddDefaultValueForUndefinedVariableRector on empty() check (#1697) --- .../UndefinedVariableResolver.php | 58 ++++++++----------- src/Application/VersionResolver.php | 4 +- vendor/autoload.php | 2 +- vendor/composer/autoload_real.php | 14 ++--- vendor/composer/autoload_static.php | 8 +-- vendor/scoper-autoload.php | 10 ++-- 6 files changed, 43 insertions(+), 53 deletions(-) diff --git a/rules/Php56/NodeAnalyzer/UndefinedVariableResolver.php b/rules/Php56/NodeAnalyzer/UndefinedVariableResolver.php index d92e5152c37..4a346268e4c 100644 --- a/rules/Php56/NodeAnalyzer/UndefinedVariableResolver.php +++ b/rules/Php56/NodeAnalyzer/UndefinedVariableResolver.php @@ -11,6 +11,7 @@ use PhpParser\Node\Expr\AssignRef; use PhpParser\Node\Expr\BinaryOp\Coalesce; use PhpParser\Node\Expr\Cast\Unset_ as UnsetCast; use PhpParser\Node\Expr\Closure; +use PhpParser\Node\Expr\Empty_; use PhpParser\Node\Expr\Isset_; use PhpParser\Node\Expr\List_; use PhpParser\Node\Expr\Variable; @@ -65,8 +66,7 @@ final class UndefinedVariableResolver public function resolve($node) : array { $undefinedVariables = []; - $variableNamesFromParams = $this->collectVariableNamesFromParams($node); - $this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $node->stmts, function (\PhpParser\Node $node) use(&$undefinedVariables, $variableNamesFromParams) : ?int { + $this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $node->stmts, function (\PhpParser\Node $node) use(&$undefinedVariables) : ?int { // entering new scope - break! if ($node instanceof \PhpParser\Node\FunctionLike && !$node instanceof \PhpParser\Node\Expr\ArrowFunction) { return \PhpParser\NodeTraverser::STOP_TRAVERSAL; @@ -91,31 +91,14 @@ final class UndefinedVariableResolver if ($scope->hasVariableType($variableName)->yes()) { return null; } - if (\in_array($variableName, $variableNamesFromParams, \true)) { - return null; - } $undefinedVariables[] = $variableName; return null; }); return \array_unique($undefinedVariables); } - /** - * @return string[] - * @param \PhpParser\Node\Expr\Closure|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $node - */ - private function collectVariableNamesFromParams($node) : array + private function issetOrUnsetOrEmptyParent(\PhpParser\Node $parentNode) : bool { - $variableNames = []; - foreach ($node->getParams() as $param) { - if ($param->var instanceof \PhpParser\Node\Expr\Variable) { - $variableNames[] = (string) $this->nodeNameResolver->getName($param->var); - } - } - return $variableNames; - } - private function issetOrUnsetParent(\PhpParser\Node $parentNode) : bool - { - return \in_array(\get_class($parentNode), [\PhpParser\Node\Stmt\Unset_::class, \PhpParser\Node\Expr\Cast\Unset_::class, \PhpParser\Node\Expr\Isset_::class], \true); + return \in_array(\get_class($parentNode), [\PhpParser\Node\Stmt\Unset_::class, \PhpParser\Node\Expr\Cast\Unset_::class, \PhpParser\Node\Expr\Isset_::class, \PhpParser\Node\Expr\Empty_::class], \true); } private function isAsCoalesceLeft(\PhpParser\Node $parentNode, \PhpParser\Node\Expr\Variable $variable) : bool { @@ -140,7 +123,7 @@ final class UndefinedVariableResolver if ($this->isAssignOrStaticVariableParent($parentNode)) { return \true; } - if ($this->issetOrUnsetParent($parentNode)) { + if ($this->issetOrUnsetOrEmptyParent($parentNode)) { return \true; } if ($this->isAsCoalesceLeft($parentNode, $variable)) { @@ -154,10 +137,6 @@ final class UndefinedVariableResolver if (!$nodeScope instanceof \PHPStan\Analyser\Scope) { return \true; } - $originalNode = $variable->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::ORIGINAL_NODE); - if (!$this->nodeComparator->areNodesEqual($variable, $originalNode)) { - return \true; - } $variableName = $this->nodeNameResolver->getName($variable); // skip $this, as probably in outer scope if ($variableName === 'this') { @@ -166,7 +145,10 @@ final class UndefinedVariableResolver if ($variableName === null) { return \true; } - return $this->hasPreviousCheckedWithIsset($variable); + if ($this->hasPreviousCheckedWithIsset($variable)) { + return \true; + } + return $this->hasPreviousCheckedWithEmpty($variable); } private function hasPreviousCheckedWithIsset(\PhpParser\Node\Expr\Variable $variable) : bool { @@ -183,16 +165,24 @@ final class UndefinedVariableResolver return \false; }); } + private function hasPreviousCheckedWithEmpty(\PhpParser\Node\Expr\Variable $variable) : bool + { + return (bool) $this->betterNodeFinder->findFirstPreviousOfNode($variable, function (\PhpParser\Node $subNode) use($variable) : bool { + if (!$subNode instanceof \PhpParser\Node\Expr\Empty_) { + return \false; + } + $subNodeExpr = $subNode->expr; + return $this->nodeComparator->areNodesEqual($subNodeExpr, $variable); + }); + } private function isStaticVariable(\PhpParser\Node $parentNode) : bool { - // definition of static variable - if ($parentNode instanceof \PhpParser\Node\Stmt\StaticVar) { - $parentParentNode = $parentNode->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE); - if ($parentParentNode instanceof \PhpParser\Node\Stmt\Static_) { - return \true; - } + if (!$parentNode instanceof \PhpParser\Node\Stmt\StaticVar) { + return \false; } - return \false; + // definition of static variable + $parentParentNode = $parentNode->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE); + return $parentParentNode instanceof \PhpParser\Node\Stmt\Static_; } private function isListAssign(\PhpParser\Node $node) : bool { diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index cd04bb9a0e7..6a86d008621 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -16,11 +16,11 @@ final class VersionResolver /** * @var string */ - public const PACKAGE_VERSION = 'dbf132092afa105601f993a27462abfb99335a4f'; + public const PACKAGE_VERSION = '7d9262746f628e267f6300c4a11b254adad9cb64'; /** * @var string */ - public const RELEASE_DATE = '2022-01-19 09:48:32'; + public const RELEASE_DATE = '2022-01-19 10:01:35'; public static function resolvePackageVersion() : string { $process = new \RectorPrefix20220119\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__); diff --git a/vendor/autoload.php b/vendor/autoload.php index 735694fedf1..bb428fb1302 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -4,4 +4,4 @@ require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInitbd22ac90a2e6a2d98be7c9ab5156beb4::getLoader(); +return ComposerAutoloaderInitf335598685e810e881ced0a65d71e1b5::getLoader(); diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index cd2003edf6b..3be43f7899d 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInitbd22ac90a2e6a2d98be7c9ab5156beb4 +class ComposerAutoloaderInitf335598685e810e881ced0a65d71e1b5 { private static $loader; @@ -22,15 +22,15 @@ class ComposerAutoloaderInitbd22ac90a2e6a2d98be7c9ab5156beb4 return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInitbd22ac90a2e6a2d98be7c9ab5156beb4', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInitf335598685e810e881ced0a65d71e1b5', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__))); - spl_autoload_unregister(array('ComposerAutoloaderInitbd22ac90a2e6a2d98be7c9ab5156beb4', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInitf335598685e810e881ced0a65d71e1b5', '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\ComposerStaticInitbd22ac90a2e6a2d98be7c9ab5156beb4::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInitf335598685e810e881ced0a65d71e1b5::getInitializer($loader)); } else { $classMap = require __DIR__ . '/autoload_classmap.php'; if ($classMap) { @@ -42,12 +42,12 @@ class ComposerAutoloaderInitbd22ac90a2e6a2d98be7c9ab5156beb4 $loader->register(true); if ($useStaticLoader) { - $includeFiles = Composer\Autoload\ComposerStaticInitbd22ac90a2e6a2d98be7c9ab5156beb4::$files; + $includeFiles = Composer\Autoload\ComposerStaticInitf335598685e810e881ced0a65d71e1b5::$files; } else { $includeFiles = require __DIR__ . '/autoload_files.php'; } foreach ($includeFiles as $fileIdentifier => $file) { - composerRequirebd22ac90a2e6a2d98be7c9ab5156beb4($fileIdentifier, $file); + composerRequiref335598685e810e881ced0a65d71e1b5($fileIdentifier, $file); } return $loader; @@ -59,7 +59,7 @@ class ComposerAutoloaderInitbd22ac90a2e6a2d98be7c9ab5156beb4 * @param string $file * @return void */ -function composerRequirebd22ac90a2e6a2d98be7c9ab5156beb4($fileIdentifier, $file) +function composerRequiref335598685e810e881ced0a65d71e1b5($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 220b72ff001..f3de0b1e92d 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInitbd22ac90a2e6a2d98be7c9ab5156beb4 +class ComposerStaticInitf335598685e810e881ced0a65d71e1b5 { public static $files = array ( '0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php', @@ -3872,9 +3872,9 @@ class ComposerStaticInitbd22ac90a2e6a2d98be7c9ab5156beb4 public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInitbd22ac90a2e6a2d98be7c9ab5156beb4::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInitbd22ac90a2e6a2d98be7c9ab5156beb4::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInitbd22ac90a2e6a2d98be7c9ab5156beb4::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInitf335598685e810e881ced0a65d71e1b5::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInitf335598685e810e881ced0a65d71e1b5::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInitf335598685e810e881ced0a65d71e1b5::$classMap; }, null, ClassLoader::class); } diff --git a/vendor/scoper-autoload.php b/vendor/scoper-autoload.php index 36cc76c56d4..16d8be04ef1 100644 --- a/vendor/scoper-autoload.php +++ b/vendor/scoper-autoload.php @@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php'; if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) { spl_autoload_call('RectorPrefix20220119\AutoloadIncluder'); } -if (!class_exists('ComposerAutoloaderInitbd22ac90a2e6a2d98be7c9ab5156beb4', false) && !interface_exists('ComposerAutoloaderInitbd22ac90a2e6a2d98be7c9ab5156beb4', false) && !trait_exists('ComposerAutoloaderInitbd22ac90a2e6a2d98be7c9ab5156beb4', false)) { - spl_autoload_call('RectorPrefix20220119\ComposerAutoloaderInitbd22ac90a2e6a2d98be7c9ab5156beb4'); +if (!class_exists('ComposerAutoloaderInitf335598685e810e881ced0a65d71e1b5', false) && !interface_exists('ComposerAutoloaderInitf335598685e810e881ced0a65d71e1b5', false) && !trait_exists('ComposerAutoloaderInitf335598685e810e881ced0a65d71e1b5', false)) { + spl_autoload_call('RectorPrefix20220119\ComposerAutoloaderInitf335598685e810e881ced0a65d71e1b5'); } if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) { spl_autoload_call('RectorPrefix20220119\Helmich\TypoScriptParser\Parser\AST\Statement'); @@ -71,9 +71,9 @@ if (!function_exists('print_node')) { return \RectorPrefix20220119\print_node(...func_get_args()); } } -if (!function_exists('composerRequirebd22ac90a2e6a2d98be7c9ab5156beb4')) { - function composerRequirebd22ac90a2e6a2d98be7c9ab5156beb4() { - return \RectorPrefix20220119\composerRequirebd22ac90a2e6a2d98be7c9ab5156beb4(...func_get_args()); +if (!function_exists('composerRequiref335598685e810e881ced0a65d71e1b5')) { + function composerRequiref335598685e810e881ced0a65d71e1b5() { + return \RectorPrefix20220119\composerRequiref335598685e810e881ced0a65d71e1b5(...func_get_args()); } } if (!function_exists('scanPath')) {