Error Identifier: conditionalType.alwaysTrue
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
/**
* @return ($i is int ? non-empty-array : array)
*/
function fill(int $i): array
{
return [];
}
Why is it reported? #
The condition in a conditional return type PHPDoc tag is always true. In the example above, $i is declared as int in the native type, so the condition $i is int is always satisfied. This makes the conditional return type pointless because only the “true” branch will ever apply, and the condition adds unnecessary complexity without providing any type narrowing benefit.
How to fix it #
Remove the conditional return type and use the “true” branch directly:
<?php declare(strict_types = 1);
/**
- * @return ($i is int ? non-empty-array : array)
+ * @return non-empty-array
*/
function fill(int $i): array
{
return [];
}
Or widen the parameter type so the condition becomes meaningful:
<?php declare(strict_types = 1);
/**
* @return ($i is int ? non-empty-array : array)
*/
-function fill(int $i): array
+function fill(int|string $i): array
{
return [];
}
How to ignore this error #
You can use the identifier conditionalType.alwaysTrue to ignore this error using a comment:
// @phpstan-ignore conditionalType.alwaysTrue
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: conditionalType.alwaysTrue