Skip to content

Commit

Permalink
Fixed inferring template types from ThisType
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 20, 2021
1 parent 8f0150d commit a843d87
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Type/Generic/GenericObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,15 @@ public function inferTemplateTypes(Type $receivedType): TemplateTypeMap

$ancestor = $receivedType->getAncestorWithClassName($this->getClassName());

if ($ancestor === null || !$ancestor instanceof GenericObjectType) {
if ($ancestor === null) {
return TemplateTypeMap::createEmpty();
}
$ancestorClassReflection = $ancestor->getClassReflection();
if ($ancestorClassReflection === null) {
return TemplateTypeMap::createEmpty();
}

$otherTypes = $ancestor->getTypes();
$otherTypes = $ancestorClassReflection->typeMapToList($ancestorClassReflection->getActiveTemplateTypeMap());
$typeMap = TemplateTypeMap::createEmpty();

foreach ($this->getTypes() as $i => $type) {
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5705,6 +5705,11 @@ public function dataBug4714(): array
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4714.php');
}

public function dataBug4725(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4725.php');
}

/**
* @dataProvider dataArrayFunctions
* @param string $description
Expand Down Expand Up @@ -11329,6 +11334,7 @@ private function gatherAssertTypes(string $file): array
* @dataProvider dataBug4707
* @dataProvider dataBug4545
* @dataProvider dataBug4714
* @dataProvider dataBug4725
* @param string $assertType
* @param string $file
* @param mixed ...$args
Expand Down
50 changes: 50 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4725.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Bug4725;

use function PHPStan\Analyser\assertType;

class Application_Model_Ada extends Clx_Model_Abstract {}

abstract class Clx_Model_Abstract {}

/**
* @template T of Clx_Model_Abstract
*/
abstract class Clx_Model_Mapper_Abstract
{
}


/**
* @template T of Clx_Model_Abstract
*/
class Clx_Paginator_Adapter_Mapper
{
/**
* @phpstan-param Clx_Model_Mapper_Abstract<T> $mapper
*/
public function __construct(Clx_Model_Mapper_Abstract $mapper)
{
}

/** @return T */
public function getT()
{

}
}

/**
* @template T of Application_Model_Ada
* @extends Clx_Model_Mapper_Abstract<T>
*/
class ClxProductNet_Model_Mapper_Ada extends Clx_Model_Mapper_Abstract
{

public function x() {
$map = new Clx_Paginator_Adapter_Mapper($this);
assertType('Bug4725\Clx_Paginator_Adapter_Mapper<T of Bug4725\Application_Model_Ada (class Bug4725\ClxProductNet_Model_Mapper_Ada, argument)>', $map);
assertType('T of Bug4725\Application_Model_Ada (class Bug4725\ClxProductNet_Model_Mapper_Ada, argument)', $map->getT());
}
}

0 comments on commit a843d87

Please sign in to comment.