|
| 1 | +<?php // lint >= 8.1 |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace Bug15406; |
| 6 | + |
| 7 | +use function PHPStan\Testing\assertType; |
| 8 | + |
| 9 | +final class Node |
| 10 | +{ |
| 11 | + /** @param list<self> $children */ |
| 12 | + public function __construct(public readonly array $children, public readonly int $value) {} |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * @template T |
| 17 | + * @template U |
| 18 | + * |
| 19 | + * @param list<T> $list |
| 20 | + * @param callable(T): iterable<U> $fn |
| 21 | + * |
| 22 | + * @return list<U> |
| 23 | + */ |
| 24 | +function collect(array $list, callable $fn): array |
| 25 | +{ |
| 26 | + $result = []; |
| 27 | + foreach ($list as $value) { |
| 28 | + foreach ($fn($value) as $item) { |
| 29 | + $result[] = $item; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return $result; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * @param list<Node> $nodes |
| 38 | + */ |
| 39 | +function twoLevels(array $nodes): void |
| 40 | +{ |
| 41 | + assertType('list<int>', collect($nodes, static fn (Node $node): array |
| 42 | + => collect($node->children, static fn (Node $child): array => [$child->value]), |
| 43 | + )); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * @param list<Node> $nodes |
| 48 | + */ |
| 49 | +function threeLevels(array $nodes): void |
| 50 | +{ |
| 51 | + assertType('list<int>', collect($nodes, static fn (Node $node): array |
| 52 | + => collect($node->children, static fn (Node $child): array |
| 53 | + => collect($child->children, static fn (Node $grandChild): array => [$grandChild->value]), |
| 54 | + ), |
| 55 | + )); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * @param list<Node> $nodes |
| 60 | + */ |
| 61 | +function threeLevelsGenerators(array $nodes): void |
| 62 | +{ |
| 63 | + assertType('list<int>', collect($nodes, static fn (Node $node): iterable |
| 64 | + => yield from collect($node->children, static fn (Node $child): iterable |
| 65 | + => yield from collect($child->children, static fn (Node $grandChild): iterable => yield $grandChild->value), |
| 66 | + ), |
| 67 | + )); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * @param list<Node> $nodes |
| 72 | + */ |
| 73 | +function threeLevelsClosures(array $nodes): void |
| 74 | +{ |
| 75 | + assertType('list<int>', collect($nodes, static function (Node $node): array { |
| 76 | + return collect($node->children, static function (Node $child): array { |
| 77 | + return collect($child->children, static function (Node $grandChild): array { |
| 78 | + return [$grandChild->value]; |
| 79 | + }); |
| 80 | + }); |
| 81 | + })); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * @param list<Node> $nodes |
| 86 | + */ |
| 87 | +function threeLevelsClosuresWithoutReturnTypes(array $nodes): void |
| 88 | +{ |
| 89 | + assertType('list<int>', collect($nodes, static function (Node $node) { |
| 90 | + return collect($node->children, static function (Node $child) { |
| 91 | + return collect($child->children, static function (Node $grandChild) { |
| 92 | + return [$grandChild->value]; |
| 93 | + }); |
| 94 | + }); |
| 95 | + })); |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * @param list<Node> $nodes |
| 100 | + */ |
| 101 | +function fiveLevels(array $nodes): void |
| 102 | +{ |
| 103 | + assertType('list<int>', collect($nodes, static fn (Node $a): array |
| 104 | + => collect($a->children, static fn (Node $b): array |
| 105 | + => collect($b->children, static fn (Node $c): array |
| 106 | + => collect($c->children, static fn (Node $d): array |
| 107 | + => collect($d->children, static fn (Node $e): array => [$e->value]), |
| 108 | + ), |
| 109 | + ), |
| 110 | + ), |
| 111 | + )); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * @template T |
| 116 | + */ |
| 117 | +final class Box |
| 118 | +{ |
| 119 | + /** @param T $value */ |
| 120 | + public function __construct(public mixed $value) |
| 121 | + { |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @template U |
| 126 | + * @param callable(T): U $callback |
| 127 | + * @return Box<U> |
| 128 | + */ |
| 129 | + public function map(callable $callback): Box |
| 130 | + { |
| 131 | + return new Box($callback($this->value)); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @template V |
| 136 | + * @template U |
| 137 | + * @param V $value |
| 138 | + * @param callable(V): U $callback |
| 139 | + * @return Box<U> |
| 140 | + */ |
| 141 | + public static function mapStatic(mixed $value, callable $callback): Box |
| 142 | + { |
| 143 | + return new Box($callback($value)); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * @template T |
| 149 | + * @param callable(): T $callback |
| 150 | + * @return T |
| 151 | + */ |
| 152 | +function run(callable $callback): mixed |
| 153 | +{ |
| 154 | + return $callback(); |
| 155 | +} |
| 156 | + |
| 157 | +function boxThreeLevels(): void |
| 158 | +{ |
| 159 | + $box = new Box(1); |
| 160 | + assertType('Bug15406\Box<Bug15406\Box<Bug15406\Box<array{k: 1}>>>', $box->map(fn (int $i): Box => (new Box($i))->map(fn (int $j): Box => (new Box($j))->map(fn (int $k): array => ['k' => $k])))); |
| 161 | +} |
| 162 | + |
| 163 | +function boxStaticThreeLevels(): void |
| 164 | +{ |
| 165 | + assertType('Bug15406\Box<Bug15406\Box<Bug15406\Box<array{k: 1}>>>', Box::mapStatic(1, fn (int $i): Box => Box::mapStatic($i, fn (int $j): Box => Box::mapStatic($j, fn (int $k): array => ['k' => $k])))); |
| 166 | +} |
| 167 | + |
| 168 | +function boxTwoLevels(): void |
| 169 | +{ |
| 170 | + /** @var Box<int> $box */ |
| 171 | + $box = new Box(1); |
| 172 | + assertType('Bug15406\Box<Bug15406\Box<array{j: int}>>', $box->map(fn (int $i): Box => (new Box($i))->map(fn (int $j): array => ['j' => $j]))); |
| 173 | +} |
| 174 | + |
| 175 | +run(function (): void { |
| 176 | + $box = new Box(1); |
| 177 | + assertType('Bug15406\Box<Bug15406\Box<array{j: 1}>>', $box->map(fn (int $i): Box => (new Box($i))->map(fn (int $j): array => ['j' => $j]))); |
| 178 | + |
| 179 | + /** @var Box<int> $box */ |
| 180 | + $box = new Box(1); |
| 181 | + assertType('Bug15406\Box<Bug15406\Box<array{j: int}>>', $box->map(fn (int $i): Box => (new Box($i))->map(fn (int $j): array => ['j' => $j]))); |
| 182 | +}); |
| 183 | + |
| 184 | +function arrayMapFourLevels(): void |
| 185 | +{ |
| 186 | + assertType('array{array{array{array{array{1}}}}}', array_map(fn ($x) => array_map(fn ($y) => array_map(fn ($z) => array_map(fn ($w) => [$w], [$z]), [$y]), [$x]), [1])); |
| 187 | +} |
0 commit comments