Skip to content

Commit

Permalink
Do not report Unable to resolve template type when the type is not pr…
Browse files Browse the repository at this point in the history
…esent in params
  • Loading branch information
ondrejmirtes committed Feb 4, 2021
1 parent d0a35b5 commit cec9ef6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
20 changes: 18 additions & 2 deletions src/Rules/FunctionCallParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ static function (Type $type): bool {
}

if ($this->checkMissingTypehints && $parametersAcceptor instanceof ResolvedFunctionVariant) {
$originalReturnType = $parametersAcceptor->getOriginalParametersAcceptor()->getReturnType();
$originalParametersAcceptor = $parametersAcceptor->getOriginalParametersAcceptor();
$returnTemplateTypes = [];
TypeTraverser::map($originalReturnType, static function (Type $type, callable $traverse) use (&$returnTemplateTypes): Type {
TypeTraverser::map($originalParametersAcceptor->getReturnType(), static function (Type $type, callable $traverse) use (&$returnTemplateTypes): Type {
if ($type instanceof TemplateType) {
$returnTemplateTypes[$type->getName()] = true;
return $type;
Expand All @@ -310,6 +310,18 @@ static function (Type $type): bool {
return $traverse($type);
});

$parameterTemplateTypes = [];
foreach ($originalParametersAcceptor->getParameters() as $parameter) {
TypeTraverser::map($parameter->getType(), static function (Type $type, callable $traverse) use (&$parameterTemplateTypes): Type {
if ($type instanceof TemplateType) {
$parameterTemplateTypes[$type->getName()] = true;
return $type;
}

return $traverse($type);
});
}

foreach ($parametersAcceptor->getResolvedTemplateTypeMap()->getTypes() as $name => $type) {
if (
!($type instanceof ErrorType)
Expand All @@ -325,6 +337,10 @@ static function (Type $type): bool {
continue;
}

if (!array_key_exists($name, $parameterTemplateTypes)) {
continue;
}

$errors[] = RuleErrorBuilder::message(sprintf($messages[9], $name))->line($funcCall->getLine())->tip('See: https://phpstan.org/blog/solving-phpstan-error-unable-to-resolve-template-type')->build();
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,10 @@ public function testOnlyRelevantUnableToResolveTemplateType(): void
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/only-relevant-unable-to-resolve-template-type.php'], [
[
'Parameter #1 $a of method OnlyRelevantUnableToResolve\Foo::doBaz() expects array, int given.',
41,
],
[
'Unable to resolve the template type T in call to method OnlyRelevantUnableToResolve\Foo::doBaz()',
41,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,33 @@ public function doBar()
/**
* @template T
* @template U
* @param T[] $a
* @return T
*/
public function doBaz()
public function doBaz($a)
{

}

public function doLorem()
{
$this->doFoo(1);
$this->doBar();
$this->doBaz();
$this->doBaz(1);
}

/**
* @template T
* @param mixed $a
* @return T
*/
public function doIpsum($a)
{

}

public function doDolor()
{
$this->doIpsum(1);
}

}

0 comments on commit cec9ef6

Please sign in to comment.