Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

take return-type into account while TemplateType validation #893

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/Rules/FunctionDefinitionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function checkFunction(
string $parameterMessage,
string $returnMessage,
string $unionTypesMessage,
string $templateTypeMissingInParameterMessage,
string $unusedTemplateTypeMessage,
string $unresolvableParameterTypeMessage,
string $unresolvableReturnTypeMessage,
): array
Expand All @@ -90,7 +90,7 @@ public function checkFunction(
$parameterMessage,
$returnMessage,
$unionTypesMessage,
$templateTypeMissingInParameterMessage,
$unusedTemplateTypeMessage,
$unresolvableParameterTypeMessage,
$unresolvableReturnTypeMessage,
);
Expand Down Expand Up @@ -204,7 +204,7 @@ public function checkClassMethod(
string $parameterMessage,
string $returnMessage,
string $unionTypesMessage,
string $templateTypeMissingInParameterMessage,
string $unusedTemplateTypeMessage,
string $unresolvableParameterTypeMessage,
string $unresolvableReturnTypeMessage,
): array
Expand All @@ -218,7 +218,7 @@ public function checkClassMethod(
$parameterMessage,
$returnMessage,
$unionTypesMessage,
$templateTypeMissingInParameterMessage,
$unusedTemplateTypeMessage,
$unresolvableParameterTypeMessage,
$unresolvableReturnTypeMessage,
);
Expand All @@ -233,7 +233,7 @@ private function checkParametersAcceptor(
string $parameterMessage,
string $returnMessage,
string $unionTypesMessage,
string $templateTypeMissingInParameterMessage,
string $unusedTemplateTypeMessage,
string $unresolvableParameterTypeMessage,
string $unresolvableReturnTypeMessage,
): array
Expand Down Expand Up @@ -353,8 +353,17 @@ private function checkParametersAcceptor(
});
}

TypeTraverser::map($parametersAcceptor->getReturnType(), static function (Type $type, callable $traverse) use (&$templateTypes): Type {
if ($type instanceof TemplateType) {
unset($templateTypes[$type->getName()]);
return $traverse($type);
}

return $traverse($type);
});

foreach (array_keys($templateTypes) as $templateTypeName) {
$errors[] = RuleErrorBuilder::message(sprintf($templateTypeMissingInParameterMessage, $templateTypeName))->build();
$errors[] = RuleErrorBuilder::message(sprintf($unusedTemplateTypeMessage, $templateTypeName))->build();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Functions/ExistingClassesInTypehintsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function processNode(Node $node, Scope $scope): array
$functionName,
),
sprintf('Function %s() uses native union types but they\'re supported only on PHP 8.0 and later.', $functionName),
sprintf('Template type %%s of function %s() is not referenced in a parameter.', $functionName),
sprintf('Template type %%s of function %s() is neither referenced in a parameter nor in the return type.', $functionName),
sprintf(
'Parameter $%%s of function %s() has unresolvable native type.',
$functionName,
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Methods/ExistingClassesInTypehintsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function processNode(Node $node, Scope $scope): array
$methodName,
),
sprintf('Method %s::%s() uses native union types but they\'re supported only on PHP 8.0 and later.', $className, $methodName),
sprintf('Template type %%s of method %s::%s() is not referenced in a parameter.', $className, $methodName),
sprintf('Template type %%s of method %s::%s() is neither referenced in a parameter nor in the return type.', $className, $methodName),
sprintf(
'Parameter $%%s of method %s::%s() has unresolvable native type.',
$className,
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Generics/data/variance-0.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[
{
"message": "Template type T of function PHPStan\\Generics\\Variance\\returnOut() is not referenced in a parameter.",
"message": "Template type T of function PHPStan\\Generics\\Variance\\returnOut() is neither referenced in a parameter nor in the return type.",
"line": 109,
"ignorable": true
},
{
"message": "Template type T of function PHPStan\\Generics\\Variance\\returnInvariant() is not referenced in a parameter.",
"message": "Template type T of function PHPStan\\Generics\\Variance\\returnInvariant() is neither referenced in a parameter nor in the return type.",
"line": 117,
"ignorable": true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function testExistingClassInTypehint(): void
87,
],
[
'Template type T of function TestFunctionTypehints\templateTypeMissingInParameter() is not referenced in a parameter.',
'Template type T of function TestFunctionTypehints\templateTypeMissingInParameter() is neither referenced in a parameter nor in the return type.',
96,
],
]);
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Functions/data/typehints.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,12 @@ function templateTypeMissingInParameter(string $a)
{

}

/**
* @template T
* @return class-string<T>
*/
function genericTemplateReturnClassString(string $string)
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function testExistingClassInTypehint(): void
113,
],
[
'Template type U of method TestMethodTypehints\TemplateTypeMissingInParameter::doFoo() is not referenced in a parameter.',
'Template type U of method TestMethodTypehints\TemplateTypeMissingInParameter::doFoo() is neither referenced in a parameter nor in the return type.',
130,
],
]);
Expand Down Expand Up @@ -242,7 +242,7 @@ public function testBug4641(): void
{
$this->analyse([__DIR__ . '/data/bug-4641.php'], [
[
'Template type U of method Bug4641\I::getRepository() is not referenced in a parameter.',
'Template type U of method Bug4641\I::getRepository() is neither referenced in a parameter nor in the return type.',
26,
],
]);
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Methods/data/typehints.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,13 @@ public function doBar(string $class): void

}

/**
* @template U of object
* @return class-string<U>
*/
public function doBarReturn(string $class): string
{
return get_class($class);
}

}