|
| 1 | +<?php // lint >= 8.0 |
| 2 | + |
| 3 | +namespace ConditionalTypeForParameterTemplate; |
| 4 | + |
| 5 | +use stdClass; |
| 6 | +use function PHPStan\Testing\assertType; |
| 7 | + |
| 8 | +class A |
| 9 | +{ |
| 10 | + |
| 11 | + /** |
| 12 | + * @template T |
| 13 | + * @param T $result |
| 14 | + * @return ($result is false ? never : T) |
| 15 | + */ |
| 16 | + public function throwOnFailure($result) |
| 17 | + { |
| 18 | + if ($result === false) { |
| 19 | + throw new \Exception(); |
| 20 | + } |
| 21 | + |
| 22 | + return $result; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @template T |
| 27 | + * @param T $result |
| 28 | + * @return ($result is not false ? T : never) |
| 29 | + */ |
| 30 | + public function negated($result) |
| 31 | + { |
| 32 | + if ($result === false) { |
| 33 | + throw new \Exception(); |
| 34 | + } |
| 35 | + |
| 36 | + return $result; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @template T |
| 41 | + * @param T $a |
| 42 | + * @param T $b |
| 43 | + * @return ($a is false ? never : T) |
| 44 | + */ |
| 45 | + public function boundByTwoParameters($a, $b) |
| 46 | + { |
| 47 | + throw new \Exception(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @template T |
| 52 | + * @param T $a |
| 53 | + * @param list<T> $others |
| 54 | + * @return ($a is false ? never : T) |
| 55 | + */ |
| 56 | + public function boundByAnotherParameter($a, array $others) |
| 57 | + { |
| 58 | + throw new \Exception(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @template T |
| 63 | + * @param T|null $value |
| 64 | + * @return ($value is null ? never : T) |
| 65 | + */ |
| 66 | + public function nullable($value) |
| 67 | + { |
| 68 | + throw new \Exception(); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @template T |
| 73 | + * @param T $val |
| 74 | + * @return ($val is array ? T : null) |
| 75 | + */ |
| 76 | + public function validateArray(mixed $val): ?array |
| 77 | + { |
| 78 | + return \is_array($val) ? $val : null; |
| 79 | + } |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * @template T |
| 85 | + * @param T $result |
| 86 | + * @return ($result is false ? never : T) |
| 87 | + */ |
| 88 | +function throwOnFailure($result) |
| 89 | +{ |
| 90 | + if ($result === false) { |
| 91 | + throw new \Exception(); |
| 92 | + } |
| 93 | + |
| 94 | + return $result; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * @param stdClass|false $v |
| 99 | + * @param array{a: int}|string $as |
| 100 | + */ |
| 101 | +function test(A $a, $v, $as, mixed $m, ?stdClass $n): void |
| 102 | +{ |
| 103 | + assertType('stdClass', $a->throwOnFailure($v)); |
| 104 | + assertType('stdClass', throwOnFailure($v)); |
| 105 | + assertType('stdClass', $a->negated($v)); |
| 106 | + assertType("'str'|stdClass|false", $a->boundByTwoParameters($v, 'str')); |
| 107 | + assertType('stdClass|false', $a->boundByAnotherParameter($v, [])); |
| 108 | + assertType('stdClass', $a->nullable($n)); |
| 109 | + assertType('array|null', $a->validateArray($as)); |
| 110 | + |
| 111 | + foreach ($a->validateArray($m) ?? [] as $x) { |
| 112 | + assertType('mixed', $x); |
| 113 | + assertType('array|null', $a->validateArray($x)); |
| 114 | + } |
| 115 | +} |
0 commit comments