diff --git a/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php b/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php index 0bd1a806059..cee6554562d 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/Levels/data/arrayDimFetches-7.json b/tests/PHPStan/Levels/data/arrayDimFetches-7.json index 50ce04c3f54..3b315d40bfc 100644 --- a/tests/PHPStan/Levels/data/arrayDimFetches-7.json +++ b/tests/PHPStan/Levels/data/arrayDimFetches-7.json @@ -28,10 +28,5 @@ "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 013481de421..95be2ea9047 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,14 @@ 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.", + 14, + ] + ]); + } + } 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 00000000000..970f714b838 --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/data/bug-8068.php @@ -0,0 +1,24 @@ + $iterable + */ + public function test3($iterable): bool + { + unset($iterable['path']); + } +}