|
8 | 8 | use Iterator;
|
9 | 9 | use PHPStan\Reflection\Native\NativeParameterReflection;
|
10 | 10 | use PHPStan\Reflection\PassedByReference;
|
| 11 | +use PHPStan\ShouldNotHappenException; |
11 | 12 | use PHPStan\Testing\PHPStanTestCase;
|
12 | 13 | use PHPStan\TrinaryLogic;
|
13 | 14 | use PHPStan\Type\Accessory\AccessoryLiteralStringType;
|
| 15 | +use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
| 16 | +use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; |
14 | 17 | use PHPStan\Type\Accessory\AccessoryNumericStringType;
|
15 | 18 | use PHPStan\Type\Accessory\HasMethodType;
|
16 | 19 | use PHPStan\Type\Accessory\HasOffsetType;
|
@@ -1640,4 +1643,30 @@ public function dataGetArrays(): iterable
|
1640 | 1643 | ];
|
1641 | 1644 | }
|
1642 | 1645 |
|
| 1646 | + /** |
| 1647 | + * @param Type[] $types |
| 1648 | + * |
| 1649 | + * @dataProvider dataUnionThrows |
| 1650 | + */ |
| 1651 | + public function testUnionThrows(array $types, string $message): void |
| 1652 | + { |
| 1653 | + $this->expectException(ShouldNotHappenException::class); |
| 1654 | + $this->expectExceptionMessage($message); |
| 1655 | + |
| 1656 | + new UnionType($types); |
| 1657 | + } |
| 1658 | + |
| 1659 | + public function dataUnionThrows(): array |
| 1660 | + { |
| 1661 | + return [ |
| 1662 | + // union type requires at least 2 types |
| 1663 | + [[new AccessoryNonEmptyStringType()], 'Cannot create PHPStan\\Type\\UnionType with: non-empty-string'], |
| 1664 | + // test invalid combinations |
| 1665 | + [[new AccessoryNonFalsyStringType(), new AccessoryNumericStringType()], 'Cannot create PHPStan\\Type\\UnionType with: non-falsy-string, numeric-string'], |
| 1666 | + [[new IntegerType(), new AccessoryNumericStringType()], 'Cannot create PHPStan\\Type\\UnionType with: int, numeric-string'], |
| 1667 | + [[new ArrayType(new IntegerType(), new StringType()), new AccessoryNonEmptyStringType()], 'Cannot create PHPStan\\Type\\UnionType with: array<int, string>, non-empty-string'], |
| 1668 | + [[new IntegerType(), new NonEmptyArrayType()], 'Cannot create PHPStan\\Type\\UnionType with: int, non-empty-array'], |
| 1669 | + ]; |
| 1670 | + } |
| 1671 | + |
1643 | 1672 | }
|
0 commit comments