psalm-plugin-symfony icon indicating copy to clipboard operation
psalm-plugin-symfony copied to clipboard

Container does not take has() into account

Open wouterj opened this issue 4 years ago • 0 comments

I have the following (rather ugly, but required) code:

        if ($this->container->has('wouterj_eloquent.initializer')) {
            $this->container->get('wouterj_eloquent.initializer')->initialize();
        }

This produces the following error with Psalm level 3 and this plugin (both latest version):

ERROR: PossiblyNullReference - src/WouterJEloquentBundle.php:44:68 - Cannot call method initialize on possibly null value (see https://psalm.dev/083)
            $this->container->get('wouterj_eloquent.initializer')->initialize();

I would expect this plugin to somehow take the has() call into account (and thus removing null from the union return type of get()). I tried digging into the source code to contribute a fix, but I'm a bit unsure how to maintain some sort of global state that is reset after the if statement is left.

wouterj avatar Apr 24 '21 11:04 wouterj