Skip to content

Commit

Permalink
Cannot create IntersectionType with less than two types
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 12, 2021
1 parent 47b5293 commit f7d232a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 29 deletions.
10 changes: 10 additions & 0 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
10 changes: 3 additions & 7 deletions tests/PHPStan/Type/CallableTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
],
[
Expand Down
20 changes: 0 additions & 20 deletions tests/PHPStan/Type/IntersectionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]),
Expand All @@ -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(
Expand Down

0 comments on commit f7d232a

Please sign in to comment.