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

Bug: Default Array keys aren't detected to be non-negative #8900

Closed
Leoniebra opened this issue Feb 16, 2023 · 6 comments
Closed

Bug: Default Array keys aren't detected to be non-negative #8900

Leoniebra opened this issue Feb 16, 2023 · 6 comments
Labels
Milestone

Comments

@Leoniebra
Copy link

Leoniebra commented Feb 16, 2023

Bug report

Hello!

I noticed that phpstan 1.9.17 treats the keys of arrays that are initialized empty and filled via $array[] = $value as possibly negative integers when using foreach to iterate through the array.
(For me it occurred with an array generated by the TYPO3 Extbase ObjectStorage ->toArray() method). This led to a match expression on which phpstan required more cases than I expected to be necessary.

Code snippet that reproduces the problem

I tried reproducing the issue here. Declaring the key within the foreaach loop as a positive-int was the best approach to a solution I found, sadly it didn't work either tho.

https://phpstan.org/r/1ee9ce43-4954-4188-9ac8-6f780f9223dd

Expected output

I expect this snippet to generate no issues, for I am sure that the keys of this array are always greater than or equal to 0.

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

It sure did! While discovering this issue, I also found a heap of correctly reported problems within our code. I'm creating this issue because I enjoy using this tool a lot :)

Thanks in advance everyone!

Best,
Leo (it / she)

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Feb 17, 2023
@Odachannnn
Copy link

Hi @Leoniebra.

I don't know why default array keys aren't detected to be non-negative,
but I found that the reason for positive-int didn't work.
Your PHPDocs starts with single asterisk, but double are needed.

https://phpstan.org/r/8b70b9bf-8644-4dfa-b0c1-67627bb2486a

@Leoniebra
Copy link
Author

Your PHPDocs starts with single asterisk, but double are needed.

OH! Thanks a bunch, that's a little embarassing!

@mad-briller
Copy link
Contributor

positive-int doesn't include 0 so you are best off doing 0|positive-int or int<0, max>

@phpstan-bot
Copy link
Contributor

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

@@ @@
-PHP 8.0 – 8.2 (1 error)
+PHP 8.0 – 8.2
 ==========
 
-10: Match expression does not handle remaining value: int<-3, -1>
+No errors
 
 PHP 7.2 – 7.4 (1 error)
 ==========
 
 11: Syntax error, unexpected T_DOUBLE_ARROW on line 11
Full report

PHP 8.0 – 8.2

No errors

PHP 7.2 – 7.4 (1 error)

Line Error
11 Syntax error, unexpected T_DOUBLE_ARROW on line 11

@phpstan-bot
Copy link
Contributor

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

@@ @@
-PHP 8.0 – 8.2 (1 error)
+PHP 8.0 – 8.2
 ==========
 
-10: Match expression does not handle remaining value: int<-3, -1>
+No errors
 
 PHP 7.2 – 7.4 (1 error)
 ==========
 
 11: Syntax error, unexpected T_DOUBLE_ARROW on line 11
Full report

PHP 8.0 – 8.2

No errors

PHP 7.2 – 7.4 (1 error)

Line Error
11 Syntax error, unexpected T_DOUBLE_ARROW on line 11

@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

5 participants