Updated Rector to commit 7d9262746f628e267f6300c4a11b254adad9cb64

7d9262746f [Php56] Skip AddDefaultValueForUndefinedVariableRector on empty() check (#1697)
This commit is contained in:
Tomas Votruba 2022-01-19 09:10:13 +00:00
parent f2b4b95aa2
commit f415b891e8
6 changed files with 43 additions and 53 deletions

View File

@ -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
{

View File

@ -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__);

2
vendor/autoload.php vendored
View File

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

View File

@ -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;

View File

@ -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);
}

View File

@ -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')) {