diff --git a/packages/NodeTypeResolver/Node/AttributeKey.php b/packages/NodeTypeResolver/Node/AttributeKey.php index 6bd3ea89d96..4caca4f2a75 100644 --- a/packages/NodeTypeResolver/Node/AttributeKey.php +++ b/packages/NodeTypeResolver/Node/AttributeKey.php @@ -192,6 +192,10 @@ final class AttributeKey * @var string */ public const IS_IN_LOOP = 'is_in_loop'; + /** + * @var string + */ + public const IS_VARIABLE_LOOP = 'is_variable_loop'; /** * @var string */ @@ -240,6 +244,10 @@ final class AttributeKey * @var string */ public const IS_ARG_VALUE = 'is_arg_value'; + /** + * @var string + */ + public const IS_PARAM_VAR = 'IS_PARAM_VAR'; /** * @var string */ diff --git a/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php b/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php index 590dc15da22..4479c01d259 100644 --- a/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php +++ b/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php @@ -11,6 +11,8 @@ use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\Isset_; +use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Param; use PhpParser\Node\Stmt\Break_; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Do_; @@ -53,12 +55,8 @@ final class ContextNodeVisitor extends NodeVisitorAbstract implements ScopeResol return null; } if ($node instanceof Attribute) { - $this->simpleCallableNodeTraverser->traverseNodesWithCallable($node->args, static function (Node $subNode) { - if ($subNode instanceof Array_) { - $subNode->setAttribute(AttributeKey::IS_ARRAY_IN_ATTRIBUTE, \true); - } - return null; - }); + $this->processContextInAttribute($node); + return null; } if ($node instanceof If_ || $node instanceof Else_ || $node instanceof ElseIf_) { $this->processContextInIf($node); @@ -70,8 +68,20 @@ final class ContextNodeVisitor extends NodeVisitorAbstract implements ScopeResol if ($node instanceof Arg) { $node->value->setAttribute(AttributeKey::IS_ARG_VALUE, \true); } + if ($node instanceof Param) { + $node->var->setAttribute(AttributeKey::IS_PARAM_VAR, \true); + } return null; } + private function processContextInAttribute(Attribute $attribute) : void + { + $this->simpleCallableNodeTraverser->traverseNodesWithCallable($attribute->args, static function (Node $subNode) { + if ($subNode instanceof Array_) { + $subNode->setAttribute(AttributeKey::IS_ARRAY_IN_ATTRIBUTE, \true); + } + return null; + }); + } /** * @param \PhpParser\Node\Expr\Isset_|\PhpParser\Node\Stmt\Unset_ $node */ @@ -103,6 +113,12 @@ final class ContextNodeVisitor extends NodeVisitorAbstract implements ScopeResol */ private function processContextInLoop($node) : void { + if ($node instanceof Foreach_) { + if ($node->keyVar instanceof Variable) { + $node->keyVar->setAttribute(AttributeKey::IS_VARIABLE_LOOP, \true); + } + $node->valueVar->setAttribute(AttributeKey::IS_VARIABLE_LOOP, \true); + } $this->simpleCallableNodeTraverser->traverseNodesWithCallable($node->stmts, static function (Node $subNode) : ?int { if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) { return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; diff --git a/rules/Php72/NodeFactory/AnonymousFunctionFactory.php b/rules/Php72/NodeFactory/AnonymousFunctionFactory.php index 2ea7df09bdb..c09f71b3a22 100644 --- a/rules/Php72/NodeFactory/AnonymousFunctionFactory.php +++ b/rules/Php72/NodeFactory/AnonymousFunctionFactory.php @@ -222,8 +222,7 @@ final class AnonymousFunctionFactory if (\in_array($variableName, $paramNames, \true)) { continue; } - $parentNode = $variable->getAttribute(AttributeKey::PARENT_NODE); - if ($parentNode instanceof Node && \in_array(\get_class($parentNode), [Assign::class, Foreach_::class, Param::class], \true)) { + if ($variable->getAttribute(AttributeKey::IS_BEING_ASSIGNED) === \true || $variable->getAttribute(AttributeKey::IS_PARAM_VAR) === \true || $variable->getAttribute(AttributeKey::IS_VARIABLE_LOOP) === \true) { $alreadyAssignedVariables[] = $variableName; } if (!$this->nodeNameResolver->isNames($variable, $alreadyAssignedVariables)) { diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index a79618e6a6d..27182979cb2 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = 'c7a7046b05c0cfba50aa245973f42a00009540ad'; + public const PACKAGE_VERSION = '3b6084dec770faeb7fca657b6f1ab6898a0801d5'; /** * @api * @var string */ - public const RELEASE_DATE = '2023-07-09 00:34:38'; + public const RELEASE_DATE = '2023-07-09 08:41:16'; /** * @var int */ diff --git a/src/PhpParser/Parser/SimplePhpParser.php b/src/PhpParser/Parser/SimplePhpParser.php index d0997621070..a9f590b56f5 100644 --- a/src/PhpParser/Parser/SimplePhpParser.php +++ b/src/PhpParser/Parser/SimplePhpParser.php @@ -8,6 +8,7 @@ use PhpParser\Node\Stmt; use PhpParser\Parser; use PhpParser\ParserFactory; use Rector\Core\PhpParser\NodeTraverser\NodeConnectingTraverser; +use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\AssignedToNodeVisitor; final class SimplePhpParser { /** @@ -25,6 +26,7 @@ final class SimplePhpParser $this->nodeConnectingTraverser = $nodeConnectingTraverser; $parserFactory = new ParserFactory(); $this->phpParser = $parserFactory->create(ParserFactory::PREFER_PHP7); + $this->nodeConnectingTraverser->addVisitor(new AssignedToNodeVisitor()); } /** * @api tests diff --git a/vendor/autoload.php b/vendor/autoload.php index 967a7af301e..3ca3e57f193 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) { require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInit696c37db7fa7b44637af01a6abf5a84e::getLoader(); +return ComposerAutoloaderInit15c88e4ebae2c19fc397915fb68d3e7c::getLoader(); diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index b7c91c10c65..ea3ee2783d1 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInit696c37db7fa7b44637af01a6abf5a84e +class ComposerAutoloaderInit15c88e4ebae2c19fc397915fb68d3e7c { private static $loader; @@ -22,17 +22,17 @@ class ComposerAutoloaderInit696c37db7fa7b44637af01a6abf5a84e return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInit696c37db7fa7b44637af01a6abf5a84e', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInit15c88e4ebae2c19fc397915fb68d3e7c', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); - spl_autoload_unregister(array('ComposerAutoloaderInit696c37db7fa7b44637af01a6abf5a84e', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInit15c88e4ebae2c19fc397915fb68d3e7c', 'loadClassLoader')); require __DIR__ . '/autoload_static.php'; - call_user_func(\Composer\Autoload\ComposerStaticInit696c37db7fa7b44637af01a6abf5a84e::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInit15c88e4ebae2c19fc397915fb68d3e7c::getInitializer($loader)); $loader->setClassMapAuthoritative(true); $loader->register(true); - $filesToLoad = \Composer\Autoload\ComposerStaticInit696c37db7fa7b44637af01a6abf5a84e::$files; + $filesToLoad = \Composer\Autoload\ComposerStaticInit15c88e4ebae2c19fc397915fb68d3e7c::$files; $requireFile = \Closure::bind(static function ($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 1f3ccac21e6..d282b08ce20 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInit696c37db7fa7b44637af01a6abf5a84e +class ComposerStaticInit15c88e4ebae2c19fc397915fb68d3e7c { public static $files = array ( 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', @@ -3077,9 +3077,9 @@ class ComposerStaticInit696c37db7fa7b44637af01a6abf5a84e public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInit696c37db7fa7b44637af01a6abf5a84e::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInit696c37db7fa7b44637af01a6abf5a84e::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInit696c37db7fa7b44637af01a6abf5a84e::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInit15c88e4ebae2c19fc397915fb68d3e7c::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInit15c88e4ebae2c19fc397915fb68d3e7c::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInit15c88e4ebae2c19fc397915fb68d3e7c::$classMap; }, null, ClassLoader::class); }