diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index aedf59ea20..c952da2e95 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -5609,11 +5609,24 @@ private function processAssignVar( } else { $varForSetOffsetValue = $var->var; } - $assignedPropertyExpr = new SetOffsetValueTypeExpr( - $varForSetOffsetValue, - $var->dim, - $assignedPropertyExpr, - ); + + if ( + $var === $originalVar + && $var->dim !== null + && $scope->hasExpressionType($var)->yes() + ) { + $assignedPropertyExpr = new SetExistingOffsetValueTypeExpr( + $varForSetOffsetValue, + $var->dim, + $assignedPropertyExpr, + ); + } else { + $assignedPropertyExpr = new SetOffsetValueTypeExpr( + $varForSetOffsetValue, + $var->dim, + $assignedPropertyExpr, + ); + } $dimFetchStack[] = $var; $var = $var->var; } diff --git a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php index 300a8a9858..4a12d0636a 100644 --- a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php @@ -935,4 +935,11 @@ public function testBug11777(): void $this->analyse([__DIR__ . '/data/bug-11777.php'], []); } + public function testBug13035(): void + { + $this->checkExplicitMixed = true; + $this->checkImplicitMixed = true; + $this->analyse([__DIR__ . '/data/bug-13035.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Properties/data/bug-13035.php b/tests/PHPStan/Rules/Properties/data/bug-13035.php new file mode 100644 index 0000000000..ccf048e704 --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/bug-13035.php @@ -0,0 +1,21 @@ + + */ + public array $list = []; + + public function bug(int $offset): void + { + if (isset($this->list[$offset])) { + $this->list[$offset] = 123; + } + } +}