Skip to content

Commit 6fbd6e4

Browse files
committed
NonexistentOffsetInArrayDimFetchRule - do not report errors on expressions after truthy isset check
1 parent b73602e commit 6fbd6e4

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

Diff for: src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public function processNode(Node $node, Scope $scope): array
5454
return $isOffsetAccessibleTypeResult->getUnknownClassErrors();
5555
}
5656

57+
if ($scope->hasExpressionType($node)->yes()) {
58+
return [];
59+
}
60+
5761
$isOffsetAccessible = $isOffsetAccessibleType->isOffsetAccessible();
5862

5963
if ($scope->isInExpressionAssign($node) && $isOffsetAccessible->yes()) {
@@ -87,7 +91,7 @@ public function processNode(Node $node, Scope $scope): array
8791
return [];
8892
}
8993

90-
if ($dimType === null || $scope->hasExpressionType($node)->yes()) {
94+
if ($dimType === null) {
9195
return [];
9296
}
9397

Diff for: tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -552,14 +552,6 @@ public function testBug7229(): void
552552
'Cannot access offset string on mixed.',
553553
24,
554554
],
555-
[
556-
'Cannot access offset string on mixed.',
557-
29,
558-
],
559-
[
560-
'Cannot access offset string on mixed.',
561-
32,
562-
],
563555
]);
564556
}
565557

@@ -729,4 +721,15 @@ public function testBug6605(): void
729721
]);
730722
}
731723

724+
public function testBug9991(): void
725+
{
726+
$this->checkExplicitMixed = true;
727+
$this->analyse([__DIR__ . '/data/bug-9991.php'], [
728+
[
729+
'Cannot access offset \'title\' on mixed.',
730+
9,
731+
],
732+
]);
733+
}
734+
732735
}

Diff for: tests/PHPStan/Rules/Arrays/data/bug-9991.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Bug9991;
4+
5+
function (): void {
6+
$data = json_decode(file_get_contents('') ?: '', flags: JSON_THROW_ON_ERROR | JSON_OBJECT_AS_ARRAY);
7+
8+
if (
9+
isset($data['title'])
10+
&& is_string($data['title'])
11+
) {
12+
echo $data['title'];
13+
}
14+
};

0 commit comments

Comments
 (0)