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

Another "Cannot unset offset on array" false positive #7417

Closed
alexpott opened this issue Jun 7, 2022 · 5 comments · Fixed by phpstan/phpstan-src#1596
Closed

Another "Cannot unset offset on array" false positive #7417

alexpott opened this issue Jun 7, 2022 · 5 comments · Fixed by phpstan/phpstan-src#1596
Labels
Milestone

Comments

@alexpott
Copy link

alexpott commented Jun 7, 2022

Bug report

Code snippet that reproduces the problem

/**
 * Reads configuration data from the storage.
 * @return array|bool
 */
function readThing() {
  return ['module' => ['test' => 0]];
}

$extensions = readThing();
$extensions['theme']['test_basetheme'] = 0;
// This is the important part of the test. Themes are ordered alphabetically
// in core.extension so this will come before it's base theme.
$extensions['theme']['test_subtheme'] = 0;
$extensions['theme']['test_subsubtheme'] = 0;
unset($extensions['theme']['test_basetheme']);
unset($extensions['theme']['test_subsubtheme']);
unset($extensions['theme']['test_subtheme']);

Expected: No errors
Results in:

Cannot unset offset 'test_basetheme' on array{test_subtheme: 0, test_subsubtheme: 0}.

https://phpstan.org/r/33cd311d-fd19-430a-b3f5-945174682f2a

Did PHPStan help you today? Did it make you happy in any way?

PHPStan made me happy because bit by bit we're using it to improve Drupal. Thanks for all the work on it!

@alexpott
Copy link
Author

alexpott commented Jun 7, 2022

Might be related to #4565

@alexpott
Copy link
Author

alexpott commented Jun 7, 2022

Note that the @return array|bool is what is important here. If this is only @return array the code passes. Yes you could argue that we should be checking the return type of readThing(); before using it but this is from a test and the test knows that this is going to be an array.

@alexpott
Copy link
Author

alexpott commented Jun 7, 2022

Also note that:

  • adding $extensions = readThing(); just before the unsets also results in no errors.
  • removing unset($extensions['theme']['test_basetheme']); results in no errors.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Jun 14, 2022
@phpstan-bot
Copy link
Contributor

@alexpott After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
-17: Cannot unset offset 'test_basetheme' on array{test_subtheme: 0, test_subsubtheme: 0}.
+No errors

@github-actions
Copy link

github-actions bot commented Sep 7, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants