|
| 1 | +<?php // lint >= 8.0 |
| 2 | + |
| 3 | +namespace Bug15433; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertNativeType; |
| 6 | +use function PHPStan\Testing\assertType; |
| 7 | + |
| 8 | +class Value |
| 9 | +{ |
| 10 | + |
| 11 | + public mixed $value; |
| 12 | + |
| 13 | + /** @phpstan-pure */ |
| 14 | + public function getValue(): mixed |
| 15 | + { |
| 16 | + return $this->value; |
| 17 | + } |
| 18 | + |
| 19 | + /** @phpstan-pure */ |
| 20 | + public static function getStaticValue(mixed $value): mixed |
| 21 | + { |
| 22 | + return $value; |
| 23 | + } |
| 24 | + |
| 25 | +} |
| 26 | + |
| 27 | +/** @phpstan-pure */ |
| 28 | +function getValue(mixed $value): mixed |
| 29 | +{ |
| 30 | + return $value; |
| 31 | +} |
| 32 | + |
| 33 | +function normalize(Value $object, mixed $value): void |
| 34 | +{ |
| 35 | + $matched = match (true) { |
| 36 | + is_array($object->value) => $object->value, |
| 37 | + default => [$object->value], |
| 38 | + }; |
| 39 | + assertType('array<mixed>', $matched); |
| 40 | + assertNativeType('array<mixed>', $matched); |
| 41 | + |
| 42 | + $reversed = match (true) { |
| 43 | + !is_array($object->value) => [$object->value], |
| 44 | + default => $object->value, |
| 45 | + }; |
| 46 | + assertType('array<mixed>', $reversed); |
| 47 | + assertNativeType('array<mixed>', $reversed); |
| 48 | + |
| 49 | + $integer = match (true) { |
| 50 | + is_int($object->value) => $object->value, |
| 51 | + default => 0, |
| 52 | + }; |
| 53 | + assertType('int', $integer); |
| 54 | + assertNativeType('int', $integer); |
| 55 | + |
| 56 | + $variable = match (true) { |
| 57 | + is_array($value) => $value, |
| 58 | + default => [$value], |
| 59 | + }; |
| 60 | + assertType('array<mixed>', $variable); |
| 61 | + assertNativeType('array<mixed>', $variable); |
| 62 | +} |
| 63 | + |
| 64 | +function normalizeCalls(Value $object, mixed $value): void |
| 65 | +{ |
| 66 | + $method = match (true) { |
| 67 | + is_array($object->getValue()) => $object->getValue(), |
| 68 | + default => [$object->getValue()], |
| 69 | + }; |
| 70 | + assertType('array<mixed>', $method); |
| 71 | + assertNativeType('array<mixed>', $method); |
| 72 | + |
| 73 | + $staticMethod = match (true) { |
| 74 | + is_array(Value::getStaticValue($value)) => Value::getStaticValue($value), |
| 75 | + default => [Value::getStaticValue($value)], |
| 76 | + }; |
| 77 | + assertType('array<mixed>', $staticMethod); |
| 78 | + assertNativeType('array<mixed>', $staticMethod); |
| 79 | + |
| 80 | + $function = match (true) { |
| 81 | + is_array(getValue($value)) => getValue($value), |
| 82 | + default => [getValue($value)], |
| 83 | + }; |
| 84 | + assertType('array<mixed>', $function); |
| 85 | + assertNativeType('array<mixed>', $function); |
| 86 | +} |
0 commit comments