Skip to content

[DependencyInjection] Warn when not using bool environment processor on bool argument together with #[Autowire(env: 'ENV')] #53918

@ruudk

Description

@ruudk

Description

We had an interesting issue today where we deployed the following code:

final class ServiceName
{
    public function __construct(
        #[Autowire(env: 'SANDBOX')]
        public bool $sandbox,
    ) {}
}

And set the following in our .env:

SANDBOX=false

The $sandbox property in ServiceName was set to true.

It took us a while to understand what went wrong here. Because false is seen as true-ish, it became true.

We missed the bool environment processor. Changing it to #[Autowire(env: 'bool:SANDBOX')] solved it. So it was just a mistake on our end.

But that lead me to wonder: why can't Symfony warn me about this? Given that I use Autowire, it already knows that I'm targeting a boolean property. It would be great if it would say something like:

Warning: You should use the `bool` environment processor for `$sandbox` in `ServiceName`.

If you think this is a valid idea, what would be the best location to implement this check?
Somewhere around here?

foreach ($attributes as $attribute) {
$attribute = $attribute->newInstance();

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions