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

Result of && is always false. #2870

Closed
aivchen opened this issue Jan 19, 2020 · 33 comments
Closed

Result of && is always false. #2870

aivchen opened this issue Jan 19, 2020 · 33 comments
Labels
Milestone

Comments

@aivchen
Copy link

aivchen commented Jan 19, 2020

Bug report

PHPStan shouldn't report any errors in this case because it can't predict what rand() returns.

Code snippet that reproduces the problem

https://phpstan.org/r/e7652aad-b990-4aa7-8b6e-eeed31adc036

Expected output

No errors.

@ondrejmirtes
Copy link
Member

Hi,
the problem is the in_array part: https://phpstan.org/r/7676c51e-adc9-4b12-b72f-866417d256cf

PHPStan correctly knows that in_array will return true, !in_array will return false, so the right part of the && condition will not even get executed.

If you have a more precise code sample with your problem, please post it.

@ondrejmirtes
Copy link
Member

Oh, I get it, in the first iteration, the array is empty. So PHPStan should not complain. Thanks.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Jan 19, 2020
@davidkudera
Copy link

Hi, I'm getting the same error for this code: https://phpstan.org/r/f90376e0-9612-4994-b515-0725a248e756

@ondrejmirtes
Copy link
Member

@davidkudera In your case PHPStan is right. At no point is $a value changed, so it's always true.

@davidkudera
Copy link

@ondrejmirtes Yeah, but at the first iteration $b is false. It resolves to true only for the next iterations.

@aivchen
Copy link
Author

aivchen commented Jan 23, 2020

@davidkudera PHPStan reports Left side of && is always true. It wants to say you that using $a variable in your loop is useless.

@ondrejmirtes
Copy link
Member

You can simplify the code without changing how it behaves: https://phpstan.org/r/aea94433-f31e-4d97-a668-25689ecb69d5

@davidkudera
Copy link

Oh, sorry, I should probably ask for new glasses... My problem is with "right" side of &&, this issue is about "result" and my simplified example is about "left" side.

I tried to simplify my actual code a bit less for the playground, but I'm unable to reproduce my error (Right side of && is always false) there. Also, the same file (copy-paste) shows no error in another project.

@ondrejmirtes
Copy link
Member

Isn't it about the treatPhpDocTypesAsCertain settings?

@davidkudera
Copy link

You're right. I have that error only when I set it to false. Here is how the code basically looks like https://phpstan.org/r/d5cad7f0-6501-4ea8-bc80-08e7d6ea2c29 (still works fine on the playground).

I'll remove that option from my config. Thank you

@ondrejmirtes
Copy link
Member

You should really reproduce it in the playground, I need it for a regression test case. I know about this problem, but I need more data.

@davidkudera
Copy link

Here you go: https://phpstan.org/r/bb5d6c90-4df9-4174-b16d-caee1296f944

I had to start from my minimal example and add more and more code. The last line I added was the culprit (the method call on line 23).

The workaround is also interesting - just flip lines 22 and 23 and it will work without any errors.

@ondrejmirtes
Copy link
Member

Perfect! This helps me tremendously!

@ondrejmirtes
Copy link
Member

Fixed, thank you! phpstan/phpstan-src@a086c3c

@aivchen
Copy link
Author

aivchen commented Jan 27, 2020

@ondrejmirtes you closed the issue, but the initial problem is still not solved.

@ondrejmirtes ondrejmirtes reopened this Jan 27, 2020
@ondrejmirtes
Copy link
Member

ondrejmirtes commented Jan 27, 2020

Sorry, @davidkudera’s issue is resolved, the one in OP is unrelated.

@eigan
Copy link

eigan commented Feb 21, 2020

@ondrejmirtes
Copy link
Member

@eigan Definitely not.

@SergeC
Copy link

SergeC commented Jul 13, 2020

Enabling "Strict rules" brings back this bug https://phpstan.org/r/e52e08c5-e090-4a07-b323-7ea9521fa07f

@SergeC
Copy link

SergeC commented Jul 13, 2020

An error above could be fixed with annotation

		/** @var string[] $arr */
		$arr=[];

But I believe it's wrong.

@phpstan-bot
Copy link
Contributor

@ondrejmirtes PHPStan now reports different result with your code snippet:

@@ @@
-6: Result of && is always false.
-6: Call to function in_array() with arguments 'test', array<int, 'test'> and true will always evaluate to true.
+6: Call to function in_array() with arguments 'test', array<int, 'test'> and true will always evaluate to true.
+6: Result of && is always false.
Full report
Line Error
6 Result of && is always false.
6 Result of && is always false.

@phpstan-bot
Copy link
Contributor

@davidkudera PHPStan now reports different result with your code snippet:

@@ @@
-18: Right side of && is always false.
+No errors

@phpstan-bot
Copy link
Contributor

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

@@ @@
-No errors
+13: While loop condition is always true.
Full report
Line Error
13 While loop condition is always true.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-18: Right side of && is always false.
+17: While loop condition is always true.
Full report
Line Error
17 While loop condition is always true.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-18: Right side of && is always false.
+-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

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

@@ @@
-11: Call to function in_array() with arguments 'a'|'b'|'c', array<int, 'a'|'b'|'c'> 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

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

@@ @@
-6: Result of && is always false.
-6: Call to function in_array() with arguments 'test', array<int, 'test'> and true will always evaluate to true.
+6: Call to function in_array() with arguments 'test', array<int<0, max>, 'test'> and true will always evaluate to true.
+6: Result of && is always false.
Full report
Line Error
6 Call to function in_array() with arguments 'test', array<int<0, max>, 'test'> and true will always evaluate to true.
6 Result of && is always false.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-11: Call to function in_array() with arguments 'a'|'b'|'c', array<int, 'a'|'b'|'c'> and true will always evaluate to true.
+11: Call to function in_array() with arguments 'a'|'b'|'c', array<int<0, max>, 'a'|'b'|'c'> and true will always evaluate to true.
Full report
Line Error
11 `Call to function in_array() with arguments 'a'

@phpstan-bot
Copy link
Contributor

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

@@ @@
-6: Result of && is always false.
+No errors

@phpstan-bot
Copy link
Contributor

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

@@ @@
-6: Result of && is always false.
-6: Call to function in_array() with arguments 'test', array<int, 'test'> and true will always evaluate to true.
+No errors

@phpstan-bot
Copy link
Contributor

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

@@ @@
-11: Call to function in_array() with arguments 'a'|'b'|'c', array<int, 'a'|'b'|'c'> 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

No branches or pull requests

6 participants