|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TypePHP\Tests\TypeChecking\Generics; |
| 6 | + |
| 7 | +use ArrayIterator; |
| 8 | +use Generator; |
| 9 | +use Traversable; |
| 10 | +use TypePHP\Exception\TypeError; |
| 11 | +use TypePHP\Internal\Util\Config; |
| 12 | +use TypePHP\Tests\Fixtures\Domain\Car; |
| 13 | +use TypePHP\Tests\Fixtures\Domain\User; |
| 14 | +use TypePHP\TypePHP; |
| 15 | + |
| 16 | +/** |
| 17 | + * @template T |
| 18 | + */ |
| 19 | +class InheritedShapeBaseBox |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @param T $value |
| 23 | + */ |
| 24 | + public function put(mixed $value): mixed |
| 25 | + { |
| 26 | + return $value; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * @extends InheritedShapeBaseBox<array{user: User, car?: Car}> |
| 32 | + */ |
| 33 | +class InheritedArrayShapeUserBox extends InheritedShapeBaseBox |
| 34 | +{ |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * @extends InheritedShapeBaseBox<object{user: User}> |
| 39 | + */ |
| 40 | +class InheritedObjectShapeUserBox extends InheritedShapeBaseBox |
| 41 | +{ |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * @extends InheritedShapeBaseBox<callable(User): User> |
| 46 | + */ |
| 47 | +class InheritedCallableUserBox extends InheritedShapeBaseBox |
| 48 | +{ |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Extends BaseBox with an iterable containing an unqualified class name |
| 53 | + * |
| 54 | + * @extends InheritedShapeBaseBox<iterable<User>> |
| 55 | + */ |
| 56 | +class InheritedIterableUserBox extends InheritedShapeBaseBox |
| 57 | +{ |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Extends BaseBox with a Traversable containing an unqualified class name |
| 62 | + * |
| 63 | + * @extends InheritedShapeBaseBox<Traversable<string, User>> |
| 64 | + */ |
| 65 | +class InheritedTraversableUserBox extends InheritedShapeBaseBox |
| 66 | +{ |
| 67 | + /** |
| 68 | + * Consumes the Traversable so lazy iteration checks fire |
| 69 | + * |
| 70 | + * @param Traversable<string, User> $items |
| 71 | + * |
| 72 | + * @return list<User> |
| 73 | + */ |
| 74 | + public function consume(Traversable $items): array |
| 75 | + { |
| 76 | + $collected = []; |
| 77 | + foreach ($items as $item) { |
| 78 | + $collected[] = $item; |
| 79 | + } |
| 80 | + |
| 81 | + return $collected; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Extends BaseBox with a Generator containing an unqualified class name |
| 87 | + * |
| 88 | + * @extends InheritedShapeBaseBox<Generator<int, User>> |
| 89 | + */ |
| 90 | +class InheritedGeneratorUserBox extends InheritedShapeBaseBox |
| 91 | +{ |
| 92 | + /** |
| 93 | + * @return Generator<int, User> |
| 94 | + */ |
| 95 | + public function stream(bool $valid = true): Generator |
| 96 | + { |
| 97 | + yield 0 => $valid ? new User('Alice') : new Car(); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Extends BaseBox with an array shape holding a nested iterable of unqualified User |
| 103 | + * |
| 104 | + * @extends InheritedShapeBaseBox<array{stream: iterable<User>}> |
| 105 | + */ |
| 106 | +class InheritedShapeWithIterableUserBox extends InheritedShapeBaseBox |
| 107 | +{ |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * @template T |
| 112 | + */ |
| 113 | +interface InheritedShapeInterface |
| 114 | +{ |
| 115 | + /** |
| 116 | + * @param T $data |
| 117 | + */ |
| 118 | + public function process(mixed $data): mixed; |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * @implements InheritedShapeInterface<array{user: User, car?: Car}> |
| 123 | + */ |
| 124 | +class InheritedShapeInterfaceImplementation implements InheritedShapeInterface |
| 125 | +{ |
| 126 | + public function process(mixed $data): mixed |
| 127 | + { |
| 128 | + return $data; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * @template T |
| 134 | + */ |
| 135 | +trait InheritedShapeTrait |
| 136 | +{ |
| 137 | + /** |
| 138 | + * @param T $record |
| 139 | + */ |
| 140 | + public function logRecord(mixed $record): mixed |
| 141 | + { |
| 142 | + return $record; |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +class InheritedShapeInlineTraitConsumer |
| 147 | +{ |
| 148 | + /** |
| 149 | + * @use InheritedShapeTrait<array{user: User}> |
| 150 | + */ |
| 151 | + use InheritedShapeTrait; |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * @use InheritedShapeTrait<array{user: User}> |
| 156 | + */ |
| 157 | +class InheritedShapeClassLevelTraitConsumer |
| 158 | +{ |
| 159 | + use InheritedShapeTrait; |
| 160 | +} |
| 161 | + |
| 162 | +describe('Inherited Generic Shapes & Iterables Namespace Resolution', function () { |
| 163 | + beforeEach(function () { |
| 164 | + Config::reset(); |
| 165 | + }); |
| 166 | + |
| 167 | + afterEach(function () { |
| 168 | + Config::reset(); |
| 169 | + }); |
| 170 | + |
| 171 | + describe('Class Shapes & Callables (@extends)', function () { |
| 172 | + test('resolves unqualified class names inside array shapes in @extends tag (reported bug)', function () { |
| 173 | + $box = new InheritedArrayShapeUserBox(); |
| 174 | + |
| 175 | + $payload = ['user' => new User('Alice')]; |
| 176 | + expect($box->put($payload))->toBe($payload); |
| 177 | + |
| 178 | + expect(fn () => $box->put(['user' => new Car()])) |
| 179 | + ->toThrow(TypeError::class, 'must be of type TypePHP\Tests\Fixtures\Domain\User') |
| 180 | + ; |
| 181 | + }); |
| 182 | + |
| 183 | + test('resolves unqualified class names inside object shapes in @extends tag', function () { |
| 184 | + $box = new InheritedObjectShapeUserBox(); |
| 185 | + |
| 186 | + $valid = (object)['user' => new User('Bob')]; |
| 187 | + expect($box->put($valid))->toBe($valid); |
| 188 | + |
| 189 | + $invalid = (object)['user' => new Car()]; |
| 190 | + expect(fn () => $box->put($invalid)) |
| 191 | + ->toThrow(TypeError::class, 'must be of type TypePHP\Tests\Fixtures\Domain\User') |
| 192 | + ; |
| 193 | + }); |
| 194 | + |
| 195 | + test('resolves unqualified class names inside callables in @extends tag', function () { |
| 196 | + $box = new InheritedCallableUserBox(); |
| 197 | + |
| 198 | + $cb = fn (User $u): User => $u; |
| 199 | + $wrapped = $box->put($cb); |
| 200 | + |
| 201 | + expect($wrapped(new User('Charlie')))->toBeInstanceOf(User::class); |
| 202 | + }); |
| 203 | + }); |
| 204 | + |
| 205 | + describe('Iterables and Generators (@extends)', function () { |
| 206 | + test('resolves unqualified class names inside iterable<User> in @extends tag', function () { |
| 207 | + $box = new InheritedIterableUserBox(); |
| 208 | + |
| 209 | + expect(TypePHP::getGenericType($box))->toBe('iterable<TypePHP\Tests\Fixtures\Domain\User>'); |
| 210 | + |
| 211 | + $payload = [new User('Alice'), new User('Bob')]; |
| 212 | + expect($box->put($payload))->toBe($payload); |
| 213 | + |
| 214 | + expect(fn () => $box->put([new Car()])) |
| 215 | + ->toThrow(TypeError::class, 'must be of type TypePHP\Tests\Fixtures\Domain\User') |
| 216 | + ; |
| 217 | + }); |
| 218 | + |
| 219 | + test('resolves unqualified class names inside Traversable<string, User> in @extends tag and validates on iteration', function () { |
| 220 | + $box = new InheritedTraversableUserBox(); |
| 221 | + |
| 222 | + expect(TypePHP::getGenericType($box))->toBe('Traversable<string, TypePHP\Tests\Fixtures\Domain\User>'); |
| 223 | + |
| 224 | + $iter = new ArrayIterator(['first' => new User('Alice')]); |
| 225 | + expect($box->consume($iter))->toHaveCount(1); |
| 226 | + |
| 227 | + $badIter = new ArrayIterator(['first' => new Car()]); |
| 228 | + expect(fn () => $box->consume($badIter)) |
| 229 | + ->toThrow(TypeError::class, 'must be of type TypePHP\Tests\Fixtures\Domain\User') |
| 230 | + ; |
| 231 | + }); |
| 232 | + |
| 233 | + test('resolves unqualified class names inside Generator<int, User> in @extends tag and validates on yield', function () { |
| 234 | + $box = new InheritedGeneratorUserBox(); |
| 235 | + |
| 236 | + expect(TypePHP::getGenericType($box))->toBe('Generator<int, TypePHP\Tests\Fixtures\Domain\User>'); |
| 237 | + |
| 238 | + $validGen = $box->stream(true); |
| 239 | + expect($validGen->current())->toBeInstanceOf(User::class); |
| 240 | + |
| 241 | + $badGen = $box->stream(false); |
| 242 | + expect(fn () => $badGen->current()) |
| 243 | + ->toThrow(TypeError::class, 'must be of type TypePHP\Tests\Fixtures\Domain\User') |
| 244 | + ; |
| 245 | + }); |
| 246 | + |
| 247 | + test('resolves unqualified class names inside array shape containing nested iterable<User>', function () { |
| 248 | + $box = new InheritedShapeWithIterableUserBox(); |
| 249 | + |
| 250 | + $valid = ['stream' => [new User('Alice'), new User('Bob')]]; |
| 251 | + expect($box->put($valid))->toBe($valid); |
| 252 | + |
| 253 | + $invalid = ['stream' => [new Car()]]; |
| 254 | + expect(fn () => $box->put($invalid)) |
| 255 | + ->toThrow(TypeError::class, 'must be of type TypePHP\Tests\Fixtures\Domain\User') |
| 256 | + ; |
| 257 | + }); |
| 258 | + }); |
| 259 | + |
| 260 | + describe('Interface Implementation (@implements)', function () { |
| 261 | + test('resolves unqualified class names inside array shapes in @implements tag', function () { |
| 262 | + $service = new InheritedShapeInterfaceImplementation(); |
| 263 | + |
| 264 | + $valid = ['user' => new User('Dave')]; |
| 265 | + expect($service->process($valid))->toBe($valid); |
| 266 | + |
| 267 | + expect(fn () => $service->process(['user' => new Car()])) |
| 268 | + ->toThrow(TypeError::class, 'must be of type TypePHP\Tests\Fixtures\Domain\User') |
| 269 | + ; |
| 270 | + }); |
| 271 | + }); |
| 272 | + |
| 273 | + describe('Trait Usage (@use)', function () { |
| 274 | + test('resolves unqualified class names inside array shapes in inline trait use statement', function () { |
| 275 | + $consumer = new InheritedShapeInlineTraitConsumer(); |
| 276 | + |
| 277 | + $valid = ['user' => new User('Eve')]; |
| 278 | + expect($consumer->logRecord($valid))->toBe($valid); |
| 279 | + |
| 280 | + expect(fn () => $consumer->logRecord(['user' => new Car()])) |
| 281 | + ->toThrow(TypeError::class, 'must be of type TypePHP\Tests\Fixtures\Domain\User') |
| 282 | + ; |
| 283 | + }); |
| 284 | + |
| 285 | + test('resolves unqualified class names inside array shapes in class-level trait use docblock', function () { |
| 286 | + $consumer = new InheritedShapeClassLevelTraitConsumer(); |
| 287 | + |
| 288 | + $valid = ['user' => new User('Frank')]; |
| 289 | + expect($consumer->logRecord($valid))->toBe($valid); |
| 290 | + |
| 291 | + expect(fn () => $consumer->logRecord(['user' => new Car()])) |
| 292 | + ->toThrow(TypeError::class, 'must be of type TypePHP\Tests\Fixtures\Domain\User') |
| 293 | + ; |
| 294 | + }); |
| 295 | + }); |
| 296 | +}); |
0 commit comments