Skip to content

Commit

Permalink
Make array access on possible false tolerant with isset
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedash95 committed Oct 4, 2022
1 parent 8462fbd commit 20f044e
Show file tree
Hide file tree
Showing 4 changed files with 44 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.",
18,
],
]);
}

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

namespace Bug8097;

use Closure;

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 20f044e

Please sign in to comment.