vimeo / psalm

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

Type inference from callback where template type is boolean #10793

Open trowski opened 8 months ago

trowski commented 8 months ago

I'm not exactly sure what's going on here, I'd expect the inferred return type from Cache::compute() to be boolean, but Psalm is saying it must be one of true or false.

https://psalm.dev/r/b8431d7429

psalm-github-bot[bot] commented 8 months ago

I found these snippets:

https://psalm.dev/r/b8431d7429 ```php $factory * @return T */ public function compute(string $key, callable $factory) { return $factory($key)->value; } } $cache = new Cache(); $cache->compute('key', function (): CacheResult { if (time() & 1) { return new CacheResult(true); } return new CacheResult(false); }); ``` ``` Psalm output (using commit 3600d51): ERROR: InvalidArgument - 25:9 - Incompatible types found for T (must have only one of false, true) ```