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

Invalid array index detection #8222

Closed
theofidry opened this issue Oct 26, 2022 · 4 comments
Closed

Invalid array index detection #8222

theofidry opened this issue Oct 26, 2022 · 4 comments
Labels
Milestone

Comments

@theofidry
Copy link

Bug report

For some downstream code, I am constraining a value to be positive-int|0. In the upstream code, this comes from an array for which the only operations are appending a value ($array[] = $value) and removing a value (unset($array[$index])).

However PHPStan doesn't appear to like that very much as it infers int for the index instead of positive-int|0, which is what it is in this specific case where the index is never explicitly set.

Code snippet that reproduces the problem

https://phpstan.org/r/b27d9595-2fb0-4e53-bae6-66728a05a53c

Expected output

No error reported.

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

First PHPStan issue I believe so it works :)

@VincentLanglet
Copy link
Contributor

Might be worth to notice that there is no error reporter with array_push
https://phpstan.org/r/2cd5d1cb-2b4e-4faa-b8bd-8de66ddc4c81

Even for invalid code https://phpstan.org/r/feaf9de5-82fc-4fdc-8339-8052cf2e3554

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

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

@@ @@
-10: Property ValueCollection::$values (array<int<0, max>, string>) does not accept non-empty-array<int, string>.
+No errors

@phpstan-bot
Copy link
Contributor

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

@@ @@
-10: Property ValueCollection::$values (array<int<0, max>, string>) does not accept non-empty-array<int, string>.
+No errors

@github-actions
Copy link

github-actions bot commented May 5, 2023

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 May 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants