|
| 1 | +<?php // lint >= 8.1 |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace Bug15235; |
| 6 | + |
| 7 | +/** |
| 8 | + * @template T of object |
| 9 | + */ |
| 10 | +class A |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @param T $object |
| 14 | + */ |
| 15 | + public function __construct(public object $object) {} |
| 16 | +} |
| 17 | + |
| 18 | +class B |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @param A<covariant static> $a |
| 22 | + */ |
| 23 | + public function method(A $a): void {} |
| 24 | + |
| 25 | + /** |
| 26 | + * @param A<covariant static> $a |
| 27 | + */ |
| 28 | + public static function staticMethod(A $a): void {} |
| 29 | + |
| 30 | + /** |
| 31 | + * @param A<static> $a |
| 32 | + */ |
| 33 | + public function invariant(A $a): void {} |
| 34 | + |
| 35 | + /** |
| 36 | + * @param A<contravariant static> $a |
| 37 | + */ |
| 38 | + public function contravariant(A $a): void {} |
| 39 | +} |
| 40 | + |
| 41 | +class D |
| 42 | +{ |
| 43 | + public function inconsistent(B $b): void |
| 44 | + { |
| 45 | + $b->method(new A($b)); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function fromNew(): void |
| 50 | +{ |
| 51 | + $b = new B(); |
| 52 | + $b->method(new A($b)); |
| 53 | + $b->invariant(new A($b)); |
| 54 | + $b->contravariant(new A($b)); |
| 55 | + $b::staticMethod(new A($b)); |
| 56 | + |
| 57 | + $callable = $b->method(...); |
| 58 | + $callable(new A($b)); |
| 59 | +} |
| 60 | + |
| 61 | +function fromGetClass(B $b): void |
| 62 | +{ |
| 63 | + if (get_class($b) !== B::class) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + $b->method(new A($b)); |
| 68 | +} |
| 69 | + |
| 70 | +/** @template-covariant T of object */ |
| 71 | +class Covariant |
| 72 | +{ |
| 73 | + /** @param T $object */ |
| 74 | + public function __construct(public object $object) {} |
| 75 | +} |
| 76 | + |
| 77 | +/** @template-contravariant T of object */ |
| 78 | +class Contravariant |
| 79 | +{ |
| 80 | + /** @param T $object */ |
| 81 | + public function set(object $object): void {} |
| 82 | +} |
| 83 | + |
| 84 | +class E |
| 85 | +{ |
| 86 | + /** @param Covariant<static> $c */ |
| 87 | + public function declaredCovariant(Covariant $c): void {} |
| 88 | + |
| 89 | + /** @param Contravariant<static> $c */ |
| 90 | + public function declaredContravariant(Contravariant $c): void {} |
| 91 | + |
| 92 | + /** @param \Traversable<int, static> $it */ |
| 93 | + public function traversable(\Traversable $it): void {} |
| 94 | + |
| 95 | + /** @return Covariant<static> */ |
| 96 | + public function makeCovariant(): Covariant |
| 97 | + { |
| 98 | + return new Covariant($this); |
| 99 | + } |
| 100 | + |
| 101 | + /** @return Wrapper<static> */ |
| 102 | + public function makeWrapper(): Wrapper |
| 103 | + { |
| 104 | + return new Wrapper($this); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +class Consumer |
| 109 | +{ |
| 110 | + /** @param Covariant<E> $c */ |
| 111 | + public function takesCovariant(Covariant $c): void {} |
| 112 | + |
| 113 | + /** @param Wrapper<contravariant E> $wrapper */ |
| 114 | + public function takesContravariantWrapper(Wrapper $wrapper): void {} |
| 115 | + |
| 116 | + /** @param Wrapper<Covariant<E>> $wrapper */ |
| 117 | + public function takesNested(Wrapper $wrapper): void {} |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * @param Covariant<E> $covariant |
| 122 | + * @param Contravariant<E> $contravariant |
| 123 | + * @param \Traversable<int, E> $traversable |
| 124 | + */ |
| 125 | +function declaredVariances(Covariant $covariant, Contravariant $contravariant, \Traversable $traversable): void |
| 126 | +{ |
| 127 | + $e = new E(); |
| 128 | + $e->declaredCovariant($covariant); |
| 129 | + $e->declaredContravariant($contravariant); |
| 130 | + $e->traversable($traversable); |
| 131 | +} |
| 132 | + |
| 133 | +function flavourOnTheArgumentSide(Consumer $consumer): void |
| 134 | +{ |
| 135 | + $e = new E(); |
| 136 | + $consumer->takesCovariant($e->makeCovariant()); |
| 137 | + $consumer->takesContravariantWrapper($e->makeWrapper()); |
| 138 | +} |
| 139 | + |
| 140 | +/** @template T of object */ |
| 141 | +class Wrapper |
| 142 | +{ |
| 143 | + /** @param T $object */ |
| 144 | + public function __construct(public object $object) {} |
| 145 | +} |
| 146 | + |
| 147 | +function nestedInGenericType(Consumer $consumer): void |
| 148 | +{ |
| 149 | + $e = new E(); |
| 150 | + $consumer->takesNested(new Wrapper($e->makeCovariant())); |
| 151 | +} |
| 152 | + |
| 153 | +class Composite |
| 154 | +{ |
| 155 | + /** @param Covariant<static>|null $c */ |
| 156 | + public function inUnion(?Covariant $c): void {} |
| 157 | + |
| 158 | + /** @param array<int, Covariant<static>> $c */ |
| 159 | + public function inArray(array $c): void {} |
| 160 | + |
| 161 | + /** @param Covariant<Covariant<static>> $c */ |
| 162 | + public function nested(Covariant $c): void {} |
| 163 | + |
| 164 | + /** @param Wrapper<Covariant<static>> $c */ |
| 165 | + public function nestedInvariant(Wrapper $c): void {} |
| 166 | +} |
| 167 | + |
| 168 | +/** |
| 169 | + * @param Covariant<Composite> $covariant |
| 170 | + * @param array<int, Covariant<Composite>> $array |
| 171 | + * @param Covariant<Covariant<Composite>> $nested |
| 172 | + * @param Wrapper<Covariant<Composite>> $nestedInvariant |
| 173 | + */ |
| 174 | +function compositeShapes(Covariant $covariant, array $array, Covariant $nested, Wrapper $nestedInvariant): void |
| 175 | +{ |
| 176 | + $composite = new Composite(); |
| 177 | + $composite->inUnion($covariant); |
| 178 | + $composite->inUnion(null); |
| 179 | + $composite->inArray($array); |
| 180 | + $composite->nested($nested); |
| 181 | + $composite->nestedInvariant($nestedInvariant); |
| 182 | +} |
0 commit comments