PHPStan\Rules\FunctionDefinitionCheck::checkFunction PHP Method

checkFunction() public method

public checkFunction ( PhpParser\Node\FunctionLike $function, Scope $scope, string $parameterMessage, string $returnMessage ) : array
$function PhpParser\Node\FunctionLike
$scope PHPStan\Analyser\Scope
$parameterMessage string
$returnMessage string
return array
    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;
    }

Usage Example

 /**
  * @param \PhpParser\Node\Stmt\ClassMethod $node
  * @param \PHPStan\Analyser\Scope $scope
  * @return string[]
  */
 public function processNode(Node $node, Scope $scope) : array
 {
     return $this->check->checkFunction($node, $scope, sprintf('Parameter $%%s of method %s::%s() has invalid typehint type %%s.', $scope->getClass(), $node->name), sprintf('Return typehint of method %s::%s() has invalid type %%s.', $scope->getClass(), $node->name));
 }
All Usage Examples Of PHPStan\Rules\FunctionDefinitionCheck::checkFunction