-
Notifications
You must be signed in to change notification settings - Fork 547
Fix subtype check for identical late-resolvable conditional types (bug #10942) #4579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.1.x
Are you sure you want to change the base?
Conversation
|
|
||
| public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult | ||
| { | ||
| if ($this->equals($otherType)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd make sense to write some tests that test this method directly. Similar to this:
phpstan-src/tests/PHPStan/Type/UnionTypeTest.php
Lines 174 to 473 in 43e8099
| /** | |
| * @return Iterator<array-key, array{UnionType, Type, TrinaryLogic}> | |
| */ | |
| public static function dataIsSuperTypeOf(): Iterator | |
| { | |
| $int = new IntegerType(); | |
| $string = new StringType(); | |
| $unionTypeA = new UnionType([ | |
| $int, | |
| $string, | |
| ]); | |
| yield [ | |
| $unionTypeA, | |
| $int, | |
| TrinaryLogic::createYes(), | |
| ]; | |
| yield [ | |
| $unionTypeA, | |
| $string, | |
| TrinaryLogic::createYes(), | |
| ]; | |
| yield [ | |
| $unionTypeA, | |
| $unionTypeA, | |
| TrinaryLogic::createYes(), | |
| ]; | |
| yield [ | |
| $unionTypeA, | |
| new IntersectionType([new StringType(), new CallableType()]), | |
| TrinaryLogic::createYes(), | |
| ]; | |
| yield [ | |
| $unionTypeA, | |
| new MixedType(), | |
| TrinaryLogic::createMaybe(), | |
| ]; | |
| yield [ | |
| $unionTypeA, | |
| new CallableType(), | |
| TrinaryLogic::createMaybe(), | |
| ]; | |
| yield [ | |
| $unionTypeA, | |
| new UnionType([new IntegerType(), new FloatType()]), | |
| TrinaryLogic::createMaybe(), | |
| ]; | |
| yield [ | |
| $unionTypeA, | |
| new UnionType([new CallableType(), new FloatType()]), | |
| TrinaryLogic::createMaybe(), | |
| ]; | |
| yield [ | |
| $unionTypeA, | |
| new IntersectionType([new MixedType(), new CallableType()]), | |
| TrinaryLogic::createMaybe(), | |
| ]; | |
| yield [ | |
| $unionTypeA, | |
| new FloatType(), | |
| TrinaryLogic::createNo(), | |
| ]; | |
| yield [ | |
| $unionTypeA, | |
| new UnionType([new ConstantBooleanType(true), new FloatType()]), | |
| TrinaryLogic::createNo(), | |
| ]; | |
| yield [ | |
| $unionTypeA, | |
| new IterableType(new MixedType(), new MixedType()), | |
| TrinaryLogic::createNo(), | |
| ]; | |
| yield [ | |
| $unionTypeA, | |
| new IntersectionType([new ArrayType(new MixedType(), new MixedType()), new CallableType()]), | |
| 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([ | |
| $intersectionTypeB, | |
| $arrayTypeB, | |
| ]); | |
| yield [ | |
| $unionTypeB, | |
| $intersectionTypeB, | |
| TrinaryLogic::createYes(), | |
| ]; | |
| yield [ | |
| $unionTypeB, | |
| $arrayTypeB, | |
| TrinaryLogic::createYes(), | |
| ]; | |
| yield [ | |
| $unionTypeB, | |
| $unionTypeB, | |
| TrinaryLogic::createYes(), | |
| ]; | |
| yield [ | |
| $unionTypeB, | |
| new MixedType(), | |
| TrinaryLogic::createMaybe(), | |
| ]; | |
| yield [ | |
| $unionTypeB, | |
| new ObjectType('ArrayObject'), | |
| TrinaryLogic::createMaybe(), | |
| ]; | |
| yield [ | |
| $unionTypeB, | |
| new IterableType(new MixedType(), new ObjectType('DatePeriod')), | |
| TrinaryLogic::createMaybe(), | |
| ]; | |
| yield [ | |
| $unionTypeB, | |
| new IterableType(new MixedType(), new MixedType()), | |
| TrinaryLogic::createMaybe(), | |
| ]; | |
| yield [ | |
| $unionTypeB, | |
| new StringType(), | |
| TrinaryLogic::createNo(), | |
| ]; | |
| yield [ | |
| $unionTypeB, | |
| new IntegerType(), | |
| TrinaryLogic::createNo(), | |
| ]; | |
| yield [ | |
| $unionTypeB, | |
| new ObjectType(stdClass::class), | |
| TrinaryLogic::createNo(), | |
| ]; | |
| yield [ | |
| $unionTypeB, | |
| new IterableType(new MixedType(), new ObjectType('DateTime')), | |
| TrinaryLogic::createNo(), | |
| ]; | |
| yield [ | |
| $unionTypeB, | |
| new CallableType(), | |
| TrinaryLogic::createMaybe(), | |
| ]; | |
| yield [ | |
| $unionTypeB, | |
| new IntersectionType([new MixedType(), new CallableType()]), | |
| TrinaryLogic::createMaybe(), | |
| ]; | |
| yield [ | |
| $unionTypeB, | |
| new IntersectionType([new StringType(), new CallableType()]), | |
| TrinaryLogic::createNo(), | |
| ]; | |
| yield 'is super type of template-of-union with same members' => [ | |
| new UnionType([ | |
| new IntegerType(), | |
| new FloatType(), | |
| ]), | |
| TemplateTypeFactory::create( | |
| TemplateTypeScope::createWithClass('Foo'), | |
| 'T', | |
| new UnionType([ | |
| new IntegerType(), | |
| new FloatType(), | |
| ]), | |
| TemplateTypeVariance::createInvariant(), | |
| ), | |
| TrinaryLogic::createYes(), | |
| ]; | |
| yield 'is super type of template-of-union equal to a union member' => [ | |
| new UnionType([ | |
| TemplateTypeFactory::create( | |
| TemplateTypeScope::createWithClass('Foo'), | |
| 'T', | |
| new UnionType([ | |
| new IntegerType(), | |
| new FloatType(), | |
| ]), | |
| TemplateTypeVariance::createInvariant(), | |
| ), | |
| new NullType(), | |
| ]), | |
| TemplateTypeFactory::create( | |
| TemplateTypeScope::createWithClass('Foo'), | |
| 'T', | |
| new UnionType([ | |
| new IntegerType(), | |
| new FloatType(), | |
| ]), | |
| TemplateTypeVariance::createInvariant(), | |
| ), | |
| TrinaryLogic::createYes(), | |
| ]; | |
| yield 'maybe super type of template-of-union equal to a union member' => [ | |
| new UnionType([ | |
| TemplateTypeFactory::create( | |
| TemplateTypeScope::createWithClass('Foo'), | |
| 'T', | |
| new UnionType([ | |
| new IntegerType(), | |
| new FloatType(), | |
| ]), | |
| TemplateTypeVariance::createInvariant(), | |
| ), | |
| new NullType(), | |
| ]), | |
| TemplateTypeFactory::create( | |
| TemplateTypeScope::createWithClass('Bar'), | |
| 'T', | |
| new UnionType([ | |
| new IntegerType(), | |
| new FloatType(), | |
| ]), | |
| TemplateTypeVariance::createInvariant(), | |
| ), | |
| TrinaryLogic::createMaybe(), | |
| ]; | |
| yield 'is super type of template-of-string equal to a union member' => [ | |
| new UnionType([ | |
| TemplateTypeFactory::create( | |
| TemplateTypeScope::createWithClass('Foo'), | |
| 'T', | |
| new StringType(), | |
| TemplateTypeVariance::createInvariant(), | |
| ), | |
| new NullType(), | |
| ]), | |
| TemplateTypeFactory::create( | |
| TemplateTypeScope::createWithClass('Foo'), | |
| 'T', | |
| new StringType(), | |
| TemplateTypeVariance::createInvariant(), | |
| ), | |
| TrinaryLogic::createYes(), | |
| ]; | |
| yield 'maybe super type of template-of-string sub type of a union member' => [ | |
| new UnionType([ | |
| TemplateTypeFactory::create( | |
| TemplateTypeScope::createWithClass('Foo'), | |
| 'T', | |
| new StringType(), | |
| TemplateTypeVariance::createInvariant(), | |
| ), | |
| new NullType(), | |
| ]), | |
| TemplateTypeFactory::create( | |
| TemplateTypeScope::createWithClass('Bar'), | |
| 'T', | |
| new StringType(), | |
| TemplateTypeVariance::createInvariant(), | |
| ), | |
| TrinaryLogic::createMaybe(), | |
| ]; | |
| } | |
| #[DataProvider('dataIsSuperTypeOf')] | |
| public function testIsSuperTypeOf(UnionType $type, Type $otherType, TrinaryLogic $expectedResult): void | |
| { | |
| $actualResult = $type->isSuperTypeOf($otherType); | |
| $this->assertSame( | |
| $expectedResult->describe(), | |
| $actualResult->describe(), | |
| sprintf('%s -> isSuperTypeOf(%s)', $type->describe(VerbosityLevel::precise()), $otherType->describe(VerbosityLevel::precise())), | |
| ); | |
| } |
Also, please review and maybe fix the isSuperTypeOf method as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented; please take a look
| $conditional, | ||
| ]); | ||
|
|
||
| $this->assertSame('Yes', $conditional->isSuperTypeOf($unionWithConditional)->describe()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make a test with a data provider as it looks like in the test I linked. Then I'l try to come up with some data points that break your logic 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also write tests for both isSuperTypeOf and isSubtypeOf. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added data-provider tests for isSuperTypeOf/isSubTypeOf (in tests/PHPStan/Type/LateResolvableTypeTraitTest.php).
closes phpstan/phpstan#10942
・Summary
Treat identical late-resolvable conditional types as compatible before resolving them.
Short-circuit when the same conditional appears inside a union.
Add regression test for bug #10942.
・Testing
vendor/bin/phpunit tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php --filter testBug10942