Skip to content

Commit b71495e

Browse files
committed
Fix "Offset X might not exist on..." in UnionTypeTest/IntersectionTypeTest
1 parent b4b3e73 commit b71495e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

tests/PHPStan/Type/IntersectionTypeTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DoctrineIntersectionTypeIsSupertypeOf\Collection;
66
use Iterator;
77
use ObjectTypeEnums\FooEnum;
8+
use PHPStan\ShouldNotHappenException;
89
use PHPStan\Testing\PHPStanTestCase;
910
use PHPStan\TrinaryLogic;
1011
use PHPStan\Type\Accessory\AccessoryArrayListType;
@@ -398,6 +399,9 @@ public function testGetEnumCases(
398399
$enumCases = $type->getEnumCases();
399400
$this->assertCount(count($expectedEnumCases), $enumCases);
400401
foreach ($enumCases as $i => $enumCase) {
402+
if (!array_key_exists($i, $expectedEnumCases)) {
403+
throw new ShouldNotHappenException();
404+
}
401405
$expectedEnumCase = $expectedEnumCases[$i];
402406
$this->assertTrue($expectedEnumCase->equals($enumCase), sprintf('%s->equals(%s)', $expectedEnumCase->describe(VerbosityLevel::precise()), $enumCase->describe(VerbosityLevel::precise())));
403407
}

tests/PHPStan/Type/UnionTypeTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Iterator;
99
use PHPStan\Reflection\Native\NativeParameterReflection;
1010
use PHPStan\Reflection\PassedByReference;
11+
use PHPStan\ShouldNotHappenException;
1112
use PHPStan\Testing\PHPStanTestCase;
1213
use PHPStan\TrinaryLogic;
1314
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
@@ -33,6 +34,7 @@
3334
use function array_merge;
3435
use function array_reverse;
3536
use function get_class;
37+
use function PHPStan\dumpType;
3638
use function sprintf;
3739
use const PHP_VERSION_ID;
3840

@@ -180,6 +182,12 @@ public static function dataIsSuperTypeOf(): Iterator
180182
new IntegerType(),
181183
new StringType(),
182184
]);
185+
if (
186+
!isset($unionTypeA->getTypes()[0])
187+
|| !isset($unionTypeA->getTypes()[1])
188+
) {
189+
throw new ShouldNotHappenException();
190+
}
183191

184192
yield [
185193
$unionTypeA,
@@ -266,6 +274,12 @@ public static function dataIsSuperTypeOf(): Iterator
266274
]),
267275
new ArrayType(new MixedType(), new ObjectType('DatePeriod')),
268276
]);
277+
if (
278+
!isset($unionTypeB->getTypes()[0])
279+
|| !isset($unionTypeB->getTypes()[1])
280+
) {
281+
throw new ShouldNotHappenException();
282+
}
269283

270284
yield [
271285
$unionTypeB,
@@ -477,6 +491,12 @@ public static function dataIsSubTypeOf(): Iterator
477491
new IntegerType(),
478492
new StringType(),
479493
]);
494+
if (
495+
!isset($unionTypeA->getTypes()[0])
496+
|| !isset($unionTypeA->getTypes()[1])
497+
) {
498+
throw new ShouldNotHappenException();
499+
}
480500

481501
yield [
482502
$unionTypeA,
@@ -570,6 +590,12 @@ public static function dataIsSubTypeOf(): Iterator
570590
]),
571591
new ArrayType(new MixedType(), new ObjectType('Item')),
572592
]);
593+
if (
594+
!isset($unionTypeB->getTypes()[0])
595+
|| !isset($unionTypeB->getTypes()[1])
596+
) {
597+
throw new ShouldNotHappenException();
598+
}
573599

574600
yield [
575601
$unionTypeB,

0 commit comments

Comments
 (0)