website/errors/if.alwaysTrue.md
<?php declare(strict_types = 1);
function doFoo(): void
{
$x = 5;
if ($x) {
echo 'always reached';
}
}
The if condition is always true based on the types and values PHPStan has inferred at that point in the code. This means the if branch will always execute, making the condition redundant. This usually points to an unnecessary check, a logic error, or a misunderstanding of the types involved.
In the example above, $x is always 5 (truthy), so the condition is always satisfied.
Remove the redundant condition if the check is unnecessary:
<?php declare(strict_types = 1);
function doFoo(): void
{
- $x = 5;
- if ($x) {
- echo 'always reached';
- }
+ echo 'always reached';
}
If the condition was meant to distinguish between different cases, fix the condition to check what was actually intended:
<?php declare(strict_types = 1);
-function doFoo(): void
+function doFoo(int $x): void
{
- $x = 5;
- if ($x) {
- echo 'always reached';
+ if ($x > 0) {
+ echo 'positive';
}
}