Skip to content

Commit d574aa7

Browse files
committed
Short-circuit identical late-resolvable conditionals in subtype checks
1 parent 43e8099 commit d574aa7

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/Type/Traits/LateResolvableTypeTrait.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPStan\Type\LateResolvableType;
1919
use PHPStan\Type\NeverType;
2020
use PHPStan\Type\Type;
21+
use PHPStan\Type\UnionType;
2122

2223
trait LateResolvableTypeTrait
2324
{
@@ -570,6 +571,18 @@ public function tryRemove(Type $typeToRemove): ?Type
570571

571572
public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
572573
{
574+
if ($this->equals($otherType)) {
575+
return IsSuperTypeOfResult::createYes();
576+
}
577+
578+
if ($otherType instanceof UnionType) {
579+
foreach ($otherType->getTypes() as $innerType) {
580+
if ($this->equals($innerType)) {
581+
return IsSuperTypeOfResult::createYes();
582+
}
583+
}
584+
}
585+
573586
$result = $this->resolve();
574587

575588
if ($result instanceof CompoundType) {

tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,13 @@ public function testBug10488(): void
550550
$this->analyse([__DIR__ . '/data/bug-10488.php'], []);
551551
}
552552

553+
public function testBug10942(): void
554+
{
555+
$this->reportMaybes = true;
556+
$this->reportStatic = true;
557+
$this->analyse([__DIR__ . '/data/bug-10942.php'], []);
558+
}
559+
553560
#[RequiresPhp('>= 8.0')]
554561
public function testBug12073(): void
555562
{
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug10942;
4+
5+
class A
6+
{
7+
8+
/**
9+
* @param string|($operator is 'in' ? int : never) $sqlRight
10+
*/
11+
protected function _renderConditionBinary(string $operator, string $sqlLeft, $sqlRight): string
12+
{
13+
return 'x';
14+
}
15+
16+
}
17+
18+
class B extends A
19+
{
20+
21+
#[\Override]
22+
protected function _renderConditionBinary(string $operator, string $sqlLeft, $sqlRight): string
23+
{
24+
return 'y';
25+
}
26+
27+
}

0 commit comments

Comments
 (0)