From 93eac043d6d795cacc60ec964771147e63f43751 Mon Sep 17 00:00:00 2001 From: ondrejmirtes <104888+ondrejmirtes@users.noreply.github.com> Date: Mon, 9 Mar 2026 16:04:56 +0000 Subject: [PATCH 1/2] Fix assignment in key of array destructuring being ignored - Propagate scope from key expression processing in list/array destructuring - Previously the scope result from processing key expressions was discarded, so assignments like `[($a = 'foo') => $b] = [...]` left `$a` undefined - New regression test in tests/PHPStan/Analyser/nsrt/bug-14019.php Closes https://github.com/phpstan/phpstan/issues/14019 --- src/Analyser/ExprHandler/AssignHandler.php | 2 +- tests/PHPStan/Analyser/nsrt/bug-14019.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Analyser/nsrt/bug-14019.php diff --git a/src/Analyser/ExprHandler/AssignHandler.php b/src/Analyser/ExprHandler/AssignHandler.php index 8fd6a455ea8..89b53c46c23 100644 --- a/src/Analyser/ExprHandler/AssignHandler.php +++ b/src/Analyser/ExprHandler/AssignHandler.php @@ -688,7 +688,7 @@ public function processAssignVar( $throwPoints = array_merge($throwPoints, $keyResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $keyResult->getImpurePoints()); $isAlwaysTerminating = $isAlwaysTerminating || $keyResult->isAlwaysTerminating(); - // no need for $keyResult->getScope() + $scope = $keyResult->getScope(); } if ($arrayItem->key === null) { diff --git a/tests/PHPStan/Analyser/nsrt/bug-14019.php b/tests/PHPStan/Analyser/nsrt/bug-14019.php new file mode 100644 index 00000000000..407fe643af7 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-14019.php @@ -0,0 +1,10 @@ + $b] = ['foo' => 1]; + +assertType("'foo'", $a); +assertType('1', $b); From bb991b8f10346b0829aca503f7f8438578c3a35d Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 9 Mar 2026 17:11:17 +0100 Subject: [PATCH 2/2] Update DefinedVariableRuleTest.php --- .../Rules/Variables/DefinedVariableRuleTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php index d217d5010df..2dde59f1423 100644 --- a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php +++ b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php @@ -1393,6 +1393,16 @@ public function testBug6830(): void $this->analyse([__DIR__ . '/data/bug-6830.php'], []); } + public function testBug14019(): void + { + $this->cliArgumentsVariablesRegistered = true; + $this->polluteScopeWithLoopInitialAssignments = false; + $this->checkMaybeUndefinedVariables = true; + $this->polluteScopeWithAlwaysIterableForeach = true; + + $this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-14019.php'], []); + } + public function testBug14117(): void { $this->cliArgumentsVariablesRegistered = true;