|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Bug15431Nsrt; |
| 4 | + |
| 5 | +use stdClass; |
| 6 | +use function PHPStan\Testing\assertType; |
| 7 | + |
| 8 | +/** |
| 9 | + * @template T of array<mixed> |
| 10 | + */ |
| 11 | +class Box |
| 12 | +{ |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * @template TKey of object|array<mixed> |
| 17 | + * @template TValue of object|array<mixed> |
| 18 | + */ |
| 19 | +class Map |
| 20 | +{ |
| 21 | + |
| 22 | + /** |
| 23 | + * @template T of TKey|TValue |
| 24 | + * @param (T is object ? T : Box<T>) $value |
| 25 | + * @return T |
| 26 | + */ |
| 27 | + protected function unbox(object $value) |
| 28 | + { |
| 29 | + assertType('Bug15431Nsrt\Box<T of TKey of array<mixed> (class Bug15431Nsrt\Map, argument)|TValue of array<mixed> (class Bug15431Nsrt\Map, argument) (method Bug15431Nsrt\Map::unbox(), argument)>|(T of TKey of object (class Bug15431Nsrt\Map, argument)|TValue of object (class Bug15431Nsrt\Map, argument) (method Bug15431Nsrt\Map::unbox(), argument))', $value); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @param (TValue is object ? TValue : Box<TValue>) $v |
| 34 | + * @param Box<TValue&array<mixed>> $b |
| 35 | + * @param TValue&object $o |
| 36 | + */ |
| 37 | + public function doFoo($v, $b, $o): void |
| 38 | + { |
| 39 | + assertType('TValue of array<mixed> (class Bug15431Nsrt\Map, argument)|TValue of object (class Bug15431Nsrt\Map, argument)', $this->unbox($v)); |
| 40 | + assertType('TValue of array<mixed> (class Bug15431Nsrt\Map, argument)', $this->unbox($b)); |
| 41 | + assertType('TValue of object (class Bug15431Nsrt\Map, argument)', $this->unbox($o)); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param (TKey&array<mixed>)|(TValue&array<mixed>) $arrays |
| 46 | + * @param (TKey&list<mixed>)|(TValue&list<mixed>) $lists |
| 47 | + */ |
| 48 | + public function doBar($arrays, $lists): void |
| 49 | + { |
| 50 | + assertType('TKey of array<mixed> (class Bug15431Nsrt\Map, argument)|TValue of array<mixed> (class Bug15431Nsrt\Map, argument)', $arrays); |
| 51 | + assertType('TKey of list<mixed> (class Bug15431Nsrt\Map, argument)|TValue of list<mixed> (class Bug15431Nsrt\Map, argument)', $lists); |
| 52 | + } |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * @template TA of array{a: int} |
| 58 | + * @template TB of array{a: int} |
| 59 | + * @template TC of array<string, int> |
| 60 | + * @template TD of list<int> |
| 61 | + * @param TA|TB $ab |
| 62 | + * @param TC|TD $cd |
| 63 | + * @param TA|array<int, string> $mixed |
| 64 | + */ |
| 65 | +function constantArrays($ab, $cd, $mixed): void |
| 66 | +{ |
| 67 | + assertType('TA of array{a: int} (function Bug15431Nsrt\constantArrays(), argument)|TB of array{a: int} (function Bug15431Nsrt\constantArrays(), argument)', $ab); |
| 68 | + assertType('TC of array<string, int> (function Bug15431Nsrt\constantArrays(), argument)|TD of list<int> (function Bug15431Nsrt\constantArrays(), argument)', $cd); |
| 69 | + assertType('array<int, string>|TA of array{a: int} (function Bug15431Nsrt\constantArrays(), argument)', $mixed); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * @template T of object|array<mixed> |
| 74 | + * @param T|Box<T> $value |
| 75 | + * @return T |
| 76 | + */ |
| 77 | +function unboxNaked($value) |
| 78 | +{ |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * @template T of object|array<mixed> |
| 83 | + * @param (T is object ? T : Box<T>) $value |
| 84 | + * @return T |
| 85 | + */ |
| 86 | +function unboxConditional(object $value) |
| 87 | +{ |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * @template T |
| 92 | + * @param T|array<T> $value |
| 93 | + * @return T |
| 94 | + */ |
| 95 | +function unwrapArray($value) |
| 96 | +{ |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * @param stdClass|Box<array{int}> $a |
| 101 | + * @param int|array<string> $b |
| 102 | + * @param array<string> $c |
| 103 | + */ |
| 104 | +function doBaz($a, $b, $c): void |
| 105 | +{ |
| 106 | + assertType('array{int}|stdClass', unboxNaked($a)); |
| 107 | + assertType('array{int}|stdClass', unboxConditional($a)); |
| 108 | + assertType('int|string', unwrapArray($b)); |
| 109 | + assertType('string', unwrapArray($c)); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * @template TA of iterable<mixed> |
| 114 | + * @template TB of iterable<int> |
| 115 | + * @param TA $a |
| 116 | + * @param TB $b |
| 117 | + */ |
| 118 | +function iterables($a, $b): void |
| 119 | +{ |
| 120 | + $c = rand(0, 1) === 1 ? $a : $b; |
| 121 | + assertType('TA of iterable (function Bug15431Nsrt\iterables(), argument)|TB of iterable<int> (function Bug15431Nsrt\iterables(), argument)', $c); |
| 122 | +} |
0 commit comments