diff --git a/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php b/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php index 8bdcff5319..9cbe8b4883 100644 --- a/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php @@ -174,6 +174,22 @@ public function testEnums(): void 'Match arm is unreachable because previous comparison is always true.', 77, ], + [ + 'Match arm comparison between MatchEnums\Foo and MatchEnums\Foo::ONE is always false.', + 85, + ], + [ + 'Match arm comparison between MatchEnums\Foo and MatchEnums\DifferentEnum::ONE is always false.', + 95, + ], + [ + 'Match arm comparison between MatchEnums\Foo and MatchEnums\Foo::ONE is always false.', + 104, + ], + [ + 'Match arm comparison between MatchEnums\Foo and MatchEnums\DifferentEnum::ONE is always false.', + 113, + ], ]); } diff --git a/tests/PHPStan/Rules/Comparison/data/match-enums.php b/tests/PHPStan/Rules/Comparison/data/match-enums.php index d96b958d59..43e765c552 100644 --- a/tests/PHPStan/Rules/Comparison/data/match-enums.php +++ b/tests/PHPStan/Rules/Comparison/data/match-enums.php @@ -78,4 +78,51 @@ public function doBaz(Foo $foo, Foo $bar): int }; } + public function doFoo2(Foo $foo): int + { + return match ($foo) { + Foo::ONE => 'one', + Foo::ONE => 'one2', + Foo::TWO => 'two', + Foo::THREE => 'three', + }; + } + + public function doFoo3(Foo $foo): int + { + return match ($foo) { + Foo::ONE => 'one', + DifferentEnum::ONE => 'one2', + Foo::TWO => 'two', + Foo::THREE => 'three', + }; + } + + public function doFoo4(Foo $foo): int + { + return match ($foo) { + Foo::ONE, Foo::ONE => 'one', + Foo::TWO => 'two', + Foo::THREE => 'three', + }; + } + + public function doFoo5(Foo $foo): int + { + return match ($foo) { + Foo::ONE, DifferentEnum::ONE => 'one', + Foo::TWO => 'two', + Foo::THREE => 'three', + }; + } + +} + +enum DifferentEnum: int +{ + + case ONE = 1; + case TWO = 2; + case THREE = 3; + } diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index ed69d28980..8877e53e31 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -2405,6 +2405,18 @@ public function testEnums(): void 'Parameter #1 $countryMap of method CallMethodInEnum\FooCall::helloArray() expects array<\'The Netherlands\'|\'United States\', bool>, array{true} given.', 70, ], + [ + 'Parameter #1 $one of method CallMethodInEnum\TestPassingEnums::requireOne() expects CallMethodInEnum\TestPassingEnums::ONE, $this(CallMethodInEnum\TestPassingEnums)&CallMethodInEnum\TestPassingEnums given.', + 91, + ], + [ + 'Parameter #1 $one of method CallMethodInEnum\TestPassingEnums::requireOne() expects CallMethodInEnum\TestPassingEnums::ONE, $this(CallMethodInEnum\TestPassingEnums)&CallMethodInEnum\TestPassingEnums given.', + 99, + ], + [ + 'Parameter #1 $one of method CallMethodInEnum\TestPassingEnums::requireOne() expects CallMethodInEnum\TestPassingEnums::ONE, $this(CallMethodInEnum\TestPassingEnums)&CallMethodInEnum\TestPassingEnums given.', + 106, + ], ]); } diff --git a/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php b/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php index 986fc854cb..5fc2439152 100644 --- a/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php +++ b/tests/PHPStan/Rules/Methods/data/call-method-in-enum.php @@ -70,3 +70,41 @@ function doFooArray() { $this->helloArray([true]); } } + +enum TestPassingEnums { + case ONE; + case TWO; + + /** + * @param self::ONE $one + * @return void + */ + public function requireOne(self $one): void + { + + } + + public function doFoo(): void + { + match ($this) { + self::ONE => $this->requireOne($this), + self::TWO => $this->requireOne($this), + }; + } + + public function doFoo2(): void + { + match ($this) { + self::ONE => $this->requireOne($this), + default => $this->requireOne($this), + }; + } + + public function doFoo3(): void + { + match ($this) { + self::TWO => $this->requireOne($this), + default => $this->requireOne($this), + }; + } +}