vimeo / psalm

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

Detect exhaustive matches/switches #11123

Open jnvsor opened 1 week ago

jnvsor commented 1 week ago

https://psalm.dev/r/8b57f1708d

Exhaustive matches and switches are not detected properly, causing PropertyNotSetInConstructor in this example

psalm-github-bot[bot] commented 1 week ago

I found these snippets:

https://psalm.dev/r/8b57f1708d ```php y = $y; } } class Y extends X { public function __construct(string $y) { if ($y === 'a' || $y === 'b' || $y === 'c') { parent::__construct($y); } else { throw new InvalidArgumentException(); } } } class Z extends X { public function __construct(string $y) { switch ($y) { case 'a': case 'b': case 'c': parent::__construct($y); return; default: throw new InvalidArgumentException(); } } } class ZZ extends X { public function __construct(string $y) { match ($y) { 'a', 'b', 'c' => parent::__construct($y), default => throw new InvalidArgumentException() }; } } ``` ``` Psalm output (using commit 03ee02c): ERROR: PropertyNotSetInConstructor - 23:7 - Property Z::$y is not defined in constructor of Z or in any methods called in the constructor ERROR: PropertyNotSetInConstructor - 38:7 - Property ZZ::$y is not defined in constructor of ZZ or in any methods called in the constructor ```