Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Type/Traits/LateResolvableTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Type\LateResolvableType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;

trait LateResolvableTypeTrait
{
Expand Down Expand Up @@ -65,6 +66,18 @@ private function isSuperTypeOfDefault(Type $type): IsSuperTypeOfResult
return IsSuperTypeOfResult::createYes();
}

if ($this->equals($type)) {
return IsSuperTypeOfResult::createYes();
}

if ($type instanceof UnionType) {
foreach ($type->getTypes() as $innerType) {
if ($this->equals($innerType)) {
return IsSuperTypeOfResult::createYes();
}
}
}

if ($type instanceof LateResolvableType) {
$type = $type->resolve();
}
Expand Down Expand Up @@ -570,6 +583,18 @@ public function tryRemove(Type $typeToRemove): ?Type

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

return IsSuperTypeOfResult::createYes();
}

if ($otherType instanceof UnionType) {
foreach ($otherType->getTypes() as $innerType) {
if ($this->equals($innerType)) {
return IsSuperTypeOfResult::createYes();
}
}
}

$result = $this->resolve();

if ($result instanceof CompoundType) {
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,13 @@ public function testBug10488(): void
$this->analyse([__DIR__ . '/data/bug-10488.php'], []);
}

public function testBug10942(): void
{
$this->reportMaybes = true;
$this->reportStatic = true;
$this->analyse([__DIR__ . '/data/bug-10942.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug12073(): void
{
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-10942.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Bug10942;

class A
{

/**
* @param string|($operator is 'in' ? int : never) $sqlRight
*/
protected function _renderConditionBinary(string $operator, string $sqlLeft, $sqlRight): string
{
return 'x';
}

}

class B extends A
{

#[\Override]
protected function _renderConditionBinary(string $operator, string $sqlLeft, $sqlRight): string
{
return 'y';
}

}
34 changes: 34 additions & 0 deletions tests/PHPStan/Type/LateResolvableTypeTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type;

use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\NeverType;
use PHPStan\Type\UnionType;
use PHPUnit\Framework\TestCase;

class LateResolvableTypeTraitTest extends TestCase
{

public function testIsSuperTypeOfForConditional(): void
{
$conditional = new ConditionalTypeForParameter(
'$operator',
new ConstantStringType('in'),
new IntegerType(),
new NeverType(),
false,
);

$this->assertSame('Yes', $conditional->isSuperTypeOf($conditional)->describe());

$unionWithConditional = new UnionType([
new StringType(),
$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).

}

}
Loading