Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make array access on possible false tolerant with isset #1791

Merged
merged 3 commits into from Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
],
Comment on lines -114 to -117
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling closure or object (without ArrayAccess) causes a fatal error, so this case shouldn't be ignored.
https://3v4l.org/lFcCq

I think it might be better to have another condition to the type, whether isOffestAccessible->no throws an fatal error or not.
This might lead to improvements in issues like phpstan/phpstan#8388

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to open a new issue about this, comments on old merged PRs will get lost :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Done phpstan/phpstan#8393

[
'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
ahmedash95 marked this conversation as resolved.
Show resolved Hide resolved
{
public function test(string $url): bool
{
$urlParsed = parse_url($url);

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

public function test2(closure $closure): bool
ahmedash95 marked this conversation as resolved.
Show resolved Hide resolved
{
return isset($closure['path']);
}

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