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

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

        // Check for scoped namespace {}.
        if (empty($tokens[$stackPtr]['conditions']) === false) {
            $namespacePtr = $phpcsFile->getCondition($stackPtr, \T_NAMESPACE);
            if ($namespacePtr !== false) {
                $namespace = $this->getDeclaredNamespaceName($phpcsFile, $namespacePtr);
                if ($namespace !== false) {
                    return $namespace;
                }

                // We are in a scoped namespace, but couldn't determine the name. Searching for a global namespace is futile.
                return '';
            }
        }

        /*
         * Not in a scoped namespace, so let's see if we can find a non-scoped namespace instead.
         * Keeping in mind that:
         * - there can be multiple non-scoped namespaces in a file (bad practice, but it happens).
         * - the namespace keyword can also be used as part of a function/method call and such.
         * - that a non-named namespace resolves to the global namespace.
         */
        $previousNSToken = $stackPtr;
        $namespace       = false;
        do {
            $previousNSToken = $phpcsFile->findPrevious(\T_NAMESPACE, ($previousNSToken - 1));

            // Stop if we encounter a scoped namespace declaration as we already know we're not in one.
            if (empty($tokens[$previousNSToken]['scope_condition']) === false && $tokens[$previousNSToken]['scope_condition'] === $previousNSToken) {
                break;
            }

            $namespace = $this->getDeclaredNamespaceName($phpcsFile, $previousNSToken);

        } while ($namespace === false && $previousNSToken !== false);

        // If we still haven't got a namespace, return an empty string.
        if ($namespace === false) {
            return '';
        } else {
            return $namespace;
        }
    }
public function determineNamespace(File $phpcsFile, $stackPtr)
    {
        $tokens = $phpcsFile->getTokens();

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

        // Check for scoped namespace {}.
        if (empty($tokens[$stackPtr]['conditions']) === false) {
            $namespacePtr = $phpcsFile->getCondition($stackPtr, \T_NAMESPACE);
            if ($namespacePtr !== false) {
                $namespace = $this->getDeclaredNamespaceName($phpcsFile, $namespacePtr);
                if ($namespace !== false) {
                    return $namespace;
                }

                // We are in a scoped namespace, but couldn't determine the name. Searching for a global namespace is futile.
                return '';
            }
        }

        /*
         * Not in a scoped namespace, so let's see if we can find a non-scoped namespace instead.
         * Keeping in mind that:
         * - there can be multiple non-scoped namespaces in a file (bad practice, but it happens).
         * - the namespace keyword can also be used as part of a function/method call and such.
         * - that a non-named namespace resolves to the global namespace.
         */
        $previousNSToken = $stackPtr;
        $namespace       = false;
        do {
            $previousNSToken = $phpcsFile->findPrevious(\T_NAMESPACE, ($previousNSToken - 1));

            // Stop if we encounter a scoped namespace declaration as we already know we're not in one.
            if (empty($tokens[$previousNSToken]['scope_condition']) === false && $tokens[$previousNSToken]['scope_condition'] === $previousNSToken) {
                break;
            }

            $namespace = $this->getDeclaredNamespaceName($phpcsFile, $previousNSToken);

        } while ($namespace === false && $previousNSToken !== false);

        // If we still haven't got a namespace, return an empty string.
        if ($namespace === false) {
            return '';
        } else {
            return $namespace;
        }
    }
public function determineNamespace ( File $ phpcsFile , $ stackPtr ) { $ tokens = $ phpcsFile -> getTokens ( ) ; // Check for the existence of the token. if ( isset ( $ tokens [ $ stackPtr ] ) === false ) { return '' ; } // Check for scoped namespace {}. if ( empty ( $ tokens [ $ stackPtr ] [ 'conditions' ] ) === false ) { $ namespacePtr = $ phpcsFile -> getCondition ( $ stackPtr , \ T_NAMESPACE ) ; if ( $ namespacePtr !== false ) { $ namespace = $ this -> getDeclaredNamespaceName ( $ phpcsFile , $ namespacePtr ) ; if ( $ namespace !== false ) { return $ namespace ; } // We are in a scoped namespace, but couldn't determine the name. Searching for a global namespace is futile. return '' ; } } /*
         * Not in a scoped namespace, so let's see if we can find a non-scoped namespace instead.
         * Keeping in mind that:
         * - there can be multiple non-scoped namespaces in a file (bad practice, but it happens).
         * - the namespace keyword can also be used as part of a function/method call and such.
         * - that a non-named namespace resolves to the global namespace.
         */ $ previousNSToken = $ stackPtr ; $ namespace = false ; do { $ previousNSToken = $ phpcsFile -> findPrevious ( \ T_NAMESPACE , ( $ previousNSToken - 1 ) ) ; // Stop if we encounter a scoped namespace declaration as we already know we're not in one. if ( empty ( $ tokens [ $ previousNSToken ] [ 'scope_condition' ] ) === false && $ tokens [ $ previousNSToken ] [ 'scope_condition' ] === $ previousNSToken ) { break ; } $ namespace = $ this -> getDeclaredNamespaceName ( $ phpcsFile , $ previousNSToken ) ; } while ( $ namespace === false && $ previousNSToken !== false ) ; // If we still haven't got a namespace, return an empty string. if ( $ namespace === false ) { return '' ; } else { return $ namespace ; } }
Determine the namespace name an arbitrary token lives in.

@param \PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile.
@param int                   $stackPtr  The token position for which to determine the namespace.

@return string Namespace name or empty string if it couldn't be determined or no namespace applies.
Determine the namespace name an arbitrary token lives in .