Skip to content

Commit

Permalink
Avoid internal error with T<X> where T bound consist of intersect…
Browse files Browse the repository at this point in the history
…ion type
  • Loading branch information
ondrejmirtes committed Jan 30, 2024
1 parent ea52b59 commit 778b569
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
use PHPStan\Type\FloatType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Helper\GetTemplateTypeType;
use PHPStan\Type\IntegerRangeType;
Expand Down Expand Up @@ -749,6 +750,9 @@ static function (string $variance): TemplateTypeVariance {
$mainType = $this->resolveIdentifierTypeNode($typeNode->type, $nameScope);
$mainTypeObjectClassNames = $mainType->getObjectClassNames();
if (count($mainTypeObjectClassNames) > 1) {
if ($mainType instanceof TemplateType) {
return new ErrorType();
}
throw new ShouldNotHappenException();
}
$mainTypeClassName = $mainTypeObjectClassNames[0] ?? null;
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,14 @@ public function testBug10302(): void
$this->assertNoErrors($errors);
}

public function testBug10509(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-10509.php');
$this->assertCount(2, $errors);
$this->assertSame('Method Bug10509\Foo::doFoo() has no return type specified.', $errors[0]->getMessage());
$this->assertSame('PHPDoc tag @return contains unresolvable type.', $errors[1]->getMessage());
}

/**
* @param string[]|null $allAnalysedFiles
* @return Error[]
Expand Down
31 changes: 31 additions & 0 deletions tests/PHPStan/Analyser/data/bug-10509.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Bug10509;

/** @template T */
interface One
{

}

/** @template T */
interface Two
{

}

/**
* @template T of One&Two
*/
class Foo
{

/**
* @return T<int>
*/
public function doFoo()
{

}

}

0 comments on commit 778b569

Please sign in to comment.