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

        // Check for the existence of the token.
        if ($stackPtr === false || isset($tokens[$stackPtr]) === false) {
            return false;
        }

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

        if ($tokens[($stackPtr + 1)]['code'] === \T_NS_SEPARATOR) {
            // Not a namespace declaration, but use of, i.e. namespace\someFunction();
            return false;
        }

        $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true);
        if ($tokens[$nextToken]['code'] === \T_OPEN_CURLY_BRACKET) {
            // Declaration for global namespace when using multiple namespaces in a file.
            // I.e.: namespace {}
            return '';
        }

        // Ok, this should be a namespace declaration, so get all the parts together.
        $validTokens = array(
            \T_STRING       => true,
            \T_NS_SEPARATOR => true,
            \T_WHITESPACE   => true,
        );

        $namespaceName = '';
        while (isset($validTokens[$tokens[$nextToken]['code']]) === true) {
            $namespaceName .= trim($tokens[$nextToken]['content']);
            $nextToken++;
        }

        return $namespaceName;
    }
public function getDeclaredNamespaceName(File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();

        // Check for the existence of the token.
        if ($stackPtr === false || isset($tokens[$stackPtr]) === false) {
            return false;
        }

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

        if ($tokens[($stackPtr + 1)]['code'] === \T_NS_SEPARATOR) {
            // Not a namespace declaration, but use of, i.e. namespace\someFunction();
            return false;
        }

        $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true);
        if ($tokens[$nextToken]['code'] === \T_OPEN_CURLY_BRACKET) {
            // Declaration for global namespace when using multiple namespaces in a file.
            // I.e.: namespace {}
            return '';
        }

        // Ok, this should be a namespace declaration, so get all the parts together.
        $validTokens = array(
            \T_STRING       => true,
            \T_NS_SEPARATOR => true,
            \T_WHITESPACE   => true,
        );

        $namespaceName = '';
        while (isset($validTokens[$tokens[$nextToken]['code']]) === true) {
            $namespaceName .= trim($tokens[$nextToken]['content']);
            $nextToken++;
        }

        return $namespaceName;
    }
public function getDeclaredNamespaceName ( File $ phpcsFile , $ stackPtr ) { $ tokens = $ phpcsFile -> getTokens ( ) ; // Check for the existence of the token. if ( $ stackPtr === false || isset ( $ tokens [ $ stackPtr ] ) === false ) { return false ; } if ( $ tokens [ $ stackPtr ] [ 'code' ] !== \ T_NAMESPACE ) { return false ; } if ( $ tokens [ ( $ stackPtr + 1 ) ] [ 'code' ] === \ T_NS_SEPARATOR ) { // Not a namespace declaration, but use of, i.e. namespace\someFunction(); return false ; } $ nextToken = $ phpcsFile -> findNext ( Tokens :: $ emptyTokens , ( $ stackPtr + 1 ) , null , true , null , true ) ; if ( $ tokens [ $ nextToken ] [ 'code' ] === \ T_OPEN_CURLY_BRACKET ) { // Declaration for global namespace when using multiple namespaces in a file. // I.e.: namespace {} return '' ; } // Ok, this should be a namespace declaration, so get all the parts together. $ validTokens = array ( \ T_STRING => true , \ T_NS_SEPARATOR => true , \ T_WHITESPACE => true , ) ; $ namespaceName = '' ; while ( isset ( $ validTokens [ $ tokens [ $ nextToken ] [ 'code' ] ] ) === true ) { $ namespaceName .= trim ( $ tokens [ $ nextToken ] [ 'content' ] ) ; $ nextToken ++ ; } return $ namespaceName ; }
Get the complete namespace name for a namespace declaration.

For hierarchical namespaces, the name will be composed of several tokens,
i.e. MyProject\Sub\Level which will be returned together as one string.

@param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile.
@param int|bool              $stackPtr  The position of a T_NAMESPACE token.

@return string|false Namespace name or false if not a namespace declaration.
Namespace name can be an empty string for global namespace declaration.
Get the complete namespace name for a namespace declaration .