diff --git a/src/Analyser/ExprHandler/NewHandler.php b/src/Analyser/ExprHandler/NewHandler.php index 9e83f7c474..76b0126376 100644 --- a/src/Analyser/ExprHandler/NewHandler.php +++ b/src/Analyser/ExprHandler/NewHandler.php @@ -190,7 +190,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $constructorResult = $node; }, StatementContext::createTopLevel()); if ($constructorResult !== null) { - $throwPoints = array_map(static fn (ThrowPoint $point) => InternalThrowPoint::createFromPublic($point), $constructorResult->getStatementResult()->getThrowPoints()); + $throwPoints = array_map(static fn (ThrowPoint $point): InternalThrowPoint => InternalThrowPoint::createFromPublic($point, $scope), $constructorResult->getStatementResult()->getThrowPoints()); $impurePoints = $constructorResult->getImpurePoints(); } } else { diff --git a/src/Analyser/InternalThrowPoint.php b/src/Analyser/InternalThrowPoint.php index 92291bd5ac..a50c4bd042 100644 --- a/src/Analyser/InternalThrowPoint.php +++ b/src/Analyser/InternalThrowPoint.php @@ -3,7 +3,6 @@ namespace PHPStan\Analyser; use PhpParser\Node; -use PHPStan\ShouldNotHappenException; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; @@ -50,13 +49,8 @@ public static function createImplicit(MutatingScope $scope, Node $node): self return new self($scope, new ObjectType(Throwable::class), $node, explicit: false, canContainAnyThrowable: true); } - public static function createFromPublic(ThrowPoint $throwPoint): self + public static function createFromPublic(ThrowPoint $throwPoint, MutatingScope $scope): self { - $scope = $throwPoint->getScope(); - if (!$scope instanceof MutatingScope) { - throw new ShouldNotHappenException(); - } - return new self($scope, $throwPoint->getType(), $throwPoint->getNode(), $throwPoint->isExplicit(), $throwPoint->canContainAnyThrowable()); } diff --git a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php index add6c66beb..076dfc65f9 100644 --- a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php +++ b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php @@ -1425,6 +1425,16 @@ public function testBug12373(): void $this->analyse([__DIR__ . '/data/bug-12373.php'], []); } + public function testBug13920(): void + { + $this->cliArgumentsVariablesRegistered = true; + $this->polluteScopeWithLoopInitialAssignments = true; + $this->checkMaybeUndefinedVariables = true; + $this->polluteScopeWithAlwaysIterableForeach = true; + + $this->analyse([__DIR__ . '/data/bug-13920.php'], []); + } + public function testBug14117(): void { $this->cliArgumentsVariablesRegistered = true; diff --git a/tests/PHPStan/Rules/Variables/data/bug-13920.php b/tests/PHPStan/Rules/Variables/data/bug-13920.php new file mode 100644 index 0000000000..e21d129248 --- /dev/null +++ b/tests/PHPStan/Rules/Variables/data/bug-13920.php @@ -0,0 +1,26 @@ +