|
| 1 | +<?php // lint >= 8.1 |
| 2 | + |
| 3 | +namespace Bug8486; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +enum Operator: string |
| 8 | +{ |
| 9 | + case Foo = 'foo'; |
| 10 | + case Bar = 'bar'; |
| 11 | + case None = ''; |
| 12 | + |
| 13 | + public function explode(): void |
| 14 | + { |
| 15 | + $character = match ($this) { |
| 16 | + self::None => 'baz', |
| 17 | + default => $this->value, |
| 18 | + }; |
| 19 | + |
| 20 | + assertType("'bar'|'baz'|'foo'", $character); |
| 21 | + } |
| 22 | + |
| 23 | + public function typeInference(): void |
| 24 | + { |
| 25 | + match ($this) { |
| 26 | + self::None => 'baz', |
| 27 | + default => assertType('$this(Bug8486\Operator~Bug8486\Operator::None)', $this), |
| 28 | + }; |
| 29 | + } |
| 30 | + |
| 31 | + public function typeInference2(): void |
| 32 | + { |
| 33 | + if ($this === self::None) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + assertType("'Bar'|'Foo'", $this->name); |
| 38 | + assertType("'bar'|'foo'", $this->value); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +class Foo |
| 43 | +{ |
| 44 | + |
| 45 | + public function doFoo(Operator $operator) |
| 46 | + { |
| 47 | + $character = match ($operator) { |
| 48 | + Operator::None => 'baz', |
| 49 | + default => $operator->value, |
| 50 | + }; |
| 51 | + |
| 52 | + assertType("'bar'|'baz'|'foo'", $character); |
| 53 | + } |
| 54 | + |
| 55 | + public function typeInference(Operator $operator): void |
| 56 | + { |
| 57 | + match ($operator) { |
| 58 | + Operator::None => 'baz', |
| 59 | + default => assertType('Bug8486\Operator~Bug8486\Operator::None', $operator), |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + public function typeInference2(Operator $operator): void |
| 64 | + { |
| 65 | + if ($operator === Operator::None) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + assertType("'Bar'|'Foo'", $operator->name); |
| 70 | + assertType("'bar'|'foo'", $operator->value); |
| 71 | + } |
| 72 | + |
| 73 | + public function typeInference3(Operator $operator): void |
| 74 | + { |
| 75 | + if ($operator === Operator::None) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + if ($operator === Operator::Foo) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + assertType("Bug8486\Operator::Bar", $operator); |
| 84 | + assertType("'Bar'", $operator->name); |
| 85 | + assertType("'bar'", $operator->value); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments