Skip to content

Commit 778b569

Browse files
committed
Avoid internal error with T<X> where T bound consist of intersection type
1 parent ea52b59 commit 778b569

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/PhpDoc/TypeNodeResolver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
use PHPStan\Type\FloatType;
6666
use PHPStan\Type\Generic\GenericClassStringType;
6767
use PHPStan\Type\Generic\GenericObjectType;
68+
use PHPStan\Type\Generic\TemplateType;
6869
use PHPStan\Type\Generic\TemplateTypeVariance;
6970
use PHPStan\Type\Helper\GetTemplateTypeType;
7071
use PHPStan\Type\IntegerRangeType;
@@ -749,6 +750,9 @@ static function (string $variance): TemplateTypeVariance {
749750
$mainType = $this->resolveIdentifierTypeNode($typeNode->type, $nameScope);
750751
$mainTypeObjectClassNames = $mainType->getObjectClassNames();
751752
if (count($mainTypeObjectClassNames) > 1) {
753+
if ($mainType instanceof TemplateType) {
754+
return new ErrorType();
755+
}
752756
throw new ShouldNotHappenException();
753757
}
754758
$mainTypeClassName = $mainTypeObjectClassNames[0] ?? null;

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,14 @@ public function testBug10302(): void
12861286
$this->assertNoErrors($errors);
12871287
}
12881288

1289+
public function testBug10509(): void
1290+
{
1291+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-10509.php');
1292+
$this->assertCount(2, $errors);
1293+
$this->assertSame('Method Bug10509\Foo::doFoo() has no return type specified.', $errors[0]->getMessage());
1294+
$this->assertSame('PHPDoc tag @return contains unresolvable type.', $errors[1]->getMessage());
1295+
}
1296+
12891297
/**
12901298
* @param string[]|null $allAnalysedFiles
12911299
* @return Error[]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Bug10509;
4+
5+
/** @template T */
6+
interface One
7+
{
8+
9+
}
10+
11+
/** @template T */
12+
interface Two
13+
{
14+
15+
}
16+
17+
/**
18+
* @template T of One&Two
19+
*/
20+
class Foo
21+
{
22+
23+
/**
24+
* @return T<int>
25+
*/
26+
public function doFoo()
27+
{
28+
29+
}
30+
31+
}

0 commit comments

Comments
 (0)