diff --git a/src/Type/IntersectionType.php b/src/Type/IntersectionType.php index 8003be5471..b21073da04 100644 --- a/src/Type/IntersectionType.php +++ b/src/Type/IntersectionType.php @@ -33,6 +33,16 @@ class IntersectionType implements CompoundType */ public function __construct(array $types) { + if (count($types) < 2) { + throw new \PHPStan\ShouldNotHappenException(sprintf( + 'Cannot create %s with: %s', + self::class, + implode(', ', array_map(static function (Type $type): string { + return $type->describe(VerbosityLevel::value()); + }, $types)) + )); + } + $this->types = UnionTypeHelper::sortTypes($types); } diff --git a/tests/PHPStan/Type/CallableTypeTest.php b/tests/PHPStan/Type/CallableTypeTest.php index 9695e032ee..c887c80101 100644 --- a/tests/PHPStan/Type/CallableTypeTest.php +++ b/tests/PHPStan/Type/CallableTypeTest.php @@ -5,6 +5,7 @@ use PHPStan\Reflection\Native\NativeParameterReflection; use PHPStan\Reflection\PassedByReference; use PHPStan\TrinaryLogic; +use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; use PHPStan\Type\Accessory\HasMethodType; use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\Constant\ConstantIntegerType; @@ -99,17 +100,12 @@ public function dataIsSubTypeOf(): array ], [ new CallableType(), - new IntersectionType([new CallableType()]), - TrinaryLogic::createYes(), - ], - [ - new CallableType(), - new IntersectionType([new StringType()]), + new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]), TrinaryLogic::createMaybe(), ], [ new CallableType(), - new IntersectionType([new IntegerType()]), + new IntegerType(), TrinaryLogic::createNo(), ], [ diff --git a/tests/PHPStan/Type/IntersectionTypeTest.php b/tests/PHPStan/Type/IntersectionTypeTest.php index 03434722b1..2310f3585d 100644 --- a/tests/PHPStan/Type/IntersectionTypeTest.php +++ b/tests/PHPStan/Type/IntersectionTypeTest.php @@ -147,16 +147,6 @@ public function dataIsSuperTypeOf(): \Iterator TrinaryLogic::createNo(), ]; - $intersectionTypeB = new IntersectionType([ - new IntegerType(), - ]); - - yield [ - $intersectionTypeB, - $intersectionTypeB, - TrinaryLogic::createYes(), - ]; - yield [ new IntersectionType([ new ArrayType(new MixedType(), new MixedType()), @@ -335,16 +325,6 @@ public function dataIsSubTypeOf(): \Iterator TrinaryLogic::createNo(), ]; - $intersectionTypeB = new IntersectionType([ - new IntegerType(), - ]); - - yield [ - $intersectionTypeB, - $intersectionTypeB, - TrinaryLogic::createYes(), - ]; - $intersectionTypeC = new IntersectionType([ new StringType(), new CallableType(), diff --git a/tests/PHPStan/Type/TypeCombinatorTest.php b/tests/PHPStan/Type/TypeCombinatorTest.php index e9f940aba7..c418c4148c 100644 --- a/tests/PHPStan/Type/TypeCombinatorTest.php +++ b/tests/PHPStan/Type/TypeCombinatorTest.php @@ -1825,7 +1825,7 @@ public function dataUnion(): array [ [ new UnionType([ - new IntersectionType([new ArrayType(new MixedType(), new MixedType())]), + new ArrayType(new MixedType(), new MixedType()), IntegerRangeType::fromInterval(null, -1), IntegerRangeType::fromInterval(1, null), ]), @@ -1842,7 +1842,7 @@ public function dataUnion(): array [ [ new UnionType([ - new IntersectionType([new ArrayType(new MixedType(), new MixedType())]), + new ArrayType(new MixedType(), new MixedType()), new CallableType(), ]), TemplateTypeFactory::create(