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

array_key_exists false positive #6181

Closed
Mika- opened this issue Dec 10, 2021 · 7 comments
Closed

array_key_exists false positive #6181

Mika- opened this issue Dec 10, 2021 · 7 comments
Labels
Milestone

Comments

@Mika-
Copy link

Mika- commented Dec 10, 2021

Bug report

array_key_exists result seems to get cached when calling multiple times and treatPhpDocTypesAsCertain is false.

Code snippet that reproduces the problem

https://phpstan.org/r/b188b057-febf-4611-8d1b-2dec775d1892

Expected output

No errors

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Dec 10, 2021
@herndlm
Copy link
Contributor

herndlm commented Dec 10, 2021

Hi, I had a quick look and saw that the constants are the "problem". BUT I think it's working as expected, as you're relying on static which is unsafe. Try either making the class final or use self:: to access the constants. See https://phpstan.org/r/52898302-552d-4fa9-b45b-ade25cb8c7e6

@Mika-
Copy link
Author

Mika- commented Dec 10, 2021

That seems to be the case. Static constant reference being resolved as mixed and then being evaluated twice the same way results as false positive.
Maybe this kind of cases should be silently ignored as happens when treatPhpDocTypesAsCertain is enabled.

@herndlm
Copy link
Contributor

herndlm commented Dec 12, 2021

@Mika- the remaining error is coming from StrictComparisonOfDifferentTypesRule which is enabled with level 4 => it would be off on 3. But I guess it's not in your interest to go down to 3 :)

Anyways, do you agree that this is not a bug then? I think it behaves as expected.

@ondrejmirtes
Copy link
Member

You should use self::KEY_VERSION etc. but this is still a bug.

@herndlm
Copy link
Contributor

herndlm commented Dec 12, 2021

Aah I think I got it, was thinking too much about types only. The constants are different of course and should not influence each other..

@phpstan-bot
Copy link
Contributor

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

@@ @@
-17: Strict comparison using === between false and true will always evaluate to false.
+No errors

@github-actions
Copy link

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 Aug 27, 2022
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