Skip to content

Commit 0ba6805

Browse files
committed
Short-circuit identical late-resolvable types in isSuperTypeOf and add coverage
1 parent d574aa7 commit 0ba6805

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/Type/Traits/LateResolvableTypeTrait.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ private function isSuperTypeOfDefault(Type $type): IsSuperTypeOfResult
6666
return IsSuperTypeOfResult::createYes();
6767
}
6868

69+
if ($this->equals($type)) {
70+
return IsSuperTypeOfResult::createYes();
71+
}
72+
73+
if ($type instanceof UnionType) {
74+
foreach ($type->getTypes() as $innerType) {
75+
if ($this->equals($innerType)) {
76+
return IsSuperTypeOfResult::createYes();
77+
}
78+
}
79+
}
80+
6981
if ($type instanceof LateResolvableType) {
7082
$type = $type->resolve();
7183
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type;
4+
5+
use PHPStan\Type\Constant\ConstantStringType;
6+
use PHPStan\Type\IntegerType;
7+
use PHPStan\Type\NeverType;
8+
use PHPStan\Type\UnionType;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class LateResolvableTypeTraitTest extends TestCase
12+
{
13+
14+
public function testIsSuperTypeOfForConditional(): void
15+
{
16+
$conditional = new ConditionalTypeForParameter(
17+
'$operator',
18+
new ConstantStringType('in'),
19+
new IntegerType(),
20+
new NeverType(),
21+
false,
22+
);
23+
24+
$this->assertSame('Yes', $conditional->isSuperTypeOf($conditional)->describe());
25+
26+
$unionWithConditional = new UnionType([
27+
new StringType(),
28+
$conditional,
29+
]);
30+
31+
$this->assertSame('Yes', $conditional->isSuperTypeOf($unionWithConditional)->describe());
32+
}
33+
34+
}

0 commit comments

Comments
 (0)