diff --git a/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php b/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php index 0bd1a80605..cee6554562 100644 --- a/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php +++ b/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php @@ -56,7 +56,11 @@ public function processNode(Node $node, Scope $scope): array $isOffsetAccessible = $isOffsetAccessibleType->isOffsetAccessible(); - if (($scope->isInExpressionAssign($node) || $scope->isUndefinedExpressionAllowed($node)) && $isOffsetAccessible->yes()) { + if ($scope->isInExpressionAssign($node) && $isOffsetAccessible->yes()) { + return []; + } + + if ($scope->isUndefinedExpressionAllowed($node) && !$isOffsetAccessible->no()) { return []; } diff --git a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php index 013481de42..06b51e5492 100644 --- a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php +++ b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php @@ -111,10 +111,6 @@ public function testRule(): void 'Cannot access offset \'a\' on Closure(): void.', 253, ], - [ - 'Cannot access offset \'a\' on array{a: 1, b: 1}|(Closure(): void).', - 258, - ], [ 'Offset string does not exist on array.', 308, @@ -538,4 +534,9 @@ public function testBug8097(): void $this->analyse([__DIR__ . '/data/bug-8097.php'], []); } + public function testBug8068(): void + { + $this->analyse([__DIR__ . '/data/bug-8068.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Arrays/data/bug-8068.php b/tests/PHPStan/Rules/Arrays/data/bug-8068.php new file mode 100644 index 0000000000..3c60c68a30 --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/data/bug-8068.php @@ -0,0 +1,11 @@ +