Skip to content

Conversation

@yt-catpaw
Copy link

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


public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
{
if ($this->equals($otherType)) {
Copy link
Member

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:

/**
* @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.

Copy link
Author

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

0ba6805

$conditional,
]);

$this->assertSame('Yes', $conditional->isSuperTypeOf($unionWithConditional)->describe());
Copy link
Member

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 😊

Copy link
Member

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!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

28bd4ec

Added data-provider tests for isSuperTypeOf/isSubTypeOf (in tests/PHPStan/Type/LateResolvableTypeTraitTest.php).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong Parameter #3 $x ... of method B::foo() should be contravariant with parameter $x ... of method A::foo()

2 participants