Get the declared return type or null
public getReturnType ( ) : null | string | |
||
return | null | string | |
/** * @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; }
/** * Checks if the function has a specified return type * * @return bool * * @link http://php.net/manual/en/reflectionfunctionabstract.hasreturntype.php */ public function hasReturnType() { $returnType = $this->functionLikeNode->getReturnType(); return isset($returnType); }