|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Reflection; |
| 4 | + |
| 5 | +use PHPStan\Testing\PHPStanTestCase; |
| 6 | +use PHPStan\TrinaryLogic; |
| 7 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 8 | +use PHPStan\Type\Constant\ConstantIntegerType; |
| 9 | +use PHPStan\Type\IntegerType; |
| 10 | +use PHPStan\Type\NeverType; |
| 11 | +use PHPStan\Type\Type; |
| 12 | +use PHPStan\Type\VerbosityLevel; |
| 13 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 14 | +use function array_map; |
| 15 | + |
| 16 | +class UnpackedArgumentTypesTest extends PHPStanTestCase |
| 17 | +{ |
| 18 | + |
| 19 | + /** |
| 20 | + * @return iterable<string, array{ConstantArrayType, list<string>, string|null}> |
| 21 | + */ |
| 22 | + public static function dataExpand(): iterable |
| 23 | + { |
| 24 | + $keyTypes = [new ConstantIntegerType(0), new ConstantIntegerType(1)]; |
| 25 | + $valueTypes = [new ConstantIntegerType(1), new ConstantIntegerType(2)]; |
| 26 | + $never = new NeverType(true); |
| 27 | + |
| 28 | + yield 'list' => [ |
| 29 | + new ConstantArrayType($keyTypes, $valueTypes, [2], isList: TrinaryLogic::createYes()), |
| 30 | + ['1', '2'], |
| 31 | + null, |
| 32 | + ]; |
| 33 | + |
| 34 | + yield 'sealed list' => [ |
| 35 | + new ConstantArrayType($keyTypes, $valueTypes, [2], isList: TrinaryLogic::createYes(), unsealed: [$never, $never]), |
| 36 | + ['1', '2'], |
| 37 | + null, |
| 38 | + ]; |
| 39 | + |
| 40 | + yield 'maybe a list' => [ |
| 41 | + new ConstantArrayType($keyTypes, $valueTypes, [2], isList: TrinaryLogic::createMaybe()), |
| 42 | + [], |
| 43 | + '1|2', |
| 44 | + ]; |
| 45 | + |
| 46 | + yield 'unsealed list' => [ |
| 47 | + new ConstantArrayType($keyTypes, $valueTypes, [2], isList: TrinaryLogic::createYes(), unsealed: [new IntegerType(), new ConstantIntegerType(3)]), |
| 48 | + [], |
| 49 | + '1|2|3', |
| 50 | + ]; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param list<string> $expectedTypes |
| 55 | + */ |
| 56 | + #[DataProvider('dataExpand')] |
| 57 | + public function testExpand(ConstantArrayType $array, array $expectedTypes, ?string $expectedRestType): void |
| 58 | + { |
| 59 | + [$types, $restType] = UnpackedArgumentTypes::expand([$array], [true]); |
| 60 | + |
| 61 | + $this->assertSame($expectedTypes, array_map(static fn (Type $type) => $type->describe(VerbosityLevel::precise()), $types)); |
| 62 | + $this->assertSame($expectedRestType, $restType?->describe(VerbosityLevel::precise())); |
| 63 | + } |
| 64 | + |
| 65 | +} |
0 commit comments