Skip to content

Commit f7d232a

Browse files
committed
Cannot create IntersectionType with less than two types
1 parent 47b5293 commit f7d232a

File tree

4 files changed

+15
-29
lines changed

4 files changed

+15
-29
lines changed

src/Type/IntersectionType.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ class IntersectionType implements CompoundType
3333
*/
3434
public function __construct(array $types)
3535
{
36+
if (count($types) < 2) {
37+
throw new \PHPStan\ShouldNotHappenException(sprintf(
38+
'Cannot create %s with: %s',
39+
self::class,
40+
implode(', ', array_map(static function (Type $type): string {
41+
return $type->describe(VerbosityLevel::value());
42+
}, $types))
43+
));
44+
}
45+
3646
$this->types = UnionTypeHelper::sortTypes($types);
3747
}
3848

tests/PHPStan/Type/CallableTypeTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\Reflection\Native\NativeParameterReflection;
66
use PHPStan\Reflection\PassedByReference;
77
use PHPStan\TrinaryLogic;
8+
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
89
use PHPStan\Type\Accessory\HasMethodType;
910
use PHPStan\Type\Constant\ConstantArrayType;
1011
use PHPStan\Type\Constant\ConstantIntegerType;
@@ -99,17 +100,12 @@ public function dataIsSubTypeOf(): array
99100
],
100101
[
101102
new CallableType(),
102-
new IntersectionType([new CallableType()]),
103-
TrinaryLogic::createYes(),
104-
],
105-
[
106-
new CallableType(),
107-
new IntersectionType([new StringType()]),
103+
new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]),
108104
TrinaryLogic::createMaybe(),
109105
],
110106
[
111107
new CallableType(),
112-
new IntersectionType([new IntegerType()]),
108+
new IntegerType(),
113109
TrinaryLogic::createNo(),
114110
],
115111
[

tests/PHPStan/Type/IntersectionTypeTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,6 @@ public function dataIsSuperTypeOf(): \Iterator
147147
TrinaryLogic::createNo(),
148148
];
149149

150-
$intersectionTypeB = new IntersectionType([
151-
new IntegerType(),
152-
]);
153-
154-
yield [
155-
$intersectionTypeB,
156-
$intersectionTypeB,
157-
TrinaryLogic::createYes(),
158-
];
159-
160150
yield [
161151
new IntersectionType([
162152
new ArrayType(new MixedType(), new MixedType()),
@@ -335,16 +325,6 @@ public function dataIsSubTypeOf(): \Iterator
335325
TrinaryLogic::createNo(),
336326
];
337327

338-
$intersectionTypeB = new IntersectionType([
339-
new IntegerType(),
340-
]);
341-
342-
yield [
343-
$intersectionTypeB,
344-
$intersectionTypeB,
345-
TrinaryLogic::createYes(),
346-
];
347-
348328
$intersectionTypeC = new IntersectionType([
349329
new StringType(),
350330
new CallableType(),

tests/PHPStan/Type/TypeCombinatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,7 @@ public function dataUnion(): array
18251825
[
18261826
[
18271827
new UnionType([
1828-
new IntersectionType([new ArrayType(new MixedType(), new MixedType())]),
1828+
new ArrayType(new MixedType(), new MixedType()),
18291829
IntegerRangeType::fromInterval(null, -1),
18301830
IntegerRangeType::fromInterval(1, null),
18311831
]),
@@ -1842,7 +1842,7 @@ public function dataUnion(): array
18421842
[
18431843
[
18441844
new UnionType([
1845-
new IntersectionType([new ArrayType(new MixedType(), new MixedType())]),
1845+
new ArrayType(new MixedType(), new MixedType()),
18461846
new CallableType(),
18471847
]),
18481848
TemplateTypeFactory::create(

0 commit comments

Comments
 (0)