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

Call to function array_key_exists() with int and array will always evaluate to true #3766

Closed
ossinkine opened this issue Aug 23, 2020 · 7 comments
Labels
Milestone

Comments

@ossinkine
Copy link

Bug report

False-positive alerts when using array_key_exists function.

Code snippet that reproduces the problem

https://phpstan.org/r/ce4a9861-014d-4ccc-84db-0e8d9ab47e34

Expected output

Expecting no alerts

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Aug 23, 2020
@ossinkine
Copy link
Author

@ondrejmirtes Please help me find where the error is, which class look at

@ondrejmirtes
Copy link
Member

@ossinkine Check what's $foo here on line 14: https://phpstan.org/r/c44956e6-9bf4-4ae7-ac52-4f8c62579a38

I suspect it will be something like array&hasOffset(int) but that doesn't mean that any int is present there...

This error message comes from this class - https://github.com/phpstan/phpstan-src/blob/master/src/Rules/Comparison/ImpossibleCheckTypeHelper.php - if it returns true, it's always true, if it returns false, it's always false, if it returns null, nothing is reported.

So you can see for yourself what types are compared with isSuperTypeOf in this branch https://github.com/phpstan/phpstan-src/blob/e96481c3210c9aa93de45f482c11789c1face65d/src/Rules/Comparison/ImpossibleCheckTypeHelper.php#L173-L195 and if the result of isSuperTypeOf between those types makes sense or not.

@ossinkine
Copy link
Author

@ondrejmirtes Unfortunatelly I could not figure out with this. The code you pointed is executed 5 times in my test, $foo has the intersect of the following types:

  1. ArrayType and HasOffsetType with offset type ConstantIntegerType with value 1
  2. ArrayType and HasOffsetType with offset type ConstantIntegerType with value 2
  3. ArrayType and HasOffsetType with offset type UnionType of ConstantIntegerType with value 1 and ConstantIntegerType with value 2
  4. ArrayType and HasOffsetType with offset type UnionType of ConstantIntegerType with value 2 and ConstantIntegerType with value 3
  5. ArrayType and HasOffsetType with offset type IntegerType

As a result the array has any integer offset what should be wrong but I don't how to fix this.

@phpstan-bot
Copy link
Contributor

@ossinkine After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-14: Call to function array_key_exists() with int and array will always evaluate to true.
+No errors

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-15: Call to function array_key_exists() with int and array will always evaluate to true.
+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 Feb 13, 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