Skip to content

Commit

Permalink
Fixed inferring from nested class-string
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 17, 2021
1 parent ec2ef2a commit a7a46b1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/Type/Generic/GenericClassStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
return TemplateTypeMap::createEmpty();
}

if (!$this->type->isSuperTypeOf($typeToInfer)->no()) {
return $this->type->inferTemplateTypes($typeToInfer);
}

return TemplateTypeMap::createEmpty();
return $this->type->inferTemplateTypes($typeToInfer);
}

public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
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 @@ -10812,6 +10812,11 @@ public function dataProcGetStatus(): array
return $this->gatherAssertTypes(__DIR__ . '/data/proc_get_status.php');
}

public function dataBug4552(): array
{
return $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/bug-4552.php');
}

/**
* @param string $file
* @return array<string, mixed[]>
Expand Down Expand Up @@ -11036,6 +11041,7 @@ private function gatherAssertTypes(string $file): array
* @dataProvider dataBug1283
* @dataProvider dataBug4538
* @dataProvider dataProcGetStatus
* @dataProvider dataBug4552
* @param string $assertType
* @param string $file
* @param mixed ...$args
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1770,4 +1770,12 @@ public function testOnlyRelevantUnableToResolveTemplateType(): void
]);
}

public function testBug4552(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-4552.php'], []);
}

}
63 changes: 63 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-4552.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types = 1);

namespace Bug4552;

use function PHPStan\Analyser\assertType;

interface OptionPresenter {}

/**
* @template TPresenter of OptionPresenter
*/
interface OptionDefinition {
/**
* @return TPresenter
*/
public function presenter();
}

class SimpleOptionPresenter implements OptionPresenter {
public function test(): bool
{

}
}

/**
* @template-implements OptionDefinition<SimpleOptionPresenter>
*/
class SimpleOptionDefinition implements OptionDefinition {
public function presenter()
{
return new SimpleOptionPresenter();
}
}

/**
* @template T of OptionPresenter
*
* @param class-string<OptionDefinition<T>> $definition
*
* @return T
*/
function present($definition) {
return instantiate($definition)->presenter();
}


/**
* @template T of OptionDefinition
*
* @param class-string<T> $definition
*
* @return T
*/
function instantiate($definition) {
return new $definition;
}

function (): void {
$p = present(SimpleOptionDefinition::class);
assertType(SimpleOptionPresenter::class, $p);
$p->test();
};

0 comments on commit a7a46b1

Please sign in to comment.