Skip to content

Commit

Permalink
Report only relevant unable to resolve template type message
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 12, 2021
1 parent 959b6e9 commit a5f9f0c
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/Reflection/ResolvedFunctionVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public function __construct(
$this->resolvedTemplateTypeMap = $resolvedTemplateTypeMap;
}

public function getOriginalParametersAcceptor(): ParametersAcceptor
{
return $this->parametersAcceptor;
}

public function getTemplateTypeMap(): TemplateTypeMap
{
return $this->parametersAcceptor->getTemplateTypeMap();
Expand Down
20 changes: 19 additions & 1 deletion src/Rules/FunctionCallParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\ResolvedFunctionVariant;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
Expand Down Expand Up @@ -295,12 +298,27 @@ static function (Type $type): bool {
))->line($argumentLine)->build();
}

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

return $traverse($type);
});

foreach ($parametersAcceptor->getResolvedTemplateTypeMap()->getTypes() as $name => $type) {
if (!($type instanceof ErrorType) && !($type instanceof NeverType)) {
continue;
}

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

$errors[] = RuleErrorBuilder::message(sprintf($messages[9], $name))->line($funcCall->getLine())->build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,6 @@ public function testGenericFunction(): void
'Unable to resolve the template type A in call to function CallGenericFunction\f',
15,
],
[
'Unable to resolve the template type B in call to function CallGenericFunction\f',
15,
],
[
'Parameter #1 $a of function CallGenericFunction\g expects DateTime, DateTimeImmutable given.',
26,
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Rules/Functions/data/call-generic-function.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* @template B
* @param int|array<A> $a
* @param int|array<B> $b
* @return A[]
*/
function f($a, $b): void {
}
function f($a, $b): array {}

function test(): void {
f(1, 2);
Expand All @@ -18,9 +18,9 @@ function test(): void {
/**
* @template A of \DateTime
* @param A $a
* @return A
*/
function g($a): void {
}
function g($a) {}

function testg(): void {
g(new \DateTimeImmutable());
Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1750,4 +1750,17 @@ public function testBug4188(): void
$this->analyse([__DIR__ . '/data/bug-4188.php'], []);
}

public function testOnlyRelevantUnableToResolveTemplateType(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/only-relevant-unable-to-resolve-template-type.php'], [
[
'Unable to resolve the template type T in call to method OnlyRelevantUnableToResolve\Foo::doBaz()',
41,
],
]);
}

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Methods/data/call-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -1477,10 +1477,10 @@ class ClassStringWithUpperBounds
* @template T of \Exception
* @param class-string<T> $s
* @param T $object
* @return T
*/
public function doFoo(string $s, $object)
{

}

public function doBar(\Throwable $t)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace OnlyRelevantUnableToResolve;

class Foo
{

/**
* @template T
* @param T $a
* @return T[]
*/
public function doFoo($a)
{

}

/**
* @template T
* @return int[]
*/
public function doBar()
{

}

/**
* @template T
* @template U
* @return T
*/
public function doBaz()
{

}

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

}

0 comments on commit a5f9f0c

Please sign in to comment.