Skip to content

Commit

Permalink
Make array access tolerant with isset
Browse files Browse the repository at this point in the history
Make array access tolerant with isset

Fixes bug phpstan/phpstan#8068
  • Loading branch information
ahmedash95 committed Oct 4, 2022
1 parent 8462fbd commit 6fd272b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 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
7 changes: 1 addition & 6 deletions tests/PHPStan/Levels/data/arrayDimFetches-7.json
Expand Up @@ -28,10 +28,5 @@
"message": "Cannot access offset 'foo' on iterable<int|string, object>.",
"line": 58,
"ignorable": true
},
{
"message": "Cannot access offset 'foo' on iterable<int|string, object>.",
"line": 66,
"ignorable": true
}
]
]
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,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,
]
]);
}

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

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

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

public function test2(closure $closure): bool
{
return isset($closure['path']);
}

/**
* @param iterable<int|string, object> $iterable
*/
public function test3($iterable): bool
{
unset($iterable['path']);
}
}

0 comments on commit 6fd272b

Please sign in to comment.