From 6fbd6e48b86b995b7ab118e0575c747fe224165b Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sun, 8 Oct 2023 13:56:24 +0200 Subject: [PATCH] NonexistentOffsetInArrayDimFetchRule - do not report errors on expressions after truthy `isset` check --- .../NonexistentOffsetInArrayDimFetchRule.php | 6 +++++- ...nexistentOffsetInArrayDimFetchRuleTest.php | 19 +++++++++++-------- tests/PHPStan/Rules/Arrays/data/bug-9991.php | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 tests/PHPStan/Rules/Arrays/data/bug-9991.php diff --git a/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php b/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php index e444bc5268..9cb8f8f4a5 100644 --- a/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php +++ b/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php @@ -54,6 +54,10 @@ public function processNode(Node $node, Scope $scope): array return $isOffsetAccessibleTypeResult->getUnknownClassErrors(); } + if ($scope->hasExpressionType($node)->yes()) { + return []; + } + $isOffsetAccessible = $isOffsetAccessibleType->isOffsetAccessible(); if ($scope->isInExpressionAssign($node) && $isOffsetAccessible->yes()) { @@ -87,7 +91,7 @@ public function processNode(Node $node, Scope $scope): array return []; } - if ($dimType === null || $scope->hasExpressionType($node)->yes()) { + if ($dimType === null) { return []; } diff --git a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php index 193605cb2c..e629152005 100644 --- a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php +++ b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php @@ -552,14 +552,6 @@ public function testBug7229(): void 'Cannot access offset string on mixed.', 24, ], - [ - 'Cannot access offset string on mixed.', - 29, - ], - [ - 'Cannot access offset string on mixed.', - 32, - ], ]); } @@ -729,4 +721,15 @@ public function testBug6605(): void ]); } + public function testBug9991(): void + { + $this->checkExplicitMixed = true; + $this->analyse([__DIR__ . '/data/bug-9991.php'], [ + [ + 'Cannot access offset \'title\' on mixed.', + 9, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Arrays/data/bug-9991.php b/tests/PHPStan/Rules/Arrays/data/bug-9991.php new file mode 100644 index 0000000000..8a18ed364c --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/data/bug-9991.php @@ -0,0 +1,14 @@ +