diff --git a/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php b/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php index cee6554562..0bd1a80605 100644 --- a/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php +++ b/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php @@ -56,11 +56,7 @@ public function processNode(Node $node, Scope $scope): array $isOffsetAccessible = $isOffsetAccessibleType->isOffsetAccessible(); - if ($scope->isInExpressionAssign($node) && $isOffsetAccessible->yes()) { - return []; - } - - if ($scope->isUndefinedExpressionAllowed($node) && !$isOffsetAccessible->no()) { + if (($scope->isInExpressionAssign($node) || $scope->isUndefinedExpressionAllowed($node)) && $isOffsetAccessible->yes()) { return []; } diff --git a/tests/PHPStan/Levels/data/arrayDimFetches-7.json b/tests/PHPStan/Levels/data/arrayDimFetches-7.json index 3b315d40bf..50ce04c3f5 100644 --- a/tests/PHPStan/Levels/data/arrayDimFetches-7.json +++ b/tests/PHPStan/Levels/data/arrayDimFetches-7.json @@ -28,5 +28,10 @@ "message": "Cannot access offset 'foo' on iterable.", "line": 58, "ignorable": true + }, + { + "message": "Cannot access offset 'foo' on iterable.", + "line": 66, + "ignorable": true } -] +] \ No newline at end of file diff --git a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php index cc2dca6fbf..013481de42 100644 --- a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php +++ b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php @@ -111,6 +111,10 @@ 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, @@ -534,14 +538,4 @@ public function testBug8097(): void $this->analyse([__DIR__ . '/data/bug-8097.php'], []); } - public function testBug8068(): void - { - $this->analyse([__DIR__ . '/data/bug-8068.php'], [ - [ - "Cannot access offset 'path' on Closure.", - 18, - ], - ]); - } - } diff --git a/tests/PHPStan/Rules/Arrays/data/bug-8068.php b/tests/PHPStan/Rules/Arrays/data/bug-8068.php deleted file mode 100644 index 96380586ef..0000000000 --- a/tests/PHPStan/Rules/Arrays/data/bug-8068.php +++ /dev/null @@ -1,28 +0,0 @@ - $iterable - */ - public function test3($iterable): bool - { - unset($iterable['path']); - } -}