Skip to content

Commit 44cb256

Browse files
authored
enforceEnumMatch: update docs with latest PHPStan behaviour (#158)
1 parent 414ba47 commit 44cb256

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ enum MyEnum: string { // missing @implements tag
204204

205205
### enforceEnumMatchRule
206206
- Enforces usage of `match ($enum)` instead of exhaustive conditions like `if ($enum === Enum::One) elseif ($enum === Enum::Two)`
207-
- This rule aims to "fix" a bit problematic behaviour of PHPStan (introduced at 1.10). It understands enum cases very well and forces you to adjust following code:
207+
- This rule aims to "fix" a bit problematic behaviour of PHPStan (introduced at 1.10.0 and fixed in [1.10.34](https://github.com/phpstan/phpstan-src/commit/fc7c0283176e5dc3867ade26ac835ee7f52599a9)). It understands enum cases very well and forces you to adjust following code:
208208
```php
209209
enum MyEnum {
210210
case Foo;
@@ -213,7 +213,7 @@ enum MyEnum {
213213

214214
if ($enum === MyEnum::Foo) {
215215
// ...
216-
} elseif ($enum === MyEnum::Bar) { // always true reported by phpstan
216+
} elseif ($enum === MyEnum::Bar) { // always true reported by phpstan (for versions 1.10.0 - 1.10.34)
217217
// ...
218218
} else {
219219
throw new LogicException('Unknown case'); // phpstan knows it cannot happen
@@ -241,6 +241,9 @@ Very good approach within similar cases is to use `match` construct so that (ide
241241
PHPStan even adds tip about `match` in those cases since `1.10.11`.
242242
For those reasons, this rule detects any always-true/false enum comparisons and forces you to rewrite it to `match ($enum)`.
243243

244+
Since PHPStan [1.10.34](https://github.com/phpstan/phpstan-src/commit/fc7c0283176e5dc3867ade26ac835ee7f52599a9), the behaviour is much better as it does not report error on the last elseif in case that it is followed by else with thrown exception.
245+
Such case raises exception in your tests if you add new enum case, but it is [still silent in PHPStan](https://phpstan.org/r/a4fdc0ab-5d1e-4f38-80ab-8da2e71a6205). This leaves space for error being deployed to production.
246+
So we still believe this rule makes sense even in latest PHPStan.
244247

245248
### enforceIteratorToArrayPreserveKeys
246249
- Enforces presence of second parameter in [iterator_to_array](https://www.php.net/manual/en/function.iterator-to-array.php) call (`$preserve_keys`) as the default value `true` is generally dangerous (risk of data loss / failure)

tests/Rule/data/EnforceEnumMatchRule/code.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ public function basicExhaustive(): int
4040
}
4141
}
4242

43+
public function basicExhaustiveWithSafetyException(): int
44+
{
45+
if ($this === self::Three) {
46+
return -1;
47+
} elseif ($this === self::Two) {
48+
return 0;
49+
} elseif ($this === self::One) { // error: This condition contains always-true enum comparison of EnforceEnumMatchRule\SomeEnum::One. Use match expression instead, PHPStan will report unhandled enum cases
50+
return 1;
51+
} else {
52+
throw new \LogicException('cannot happen');
53+
}
54+
}
55+
4356
public function notExhaustiveWithNegatedConditionInLastElseif(): int
4457
{
4558
if ($this === self::Three) {

0 commit comments

Comments
 (0)