vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.55k stars 659 forks source link

Bug: Ternary operator nullability checking #5619

Open nzour opened 3 years ago

nzour commented 3 years ago

Simple idea

// pure functions here
function foo(): ?Foo { ... }
function bar(Foo $foo): mixed { ... }

$baz = foo() ? bar(foo()) : null;

Works as expected https://psalm.dev/r/25bc0f9e95

Simple example that works not correctly (on my opinion) when I use functions without objects and methods https://psalm.dev/r/08f7a4ae06

Does not work even inside class context https://psalm.dev/r/0984111f90

And finally, icing on the cake. Some strange things happen here https://psalm.dev/r/c11020ef40

psalm-github-bot[bot] commented 3 years ago

I found these snippets:

https://psalm.dev/r/25bc0f9e95 ```php getContext() ? mapContext($container->getContext()) : null; ``` ``` Psalm output (using commit f874740): No issues! ```
https://psalm.dev/r/08f7a4ae06 ```php
https://psalm.dev/r/0984111f90 ```php
https://psalm.dev/r/c11020ef40 ```php getContext() ? mapContext($container->getContext()) : null; } } /** * @psalm-immutable */ final class NotWorking { // the only difference is this property public null|array $ctx; // the error would not occure if I use default value for property // public null|array $ctx = []; public function __construct(Container $container) { $this->ctx = $container->getContext() ? mapContext($container->getContext()) : null; } } ``` ``` Psalm output (using commit f874740): ERROR: PossiblyNullArgument - 47:60 - Argument 1 of mapContext cannot be null, possibly null value provided ```