Skip to content

Commit

Permalink
NonexistentOffsetInArrayDimFetchRule - do not report errors on expres…
Browse files Browse the repository at this point in the history
…sions after truthy `isset` check
  • Loading branch information
ondrejmirtes committed Oct 8, 2023
1 parent b73602e commit 6fbd6e4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -87,7 +91,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if ($dimType === null || $scope->hasExpressionType($node)->yes()) {
if ($dimType === null) {
return [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]);
}

Expand Down Expand Up @@ -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,
],
]);
}

}
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-9991.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Bug9991;

function (): void {
$data = json_decode(file_get_contents('') ?: '', flags: JSON_THROW_ON_ERROR | JSON_OBJECT_AS_ARRAY);

if (
isset($data['title'])
&& is_string($data['title'])
) {
echo $data['title'];
}
};

0 comments on commit 6fbd6e4

Please sign in to comment.