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 0 and int<1, max> will always evaluate to false. #2889
Comments
This bug report is missing a link to reproduction on phpstan.org. |
Funny enough, if you make your Otherwise you can work around it using local variables: /** @var User */
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
/** @var null|UserProduct */
$userProduct = $em->getRepository(UserProduct::class)->findOneByUserAndProduct($user, $product);
if (!$userProduct) {
throw new BadRequestHttpException();
}
$count = $userProduct->getCount();
if ($count < 1) {
throw new BadRequestHttpException();
}
// decreases the products
$userProduct->setCount($userProduct->getCount() - 1); // <--- here I am decreasing!
if (0 === $userProduct->getCount()) {
$em->remove($userProduct);
} |
@ondrejmirtes Why is this issue closed? You suggest a workaround but the issue still exists. Why the PHPStan memorizes the method result? |
It memorizes it so it could detect: assert($userProduct->getCount() === 0);
assert($userProduct->getCount() === 0); // always true I'm aware of this issue, it's definitely a duplicate but I didn't spend the time looking for the issue ID. |
It depends on function getCount(): int
{
static $count;
return ++$count;
} There is no rules which declare the some methods must be idempotent, right? |
Currently not. All methods are memorized, except for void methods. Void methods also invalidate previously memorized calls. |
So is there the way to disable this behavior? BTW void method does not invalidate the memorized calls: https://phpstan.org/r/51fd580d-ac38-4e82-ad22-a65c407da902 |
#1157 is this related issue? |
This is something of a pest to be honest, I've run into this multiple times in various forms. The assumption that a method with a return value is pure is unreliable in many cases. |
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. |
Bug report
Hello, phpstan is reporting a false positive here:
The error is this:
Strict comparison using === between 0 and int<1, max> will always evaluate to false.
And the check is
if (0 === $userProduct->getCount())
. It's correct because I am decreasing the value.Code snippet that reproduces the problem
Expected output
The text was updated successfully, but these errors were encountered: