Right side of && is always true #4477
-
Hi, So before I go to create an issue, I have a few reports like this in our codebase (we just started setting up). Given the example below: $rand = mt_rand(0, 100);
$varA = in_array(1, [$rand]);
$varB = false;
if ($varA) {
$varB = true;
}
if ($varA && $varB) { // Here occurs "Right side of && is always true"
echo 'ok';
} The last check shows the error "Right side of && is always true". But to me $varB is not always true and can be false as well? This is just a reproducing example, actual code is not like this ;). Maybe I am missing something, please tell me! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think you're hitting the "dependent variables" feature that was added in 0.12.64: https://github.com/phpstan/phpstan/releases/tag/0.12.64 In your example, So having |
Beta Was this translation helpful? Give feedback.
I think you're hitting the "dependent variables" feature that was added in 0.12.64: https://github.com/phpstan/phpstan/releases/tag/0.12.64
In your example,
$varB
will only ever be true when$varA
is true. So if you check for truthiness of$varA
, it means that you don't have to check for$varB
because it can only ever be true too.So having
if ($varA)
is sufficient. See: https://phpstan.org/r/cd4aa27a-7bf4-4f97-adc6-2f60246da018