Skip to content

Commit

Permalink
Make array access tolerant with isset
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedash95 committed Oct 3, 2022
1 parent 98ad726 commit 4c3f696
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php
Expand Up @@ -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 [];
}

Expand Down
Expand Up @@ -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<int, string>.',
308,
Expand Down Expand Up @@ -538,4 +534,9 @@ public function testBug8097(): void
$this->analyse([__DIR__ . '/data/bug-8097.php'], []);
}

public function testBug8068(): void
{
$this->analyse([__DIR__ . '/data/bug-8068.php'], []);
}

}
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-8068.php
@@ -0,0 +1,11 @@
<?php

class HelloWorld
{
public function test(string $url): bool
{
$urlParsed = parse_url($url);

return isset($urlParsed['path']);
}
}

0 comments on commit 4c3f696

Please sign in to comment.