Description
Symfony version(s) affected
5.4.22
Description
When typecast env-var processors (e.g. bool:
) are used together with default
, the expression may evaluate to null
.
When OPTIONAL_ENV_VAR
is undefined in the following example, the default:
processor will assign null
to the result, and the bool:
processor will pass it through, even though the documentation states the return type is bool
. This is a problem when the value is passed as an non-nullable argument to a strictly-typed class.
\EnvironmentService:
arguments:
$boolFlag: '%env(bool:default::OPTIONAL_ENV_VAR)%'
This is not indented and the processor should be strictly non-nullable, as per the initial discussion here symfony/symfony-docs#18283 .
How to reproduce
Pass the following as an argument to a Symfony service. Do not set the value of OPTIONAL_ENV_VAR
.
final class EnvironmentService
{
public function __construct(
private bool $boolFlag,
) {}
}
# services.yaml
\EnvironmentService:
arguments:
$boolFlag: '%env(bool:default::OPTIONAL_ENV_VAR)%'
Symfony container build will fail due to null
being passed as the $boolFlag
which has to be a bool
.
Possible Solution
No response
Additional Context
No response