psalm / psalm-plugin-symfony

Psalm Plugin for Symfony
MIT License
228 stars 53 forks source link

Typing from the parameter bag #345

Closed fldv closed 6 months ago

fldv commented 6 months ago

Hello

I am faced with a parameter typing problem arising from the bag parameter.

I declared a boolean type parameter in the parameters.yaml file :

param: '%env(bool:MY_ENV_PARAM)%'

Below, the code from a controller which aims to retrieve a parameter :

$param = $this->parameterBag->get('param');
assert(is_bool($param));`

Following the execution of Psalm, here is the error generated :

ERROR: TypeDoesNotContainType - src/Contoller/MyController.php:150:10 - Type string for $param is never bool (see https://psalm.dev/056)
                assert(is_bool($param));

The problem is that it tells me that the type of $param is string, but I declared it as a boolean. My goal is to delete my assert, and for Psalm to correctly detect the type. I inspected the code, the ParameterBagHandler handler is loaded correctly. Only in my App_KernelDevDebugContainer.xml, my parameter is in this format:

<parameter key="param">%env(bool:MY_ENV_PARAM)%</parameter>

In the handler ParameterBagHandler, the parameter type is tested with the gettype method, which does not work because the environment variables are not resolved, and the result is the same as stated in the configuration file.

What is tested is:

gettype('%env(bool:MY_ENV_PARAM)%')

instead of :

gettype(true)

If it helps, here is my Psalm setup:

<plugins>
<pluginClass xmlns="https://getpsalm.org/schema/config" class="Psalm\SymfonyPsalmPlugin\Plugin">
    <containerXml>var/cache/dev/App_KernelDevDebugContainer.xml</containerXml>
</pluginClass>
</plugins>

<extraFiles>
   <directory name="var/cache/dev/Symfony/Config" />
</extraFiles>

<stubs>
   <file name="vendor/symfony/dependency-injection/Loader/Configurator/ContainerConfigurator.php" />
</stubs>

Do you have any idea, I'm doing something wrong ? Thanks

seferov commented 5 months ago

@fldv thanks for the report. Could you please check it with v5.2.2?

fldv commented 5 months ago

@fldv thanks for the report. Could you please check it with v5.2.2?

Thanks for the fix and for the feedback. Tested and approved