public function getReturnTypeHintToken(File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();

        if (\defined('T_RETURN_TYPE') && $tokens[$stackPtr]['code'] === \T_RETURN_TYPE) {
            return $stackPtr;
        }

        if ($tokens[$stackPtr]['code'] !== \T_FUNCTION && $tokens[$stackPtr]['code'] !== \T_CLOSURE) {
            return false;
        }

        if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) {
            return false;
        }

        // Allow for interface and abstract method declarations.
        $endOfFunctionDeclaration = null;
        if (isset($tokens[$stackPtr]['scope_opener'])) {
            $endOfFunctionDeclaration = $tokens[$stackPtr]['scope_opener'];
        } else {
            $nextSemiColon = $phpcsFile->findNext(\T_SEMICOLON, ($tokens[$stackPtr]['parenthesis_closer'] + 1), null, false, null, true);
            if ($nextSemiColon !== false) {
                $endOfFunctionDeclaration = $nextSemiColon;
            }
        }

        if (isset($endOfFunctionDeclaration) === false) {
            return false;
        }

        $hasColon = $phpcsFile->findNext(
            array(\T_COLON, \T_INLINE_ELSE),
            ($tokens[$stackPtr]['parenthesis_closer'] + 1),
            $endOfFunctionDeclaration
        );
        if ($hasColon === false) {
            return false;
        }

        /*
         * - `self`, `parent` and `callable` are not being recognized as return types in PHPCS < 2.6.0.
         * - Return types are not recognized at all in PHPCS < 2.4.0.
         * - The T_RETURN_TYPE token is defined, but no longer in use since PHPCS 3.3.0+.
         *   The token will now be tokenized as T_STRING.
         * - An `array` (return) type declaration was tokenized as `T_ARRAY_HINT` in PHPCS 2.3.3 - 3.2.3
         *   to prevent confusing sniffs looking for array declarations.
         *   As of PHPCS 3.3.0 `array` as a type declaration will be tokenized as `T_STRING`.
         */
        $unrecognizedTypes = array(
            \T_CALLABLE,
            \T_SELF,
            \T_PARENT,
            \T_ARRAY, // PHPCS < 2.4.0.
            \T_STRING,
        );

        return $phpcsFile->findPrevious($unrecognizedTypes, ($endOfFunctionDeclaration - 1), $hasColon);
    }
public function getReturnTypeHintToken(File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();

        if (\defined('T_RETURN_TYPE') && $tokens[$stackPtr]['code'] === \T_RETURN_TYPE) {
            return $stackPtr;
        }

        if ($tokens[$stackPtr]['code'] !== \T_FUNCTION && $tokens[$stackPtr]['code'] !== \T_CLOSURE) {
            return false;
        }

        if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) {
            return false;
        }

        // Allow for interface and abstract method declarations.
        $endOfFunctionDeclaration = null;
        if (isset($tokens[$stackPtr]['scope_opener'])) {
            $endOfFunctionDeclaration = $tokens[$stackPtr]['scope_opener'];
        } else {
            $nextSemiColon = $phpcsFile->findNext(\T_SEMICOLON, ($tokens[$stackPtr]['parenthesis_closer'] + 1), null, false, null, true);
            if ($nextSemiColon !== false) {
                $endOfFunctionDeclaration = $nextSemiColon;
            }
        }

        if (isset($endOfFunctionDeclaration) === false) {
            return false;
        }

        $hasColon = $phpcsFile->findNext(
            array(\T_COLON, \T_INLINE_ELSE),
            ($tokens[$stackPtr]['parenthesis_closer'] + 1),
            $endOfFunctionDeclaration
        );
        if ($hasColon === false) {
            return false;
        }

        /*
         * - `self`, `parent` and `callable` are not being recognized as return types in PHPCS < 2.6.0.
         * - Return types are not recognized at all in PHPCS < 2.4.0.
         * - The T_RETURN_TYPE token is defined, but no longer in use since PHPCS 3.3.0+.
         *   The token will now be tokenized as T_STRING.
         * - An `array` (return) type declaration was tokenized as `T_ARRAY_HINT` in PHPCS 2.3.3 - 3.2.3
         *   to prevent confusing sniffs looking for array declarations.
         *   As of PHPCS 3.3.0 `array` as a type declaration will be tokenized as `T_STRING`.
         */
        $unrecognizedTypes = array(
            \T_CALLABLE,
            \T_SELF,
            \T_PARENT,
            \T_ARRAY, // PHPCS < 2.4.0.
            \T_STRING,
        );

        return $phpcsFile->findPrevious($unrecognizedTypes, ($endOfFunctionDeclaration - 1), $hasColon);
    }
