src/main/php/NewInstance.php
private static function detectReturnType(\ReflectionMethod $method): ?string
    {
        if ($method->hasReturnType()) {
            return (string) $method->getReturnType();
        }

        $docComment = $method->getDocComment();
        if (false === $docComment) {
            return null;
        }

        $returnPart = strstr($docComment, '@return');
        if (false === $returnPart) {
            return null;
        }

        $returnParts = explode(' ', trim(str_replace('@return', '', $returnPart)));
        $returnType  = ltrim(trim($returnParts[0]), '\\');
        if (empty($returnType) || strpos($returnType, '*') !== false) {
            return null;
        }

        return $returnType;
    }
private static function detectReturnType(\ReflectionMethod $method): ?string
    {
        if ($method->hasReturnType()) {
            return (string) $method->getReturnType();
        }

        $docComment = $method->getDocComment();
        if (false === $docComment) {
            return null;
        }

        $returnPart = strstr($docComment, '@return');
        if (false === $returnPart) {
            return null;
        }

        $returnParts = explode(' ', trim(str_replace('@return', '', $returnPart)));
        $returnType  = ltrim(trim($returnParts[0]), '\\');
        if (empty($returnType) || strpos($returnType, '*') !== false) {
            return null;
        }

        return $returnType;
    }
private static function detectReturnType ( \ ReflectionMethod $ method ) : ? string { if ( $ method -> hasReturnType ( ) ) { return ( string ) $ method -> getReturnType ( ) ; } $ docComment = $ method -> getDocComment ( ) ; if ( false === $ docComment ) { return null ; } $ returnPart = strstr ( $ docComment , '@return' ) ; if ( false === $ returnPart ) { return null ; } $ returnParts = explode ( ' ' , trim ( str_replace ( '@return' , '' , $ returnPart ) ) ) ; $ returnType = ltrim ( trim ( $ returnParts [ 0 ] ) , '\\' ) ; if ( empty ( $ returnType ) || strpos ( $ returnType , '*' ) !== false ) { return null ; } return $ returnType ; }
detects return type of method

It will make use of reflection to detect the return type. In case this
does not yield a result the doc comment will be parsed for the return
annotation.

@param   \ReflectionMethod  $method
@return  string|null
detects return type of method