Skip to content

Commit 344c21f

Browse files
committed
Various enum tests
1 parent f546c37 commit 344c21f

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,22 @@ public function testEnums(): void
174174
'Match arm is unreachable because previous comparison is always true.',
175175
77,
176176
],
177+
[
178+
'Match arm comparison between MatchEnums\Foo and MatchEnums\Foo::ONE is always false.',
179+
85,
180+
],
181+
[
182+
'Match arm comparison between MatchEnums\Foo and MatchEnums\DifferentEnum::ONE is always false.',
183+
95,
184+
],
185+
[
186+
'Match arm comparison between MatchEnums\Foo and MatchEnums\Foo::ONE is always false.',
187+
104,
188+
],
189+
[
190+
'Match arm comparison between MatchEnums\Foo and MatchEnums\DifferentEnum::ONE is always false.',
191+
113,
192+
],
177193
]);
178194
}
179195

tests/PHPStan/Rules/Comparison/data/match-enums.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,51 @@ public function doBaz(Foo $foo, Foo $bar): int
7878
};
7979
}
8080

81+
public function doFoo2(Foo $foo): int
82+
{
83+
return match ($foo) {
84+
Foo::ONE => 'one',
85+
Foo::ONE => 'one2',
86+
Foo::TWO => 'two',
87+
Foo::THREE => 'three',
88+
};
89+
}
90+
91+
public function doFoo3(Foo $foo): int
92+
{
93+
return match ($foo) {
94+
Foo::ONE => 'one',
95+
DifferentEnum::ONE => 'one2',
96+
Foo::TWO => 'two',
97+
Foo::THREE => 'three',
98+
};
99+
}
100+
101+
public function doFoo4(Foo $foo): int
102+
{
103+
return match ($foo) {
104+
Foo::ONE, Foo::ONE => 'one',
105+
Foo::TWO => 'two',
106+
Foo::THREE => 'three',
107+
};
108+
}
109+
110+
public function doFoo5(Foo $foo): int
111+
{
112+
return match ($foo) {
113+
Foo::ONE, DifferentEnum::ONE => 'one',
114+
Foo::TWO => 'two',
115+
Foo::THREE => 'three',
116+
};
117+
}
118+
119+
}
120+
121+
enum DifferentEnum: int
122+
{
123+
124+
case ONE = 1;
125+
case TWO = 2;
126+
case THREE = 3;
127+
81128
}

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,18 @@ public function testEnums(): void
24052405
'Parameter #1 $countryMap of method CallMethodInEnum\FooCall::helloArray() expects array<\'The Netherlands\'|\'United States\', bool>, array{true} given.',
24062406
70,
24072407
],
2408+
[
2409+
'Parameter #1 $one of method CallMethodInEnum\TestPassingEnums::requireOne() expects CallMethodInEnum\TestPassingEnums::ONE, $this(CallMethodInEnum\TestPassingEnums)&CallMethodInEnum\TestPassingEnums given.',
2410+
91,
2411+
],
2412+
[
2413+
'Parameter #1 $one of method CallMethodInEnum\TestPassingEnums::requireOne() expects CallMethodInEnum\TestPassingEnums::ONE, $this(CallMethodInEnum\TestPassingEnums)&CallMethodInEnum\TestPassingEnums given.',
2414+
99,
2415+
],
2416+
[
2417+
'Parameter #1 $one of method CallMethodInEnum\TestPassingEnums::requireOne() expects CallMethodInEnum\TestPassingEnums::ONE, $this(CallMethodInEnum\TestPassingEnums)&CallMethodInEnum\TestPassingEnums given.',
2418+
106,
2419+
],
24082420
]);
24092421
}
24102422

tests/PHPStan/Rules/Methods/data/call-method-in-enum.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,41 @@ function doFooArray() {
7070
$this->helloArray([true]);
7171
}
7272
}
73+
74+
enum TestPassingEnums {
75+
case ONE;
76+
case TWO;
77+
78+
/**
79+
* @param self::ONE $one
80+
* @return void
81+
*/
82+
public function requireOne(self $one): void
83+
{
84+
85+
}
86+
87+
public function doFoo(): void
88+
{
89+
match ($this) {
90+
self::ONE => $this->requireOne($this),
91+
self::TWO => $this->requireOne($this),
92+
};
93+
}
94+
95+
public function doFoo2(): void
96+
{
97+
match ($this) {
98+
self::ONE => $this->requireOne($this),
99+
default => $this->requireOne($this),
100+
};
101+
}
102+
103+
public function doFoo3(): void
104+
{
105+
match ($this) {
106+
self::TWO => $this->requireOne($this),
107+
default => $this->requireOne($this),
108+
};
109+
}
110+
}

0 commit comments

Comments
 (0)