|
| 1 | +<?php // lint >= 8.1 |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace Bug12038; |
| 6 | + |
| 7 | +use function PHPStan\Testing\assertType; |
| 8 | + |
| 9 | +/** |
| 10 | + * @template X |
| 11 | + * @template Y |
| 12 | + * @template Z |
| 13 | + * |
| 14 | + * @param callable(X, Y): Z $fn |
| 15 | + * @return callable(Y, X): Z |
| 16 | + */ |
| 17 | +function flip(callable $fn): callable |
| 18 | +{ |
| 19 | + return fn ($y, $x) => $fn($x, $y); |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * @template A |
| 24 | + * @template B |
| 25 | + * |
| 26 | + * @param list<A> $fa |
| 27 | + * @param list<B> $fb |
| 28 | + * @return list<array{A, B}> |
| 29 | + */ |
| 30 | +function zip(array $fa, array $fb): array |
| 31 | +{ |
| 32 | + $length = min(count($fa), count($fb)); |
| 33 | + $zipped = []; |
| 34 | + |
| 35 | + for ($i = 0; $i < $length; $i++) { |
| 36 | + $zipped[] = [$fa[$i], $fb[$i]]; |
| 37 | + } |
| 38 | + |
| 39 | + return $zipped; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * @template A |
| 44 | + * @template B |
| 45 | + * @template C |
| 46 | + * |
| 47 | + * @param callable(A): B $ab |
| 48 | + * @param callable(B): C $bc |
| 49 | + * @return callable(A): C |
| 50 | + */ |
| 51 | +function compose(callable $ab, callable $bc): callable |
| 52 | +{ |
| 53 | + return fn($a) => $bc($ab($a)); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * @template T |
| 58 | + * @param T $a |
| 59 | + * @return list<T> |
| 60 | + */ |
| 61 | +function toList(mixed $a): array |
| 62 | +{ |
| 63 | + return [$a]; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * @template V |
| 68 | + * @param V $a |
| 69 | + * @return array{boxed: V} |
| 70 | + */ |
| 71 | +function box(mixed $a): array |
| 72 | +{ |
| 73 | + return ['boxed' => $a]; |
| 74 | +} |
| 75 | + |
| 76 | +// flip(zip(...)) should preserve template types |
| 77 | +$flipZip = flip(zip(...)); |
| 78 | +assertType('callable(list<B>, list<A>): list<array{A, B}>', $flipZip); |
| 79 | + |
| 80 | +/** @var list<string> */ |
| 81 | +$strings = []; |
| 82 | +/** @var list<int> */ |
| 83 | +$ints = []; |
| 84 | + |
| 85 | +assertType('list<array{int, string}>', $flipZip($strings, $ints)); |
| 86 | +assertType('list<array{string, int}>', $flipZip($ints, $strings)); |
| 87 | + |
| 88 | +// compose(toList(...), box(...)) should properly unify template types |
| 89 | +$composed1 = compose(toList(...), box(...)); |
| 90 | +assertType('callable(A): array{boxed: list<A>}', $composed1); |
| 91 | + |
| 92 | +// compose(box(...), toList(...)) should properly unify template types |
| 93 | +$composed2 = compose(box(...), toList(...)); |
| 94 | +assertType('callable(A): list<array{boxed: A}>', $composed2); |
0 commit comments