vimeo / psalm

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

Type of templated object depends on order of the parameters in constructor #7602

Open dsech opened 2 years ago

dsech commented 2 years ago

This works ok: https://psalm.dev/r/7855aed71f But if the constructor parameters are reversed, then it's not ok anymore: https://psalm.dev/r/d5cb90384d

I expect in both scenarios to not have issues. The problem exists in versions >=4.9.0

Also, cannot reproduce anymore if type of $someParam is ?string, so maybe that conditional type is part of the fault.

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

I found these snippets:

https://psalm.dev/r/7855aed71f ```php */ public static function type1(): self { return new self(self::TYPE1, 'some-string'); } } ``` ``` Psalm output (using commit faad966): No issues! ```
https://psalm.dev/r/d5cb90384d ```php */ public static function type1(): self { return new self('some-string', self::TYPE1); } } ``` ``` Psalm output (using commit faad966): INFO: MixedReturnTypeCoercion - 23:16 - The type 'SomeTemplatedClass<'type1'|mixed>' is more general than the declared return type 'SomeTemplatedClass<'type1'>' for SomeTemplatedClass::type1 INFO: MixedReturnTypeCoercion - 20:16 - The declared return type 'SomeTemplatedClass<'type1'>' for SomeTemplatedClass::type1 is more specific than the inferred return type 'SomeTemplatedClass<'type1'|mixed>' ```
orklah commented 2 years ago

Note: it has nothing to do with the order of the parameters in the docblock itself. Only the order of params in the signature matters.