From b71495e430b618fb9119830df5dead90358d29f4 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 31 Oct 2025 08:49:18 +0100 Subject: [PATCH 1/4] Fix "Offset X might not exist on..." in UnionTypeTest/IntersectionTypeTest --- tests/PHPStan/Type/IntersectionTypeTest.php | 4 ++++ tests/PHPStan/Type/UnionTypeTest.php | 26 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/tests/PHPStan/Type/IntersectionTypeTest.php b/tests/PHPStan/Type/IntersectionTypeTest.php index 7318ba9c65..90b91b2a4b 100644 --- a/tests/PHPStan/Type/IntersectionTypeTest.php +++ b/tests/PHPStan/Type/IntersectionTypeTest.php @@ -5,6 +5,7 @@ use DoctrineIntersectionTypeIsSupertypeOf\Collection; use Iterator; use ObjectTypeEnums\FooEnum; +use PHPStan\ShouldNotHappenException; use PHPStan\Testing\PHPStanTestCase; use PHPStan\TrinaryLogic; use PHPStan\Type\Accessory\AccessoryArrayListType; @@ -398,6 +399,9 @@ public function testGetEnumCases( $enumCases = $type->getEnumCases(); $this->assertCount(count($expectedEnumCases), $enumCases); foreach ($enumCases as $i => $enumCase) { + if (!array_key_exists($i, $expectedEnumCases)) { + throw new ShouldNotHappenException(); + } $expectedEnumCase = $expectedEnumCases[$i]; $this->assertTrue($expectedEnumCase->equals($enumCase), sprintf('%s->equals(%s)', $expectedEnumCase->describe(VerbosityLevel::precise()), $enumCase->describe(VerbosityLevel::precise()))); } diff --git a/tests/PHPStan/Type/UnionTypeTest.php b/tests/PHPStan/Type/UnionTypeTest.php index f140f4ccfe..31529368a9 100644 --- a/tests/PHPStan/Type/UnionTypeTest.php +++ b/tests/PHPStan/Type/UnionTypeTest.php @@ -8,6 +8,7 @@ use Iterator; use PHPStan\Reflection\Native\NativeParameterReflection; use PHPStan\Reflection\PassedByReference; +use PHPStan\ShouldNotHappenException; use PHPStan\Testing\PHPStanTestCase; use PHPStan\TrinaryLogic; use PHPStan\Type\Accessory\AccessoryLiteralStringType; @@ -33,6 +34,7 @@ use function array_merge; use function array_reverse; use function get_class; +use function PHPStan\dumpType; use function sprintf; use const PHP_VERSION_ID; @@ -180,6 +182,12 @@ public static function dataIsSuperTypeOf(): Iterator new IntegerType(), new StringType(), ]); + if ( + !isset($unionTypeA->getTypes()[0]) + || !isset($unionTypeA->getTypes()[1]) + ) { + throw new ShouldNotHappenException(); + } yield [ $unionTypeA, @@ -266,6 +274,12 @@ public static function dataIsSuperTypeOf(): Iterator ]), new ArrayType(new MixedType(), new ObjectType('DatePeriod')), ]); + if ( + !isset($unionTypeB->getTypes()[0]) + || !isset($unionTypeB->getTypes()[1]) + ) { + throw new ShouldNotHappenException(); + } yield [ $unionTypeB, @@ -477,6 +491,12 @@ public static function dataIsSubTypeOf(): Iterator new IntegerType(), new StringType(), ]); + if ( + !isset($unionTypeA->getTypes()[0]) + || !isset($unionTypeA->getTypes()[1]) + ) { + throw new ShouldNotHappenException(); + } yield [ $unionTypeA, @@ -570,6 +590,12 @@ public static function dataIsSubTypeOf(): Iterator ]), new ArrayType(new MixedType(), new ObjectType('Item')), ]); + if ( + !isset($unionTypeB->getTypes()[0]) + || !isset($unionTypeB->getTypes()[1]) + ) { + throw new ShouldNotHappenException(); + } yield [ $unionTypeB, From ae9fb4b33029e6ab425bd2d8100d05ed21c3e5c9 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 31 Oct 2025 09:27:32 +0100 Subject: [PATCH 2/4] cs --- tests/PHPStan/Type/IntersectionTypeTest.php | 1 + tests/PHPStan/Type/UnionTypeTest.php | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Type/IntersectionTypeTest.php b/tests/PHPStan/Type/IntersectionTypeTest.php index 90b91b2a4b..25fd05dfb5 100644 --- a/tests/PHPStan/Type/IntersectionTypeTest.php +++ b/tests/PHPStan/Type/IntersectionTypeTest.php @@ -22,6 +22,7 @@ use stdClass; use Test\ClassWithToString; use Traversable; +use function array_key_exists; use function count; use function sprintf; use const PHP_VERSION_ID; diff --git a/tests/PHPStan/Type/UnionTypeTest.php b/tests/PHPStan/Type/UnionTypeTest.php index 31529368a9..3b5079709f 100644 --- a/tests/PHPStan/Type/UnionTypeTest.php +++ b/tests/PHPStan/Type/UnionTypeTest.php @@ -34,7 +34,6 @@ use function array_merge; use function array_reverse; use function get_class; -use function PHPStan\dumpType; use function sprintf; use const PHP_VERSION_ID; From 8efab3a3d65da497b917c1c723a6eb801d83db9e Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 31 Oct 2025 12:19:38 +0100 Subject: [PATCH 3/4] Discard changes to tests/PHPStan/Type/IntersectionTypeTest.php --- tests/PHPStan/Type/IntersectionTypeTest.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/PHPStan/Type/IntersectionTypeTest.php b/tests/PHPStan/Type/IntersectionTypeTest.php index 25fd05dfb5..7318ba9c65 100644 --- a/tests/PHPStan/Type/IntersectionTypeTest.php +++ b/tests/PHPStan/Type/IntersectionTypeTest.php @@ -5,7 +5,6 @@ use DoctrineIntersectionTypeIsSupertypeOf\Collection; use Iterator; use ObjectTypeEnums\FooEnum; -use PHPStan\ShouldNotHappenException; use PHPStan\Testing\PHPStanTestCase; use PHPStan\TrinaryLogic; use PHPStan\Type\Accessory\AccessoryArrayListType; @@ -22,7 +21,6 @@ use stdClass; use Test\ClassWithToString; use Traversable; -use function array_key_exists; use function count; use function sprintf; use const PHP_VERSION_ID; @@ -400,9 +398,6 @@ public function testGetEnumCases( $enumCases = $type->getEnumCases(); $this->assertCount(count($expectedEnumCases), $enumCases); foreach ($enumCases as $i => $enumCase) { - if (!array_key_exists($i, $expectedEnumCases)) { - throw new ShouldNotHappenException(); - } $expectedEnumCase = $expectedEnumCases[$i]; $this->assertTrue($expectedEnumCase->equals($enumCase), sprintf('%s->equals(%s)', $expectedEnumCase->describe(VerbosityLevel::precise()), $enumCase->describe(VerbosityLevel::precise()))); } From 225cabac6ba10ea1582c030668eadf1c30bd4b18 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 Nov 2025 08:25:38 +0100 Subject: [PATCH 4/4] simplify --- tests/PHPStan/Type/UnionTypeTest.php | 79 +++++++++++----------------- 1 file changed, 31 insertions(+), 48 deletions(-) diff --git a/tests/PHPStan/Type/UnionTypeTest.php b/tests/PHPStan/Type/UnionTypeTest.php index 3b5079709f..a4940279a4 100644 --- a/tests/PHPStan/Type/UnionTypeTest.php +++ b/tests/PHPStan/Type/UnionTypeTest.php @@ -8,7 +8,6 @@ use Iterator; use PHPStan\Reflection\Native\NativeParameterReflection; use PHPStan\Reflection\PassedByReference; -use PHPStan\ShouldNotHappenException; use PHPStan\Testing\PHPStanTestCase; use PHPStan\TrinaryLogic; use PHPStan\Type\Accessory\AccessoryLiteralStringType; @@ -177,26 +176,22 @@ public function testSelfCompare(Type $type): void */ public static function dataIsSuperTypeOf(): Iterator { + $int = new IntegerType(); + $string = new StringType(); $unionTypeA = new UnionType([ - new IntegerType(), - new StringType(), + $int, + $string, ]); - if ( - !isset($unionTypeA->getTypes()[0]) - || !isset($unionTypeA->getTypes()[1]) - ) { - throw new ShouldNotHappenException(); - } yield [ $unionTypeA, - $unionTypeA->getTypes()[0], + $int, TrinaryLogic::createYes(), ]; yield [ $unionTypeA, - $unionTypeA->getTypes()[1], + $string, TrinaryLogic::createYes(), ]; @@ -266,29 +261,25 @@ public static function dataIsSuperTypeOf(): Iterator TrinaryLogic::createNo(), ]; + $intersectionTypeB = new IntersectionType([ + new ObjectType('ArrayObject'), + new IterableType(new MixedType(), new ObjectType('DatePeriod')), + ]); + $arrayTypeB = new ArrayType(new MixedType(), new ObjectType('DatePeriod')); $unionTypeB = new UnionType([ - new IntersectionType([ - new ObjectType('ArrayObject'), - new IterableType(new MixedType(), new ObjectType('DatePeriod')), - ]), - new ArrayType(new MixedType(), new ObjectType('DatePeriod')), + $intersectionTypeB, + $arrayTypeB, ]); - if ( - !isset($unionTypeB->getTypes()[0]) - || !isset($unionTypeB->getTypes()[1]) - ) { - throw new ShouldNotHappenException(); - } yield [ $unionTypeB, - $unionTypeB->getTypes()[0], + $intersectionTypeB, TrinaryLogic::createYes(), ]; yield [ $unionTypeB, - $unionTypeB->getTypes()[1], + $arrayTypeB, TrinaryLogic::createYes(), ]; @@ -486,16 +477,12 @@ public function testIsSuperTypeOf(UnionType $type, Type $otherType, TrinaryLogic */ public static function dataIsSubTypeOf(): Iterator { + $int = new IntegerType(); + $string = new StringType(); $unionTypeA = new UnionType([ - new IntegerType(), - new StringType(), + $int, + $string, ]); - if ( - !isset($unionTypeA->getTypes()[0]) - || !isset($unionTypeA->getTypes()[1]) - ) { - throw new ShouldNotHappenException(); - } yield [ $unionTypeA, @@ -517,13 +504,13 @@ public static function dataIsSubTypeOf(): Iterator yield [ $unionTypeA, - $unionTypeA->getTypes()[0], + $int, TrinaryLogic::createMaybe(), ]; yield [ $unionTypeA, - $unionTypeA->getTypes()[1], + $string, TrinaryLogic::createMaybe(), ]; @@ -581,20 +568,16 @@ public static function dataIsSubTypeOf(): Iterator TrinaryLogic::createNo(), ]; + $intersectionTypeB = new IntersectionType([ + new ObjectType('ArrayObject'), + new IterableType(new MixedType(), new ObjectType('Item')), + new CallableType(), + ]); + $arrayTypeB = new ArrayType(new MixedType(), new ObjectType('Item')); $unionTypeB = new UnionType([ - new IntersectionType([ - new ObjectType('ArrayObject'), - new IterableType(new MixedType(), new ObjectType('Item')), - new CallableType(), - ]), - new ArrayType(new MixedType(), new ObjectType('Item')), + $intersectionTypeB, + $arrayTypeB, ]); - if ( - !isset($unionTypeB->getTypes()[0]) - || !isset($unionTypeB->getTypes()[1]) - ) { - throw new ShouldNotHappenException(); - } yield [ $unionTypeB, @@ -616,13 +599,13 @@ public static function dataIsSubTypeOf(): Iterator yield [ $unionTypeB, - $unionTypeB->getTypes()[0], + $intersectionTypeB, TrinaryLogic::createMaybe(), ]; yield [ $unionTypeB, - $unionTypeB->getTypes()[1], + $arrayTypeB, TrinaryLogic::createMaybe(), ];