Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Short-circuit identical late-resolvable types in isSuperTypeOf and ad…
…d coverage
  • Loading branch information
yt-catpaw committed Nov 27, 2025
commit 0ba680581efd69de144e250733366d5ddb95920c
12 changes: 12 additions & 0 deletions src/Type/Traits/LateResolvableTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,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
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