public function getReturnTypeHintToken ( File $ phpcsFile , $ stackPtr ) { $ tokens = $ phpcsFile -> getTokens ( ) ; if ( \ defined ( 'T_RETURN_TYPE' ) && $ tokens [ $ stackPtr ] [ 'code' ] === \ T_RETURN_TYPE ) { return $ stackPtr ; } if ( $ tokens [ $ stackPtr ] [ 'code' ] !== \ T_FUNCTION && $ tokens [ $ stackPtr ] [ 'code' ] !== \ T_CLOSURE ) { return false ; } if ( isset ( $ tokens [ $ stackPtr ] [ 'parenthesis_closer' ] ) === false ) { return false ; } // Allow for interface and abstract method declarations. $ endOfFunctionDeclaration = null ; if ( isset ( $ tokens [ $ stackPtr ] [ 'scope_opener' ] ) ) { $ endOfFunctionDeclaration = $ tokens [ $ stackPtr ] [ 'scope_opener' ] ; } else { $ nextSemiColon = $ phpcsFile -> findNext ( \ T_SEMICOLON , ( $ tokens [ $ stackPtr ] [ 'parenthesis_closer' ] + 1 ) , null , false , null , true ) ; if ( $ nextSemiColon !== false ) { $ endOfFunctionDeclaration = $ nextSemiColon ; } } if ( isset ( $ endOfFunctionDeclaration ) === false ) { return false ; } $ hasColon = $ phpcsFile -> findNext ( array ( \ T_COLON , \ T_INLINE_ELSE ) , ( $ tokens [ $ stackPtr ] [ 'parenthesis_closer' ] + 1 ) , $ endOfFunctionDeclaration ) ; if ( $ hasColon === false ) { return false ; } /*
         * - `self`, `parent` and `callable` are not being recognized as return types in PHPCS < 2.6.0.
         * - Return types are not recognized at all in PHPCS < 2.4.0.
         * - The T_RETURN_TYPE token is defined, but no longer in use since PHPCS 3.3.0+.
         *   The token will now be tokenized as T_STRING.
         * - An `array` (return) type declaration was tokenized as `T_ARRAY_HINT` in PHPCS 2.3.3 - 3.2.3
         *   to prevent confusing sniffs looking for array declarations.
         *   As of PHPCS 3.3.0 `array` as a type declaration will be tokenized as `T_STRING`.
         */ $ unrecognizedTypes = array ( \ T_CALLABLE , \ T_SELF , \ T_PARENT , \ T_ARRAY , // PHPCS < 2.4.0. \ T_STRING , ) ; return $ phpcsFile -> findPrevious ( $ unrecognizedTypes , ( $ endOfFunctionDeclaration - 1 ) , $ hasColon ) ; }
Get the stack pointer for a return type token for a given function.

Compatible layer for older PHPCS versions which don't recognize
return type hints correctly.

Expects to be passed T_RETURN_TYPE, T_FUNCTION or T_CLOSURE token.

@param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
@param int                   $stackPtr  The position of the token.

@return int|false Stack pointer to the return type token or false if
no return type was found or the passed token was
not of the correct type.
Get the stack pointer for a return type token for a given function .