/**
* @param \PhpParser\Node\FunctionLike $function
* @param \PHPStan\Analyser\Scope $scope
* @param string $parameterMessage
* @param string $returnMessage
* @return string[]
*/
public function checkFunction(FunctionLike $function, Scope $scope, string $parameterMessage, string $returnMessage) : array
{
if ($function instanceof ClassMethod && $scope->getClass() !== null) {
if (!$this->broker->hasClass($scope->getClass())) {
return [];
}
return $this->checkParametersAcceptor($this->broker->getClass($scope->getClass())->getMethod($function->name), $parameterMessage, $returnMessage);
}
if ($function instanceof Function_) {
$functionName = $function->name;
if (isset($function->namespacedName)) {
$functionName = $function->namespacedName;
}
return $this->checkParametersAcceptor($this->broker->getFunction(new Name($functionName)), $parameterMessage, $returnMessage);
}
$errors = [];
foreach ($function->getParams() as $param) {
$class = $param->type instanceof NullableType ? (string) $param->type->type : (string) $param->type;
if ($class && !in_array($class, self::VALID_TYPEHINTS, true) && !$this->broker->hasClass($class)) {
$errors[] = sprintf($parameterMessage, $param->name, $class);
}
}
$returnType = $function->getReturnType() instanceof NullableType ? (string) $function->getReturnType()->type : (string) $function->getReturnType();
if ($returnType && !in_array($returnType, self::VALID_TYPEHINTS, true) && !$this->broker->hasClass($returnType)) {
$errors[] = sprintf($returnMessage, $returnType);
}
return $errors;
}