Skip to content

Commit

Permalink
Various enum tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 3, 2024
1 parent f546c37 commit 344c21f
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]);
}

Expand Down
47 changes: 47 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/match-enums.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]);
}

Expand Down
38 changes: 38 additions & 0 deletions tests/PHPStan/Rules/Methods/data/call-method-in-enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
}
}

0 comments on commit 344c21f

Please sign in to comment.