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

Strict comparison using === between 4 and 4 will always evaluate to true. #4242

Closed
SergeC opened this issue Dec 16, 2020 · 7 comments · Fixed by phpstan/phpstan-src#2105
Closed
Labels
Milestone

Comments

@SergeC
Copy link

SergeC commented Dec 16, 2020

Bug report

I have a code in my project like in case1 in a code sample which triggers the following error. In case2 is the same code snippet with sorted elseif branches.

Code snippet that reproduces the problem

https://phpstan.org/r/1b70ab7b-24e9-4507-bb77-c175607bf78f

Expected output

The case1 example must work without an error.

@SergeC
Copy link
Author

SergeC commented Dec 16, 2020

The case1 will not trigger any errors if following branch removed:

else {
		return;
		}

@ondrejmirtes
Copy link
Member

Both of the examples should report an error. Here are the inferred types: https://phpstan.org/r/74a325e0-0502-44d8-b536-5b348d14ec97

I don't know why the in_array() call on line 105 does not report an error, but it should.

If you're not interested in "always true" errors, you can turn them off. Either by stopping using phpstan-strict-rules, or by setting these settings off: https://phpstan.org/config-reference#checkalwaystruechecktypefunctioncall

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Dec 16, 2020
@SergeC
Copy link
Author

SergeC commented Dec 16, 2020

@ondrejmirtes Can you please provide more details on why both cases should report an error and how it works? I believe there is no always true condition. The $data->getType() statement always returns 1 since it passed with construct $data=(new Data(Enum::TYPE_A));. In your example the last \PHPStan\dumpType($data->getType()); statement returns Dumped type: 7|8|9|100 but $data->getType() always returns 1.

@ondrejmirtes
Copy link
Member

@SergeC The method is annotated as getType(): int. There's no generics involved.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-62: Strict comparison using === between 4 and 4 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

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

@@ @@
- 53: Dumped type: 1|2|3|4|7|8|9|100
- 63: Strict comparison using === between 4 and 4 will always evaluate to true.
- 86: Dumped type: 1|2|3|4|7|8|9|100
-103: Dumped type: 7|8|9|100
+ -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

@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 10, 2023
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.

3 participants