diff --git a/src/Application/ChangedNodeScopeRefresher.php b/src/Application/ChangedNodeScopeRefresher.php index 302a04fc602..7f8e8e672c3 100644 --- a/src/Application/ChangedNodeScopeRefresher.php +++ b/src/Application/ChangedNodeScopeRefresher.php @@ -3,8 +3,11 @@ declare (strict_types=1); namespace Rector\Application; +use PhpParser\Modifiers; use PhpParser\Node; use PhpParser\Node\ArrayItem; +use PhpParser\Node\ClosureUse; +use PhpParser\Node\DeclareItem; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\CallLike; @@ -15,13 +18,20 @@ use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\NullsafeMethodCall; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\FunctionLike; +use PhpParser\Node\PropertyItem; +use PhpParser\Node\StaticVar; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\ClassMethod; +use PhpParser\Node\Stmt\Declare_; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\If_; +use PhpParser\Node\Stmt\Property; +use PhpParser\Node\Stmt\Static_; use PhpParser\Node\Stmt\Switch_; use PhpParser\Node\Stmt\TryCatch; +use PhpParser\Node\Stmt\Use_; +use PhpParser\Node\UseItem; use PHPStan\Analyser\MutatingScope; use Rector\Exception\ShouldNotHappenException; use Rector\NodeAnalyzer\ScopeAnalyzer; @@ -91,9 +101,27 @@ final class ChangedNodeScopeRefresher if ($node instanceof Expr) { return [new Expression($node)]; } + // moved from Expr/Stmt to directly under Node on PHPParser 5 if ($node instanceof ArrayItem) { return [new Expression(new Array_([$node]))]; } + if ($node instanceof ClosureUse) { + $closure = new Closure(); + $closure->uses[] = $node; + return [new Expression($closure)]; + } + if ($node instanceof DeclareItem) { + return [new Declare_([$node])]; + } + if ($node instanceof PropertyItem) { + return [new Property(Modifiers::PUBLIC, [$node])]; + } + if ($node instanceof StaticVar) { + return [new Static_([$node])]; + } + if ($node instanceof UseItem) { + return [new Use_([$node])]; + } $errorMessage = \sprintf('Complete parent node of "%s" be a stmt.', \get_class($node)); throw new ShouldNotHappenException($errorMessage); } diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index eec8b95f82c..859c4852fa0 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 = '2.0.0-rc2'; + public const PACKAGE_VERSION = 'b5859be5c0af9089cc6fd2b007c24163db198334'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-12-03 20:06:47'; + public const RELEASE_DATE = '2024-12-05 10:00:50'; /** * @var int */ diff --git a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php index 619a4fdf3d8..aea13184951 100644 --- a/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php +++ b/src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php @@ -28,6 +28,7 @@ use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\StaticPropertyFetch; use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Expr\Yield_; use PhpParser\Node\Identifier; use PhpParser\Node\IntersectionType; use PhpParser\Node\Name; @@ -260,6 +261,9 @@ final class PHPStanNodeScopeResolver $this->processMatch($node, $mutatingScope); return; } + if ($node instanceof Yield_) { + $this->processYield($node, $mutatingScope); + } }; $this->nodeScopeResolverProcessNodes($stmts, $scope, $nodeCallback); $nodeTraverser = new NodeTraverser(); @@ -270,6 +274,15 @@ final class PHPStanNodeScopeResolver $nodeTraverser->traverse($stmts); return $stmts; } + private function processYield(Yield_ $yield, MutatingScope $mutatingScope) : void + { + if ($yield->key instanceof Expr) { + $yield->key->setAttribute(AttributeKey::SCOPE, $mutatingScope); + } + if ($yield->value instanceof Expr) { + $yield->value->setAttribute(AttributeKey::SCOPE, $mutatingScope); + } + } private function processMatch(Match_ $match, MutatingScope $mutatingScope) : void { $match->cond->setAttribute(AttributeKey::SCOPE, $mutatingScope);