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

False positive with when using in_array with literals (or constants) #5496

Closed
faizanakram99 opened this issue Aug 20, 2021 · 5 comments
Closed
Labels
Milestone

Comments

@faizanakram99
Copy link

faizanakram99 commented Aug 20, 2021

Bug report

latest version (reproduced at phpstan.org)

See line 22 of attached snippet, the array is missing 'auto' value yet phpstan reports that \in_array('auto', $propagation, true) will always return true (at line 18)

Code snippet that reproduces the problem

https://phpstan.org/r/00e38865-bbc2-4373-8655-e2f1a3c6c14a

Expected output

No errors

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

Indeed. Thanks for the great tool.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Aug 21, 2021
@staabm
Copy link
Contributor

staabm commented Sep 11, 2021

let me share my current observations:

phpstan runs into this return true case, which leads to the assumption that the value will always be true:

https://github.com/phpstan/phpstan-src/blob/8cc6e7e03b105124686ad4707d57ef818d456347/src/Rules/Comparison/ImpossibleCheckTypeHelper.php#L201

this effectively means UnionType->isSuperTypeOf(ConstantStringType) or
pseudocode: 'auto'|'copy'->isSuperTypeOf('auto')
returns Trinary::yes()

the way to solve this issue is to make sure we actually return null from within https://github.com/phpstan/phpstan-src/blob/8cc6e7e03b105124686ad4707d57ef818d456347/src/Rules/Comparison/ImpossibleCheckTypeHelper.php#L85-L134 so phpstan returns early from findSpecifiedType

@phpstan-bot
Copy link
Contributor

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

@@ @@
-18: Call to function in_array() with arguments 'auto', array<string, 'auto'|'copy'> and true will always evaluate to true.
+-1: Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40
+Run PHPStan with --debug option and post the stack trace to:
+https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
Full report
Line Error
-1 Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40Run PHPStan with --debug option and post the stack trace to:https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md

@phpstan-bot
Copy link
Contributor

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

@@ @@
-18: Call to function in_array() with arguments 'auto', array<string, 'auto'|'copy'> and true will always evaluate to true.
+No errors

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@4321374

@github-actions
Copy link

github-actions bot commented Apr 3, 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 Apr 3, 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.

4 participants