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 always-true with in_array #5354

Closed
VincentLanglet opened this issue Jul 21, 2021 · 15 comments
Closed

False positive always-true with in_array #5354

VincentLanglet opened this issue Jul 21, 2021 · 15 comments
Labels
Milestone

Comments

@VincentLanglet
Copy link
Contributor

I expect no error from the code
https://phpstan.org/r/179a9da2-e9e7-4d4b-b4af-b0e01c35a9db

In my project issue I get

Call to function in_array() with arguments 0, array<int, 0|1>&nonEmpty and true will always evaluate to true.

But even if the array is non-empty, the in_array check isn't supposed to be always true.

@ondrejmirtes ondrejmirtes transferred this issue from phpstan/phpstan-strict-rules Jul 21, 2021
@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Jul 21, 2021
@staabm
Copy link
Contributor

staabm commented Aug 1, 2021

@staabm
Copy link
Contributor

staabm commented Aug 1, 2021

hmm had a look into it. I don't know how to solve it, because

		$a = [];
		foreach ($foo as $e) {
			$a[] = rand(5, 15) > 10 ? 0 : 1;
		}

leads to $a typed with array<int, 0|1> and thats obviously contains always 0.
I feel like in this case there needs to be some kind of knowledge about the array, that the values are optionally contained.

this also makes me question, whether phpstan/phpstan-src#606 is a good thing, I implemented earlier today

@ondrejmirtes
Copy link
Member

in_array() is a problematic function. Here's where it's implemented:

Ideally all of this should be handled in InArrayFunctionTypeSpecifyingExtension. It would also fix phpstan/phpstan-webmozart-assert#142.

@VincentLanglet
Copy link
Contributor Author

The issue can be simplified to https://phpstan.org/r/02705e4a-3f03-4fa4-83c1-9e1ec86a76f5

leads to $a typed with array<int, 0|1> and thats obviously contains always 0.

array<int, 0|1> doesn't always contain 0 to me.

[1, 1, 1, 1] or [] are both example which are compatible to the array<int, 0|1> definition to me.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-15: Call to function in_array() with arguments 0, array<int, 0|1> and true will always evaluate to true.
+-1: Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument phpstan/phpstan#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 phpstan/phpstan#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

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

@@ @@
-10: Call to function in_array() with arguments 0, array<int, 0|1> and true will always evaluate to true.
+-1: Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument phpstan/phpstan#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 phpstan/phpstan#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

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

@@ @@
-15: Call to function in_array() with arguments 0, array<int, 0|1> and true will always evaluate to true.
+No errors

@phpstan-bot
Copy link
Contributor

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

@@ @@
-10: Call to function in_array() with arguments 0, array<int, 0|1> and true will always evaluate to true.
+No errors

@VincentLanglet
Copy link
Contributor Author

Seems fixed

1 similar comment
@VincentLanglet
Copy link
Contributor Author

Seems fixed

@ondrejmirtes
Copy link
Member

This was a bug in the playground, it's not fixed.

@olsavmic
Copy link

olsavmic commented Oct 8, 2021

@ondrejmirtes We're also dealing with this constant array union type issue (and the other false positive looks pretty dangerous). I'm not sure how to re-implement the rule in the context of InArrayFunctionTypeSpecifyingExtension. Can you give me a hint? I may try to look into it.

@ondrejmirtes
Copy link
Member

@olsavmic You can ask Scope for the same information in InArrayFunctionTypeSpecifyingExtension the ImpossibleCheckTypeHelper asks right now. That would fix phpstan/phpstan-webmozart-assert#142 (because that scenario uses InArrayFunctionTypeSpecifyingExtension indirectly and avoids ImpossibleCheckTypeHelper).

As for this specific issue, you need to come up with the logic that would fix it. in_array is a difficult beast. These subarticles will definitely help you get up to speed: https://phpstan.org/developing-extensions/core-concepts

@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.

5 participants