From 29de8992f58ff11b9b77c14b7507e03c6f53e3f7 Mon Sep 17 00:00:00 2001 From: Ahmed Ashraf Date: Tue, 4 Oct 2022 00:19:35 +0200 Subject: [PATCH] Make array access tolerant with isset Make array access tolerant with isset Fixes bug https://github.com/phpstan/phpstan/issues/8068 --- .../NonexistentOffsetInArrayDimFetchRule.php | 6 ++++- .../Levels/data/arrayDimFetches-7.json | 7 +----- ...nexistentOffsetInArrayDimFetchRuleTest.php | 14 +++++++---- tests/PHPStan/Rules/Arrays/data/bug-8068.php | 24 +++++++++++++++++++ 4 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 tests/PHPStan/Rules/Arrays/data/bug-8068.php 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/Levels/data/arrayDimFetches-7.json b/tests/PHPStan/Levels/data/arrayDimFetches-7.json index 50ce04c3f5..3b315d40bf 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 013481de42..36105d3779 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 0000000000..970f714b83 --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/data/bug-8068.php @@ -0,0 +1,24 @@ + $iterable + */ + public function test3($iterable): bool + { + unset($iterable['path']); + } +}