public static function fixNamespaceDeclarations($source)
    {
        if (!function_exists('token_get_all') || !self::$useTokenizer) {
            if (preg_match('/(^|\s)namespace(.*?)\s*;/', $source)) {
                $source = preg_replace('/(^|\s)namespace(.*?)\s*;/', "$1namespace$2\n{", $source)."}\n";
            }

            return $source;
        }

        $rawChunk = '';
        $output = '';
        $inNamespace = false;
        $tokens = token_get_all($source);

        for ($i = 0; isset($tokens[$i]); ++$i) {
            $token = $tokens[$i];
            if (!isset($token[1]) || 'b"' === $token) {
                $rawChunk .= $token;
            } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
                // strip comments
                continue;
            } elseif (T_NAMESPACE === $token[0]) {
                if ($inNamespace) {
                    $rawChunk .= "}\n";
                }
                $rawChunk .= $token[1];

                // namespace name and whitespaces
                while (isset($tokens[++$i][1]) && in_array($tokens[$i][0], array(T_WHITESPACE, T_NS_SEPARATOR, T_STRING))) {
                    $rawChunk .= $tokens[$i][1];
                }
                if ('{' === $tokens[$i]) {
                    $inNamespace = false;
                    --$i;
                } else {
                    $rawChunk = rtrim($rawChunk)."\n{";
                    $inNamespace = true;
                }
            } elseif (T_START_HEREDOC === $token[0]) {
                $output .= self::compressCode($rawChunk).$token[1];
                do {
                    $token = $tokens[++$i];
                    $output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token;
                } while (T_END_HEREDOC !== $token[0]);
                $output .= "\n";
                $rawChunk = '';
            } elseif (T_CONSTANT_ENCAPSED_STRING === $token[0]) {
                $output .= self::compressCode($rawChunk).$token[1];
                $rawChunk = '';
            } else {
                $rawChunk .= $token[1];
            }
        }

        if ($inNamespace) {
            $rawChunk .= "}\n";
        }

        $output .= self::compressCode($rawChunk);

        if (\PHP_VERSION_ID >= 70000) {
            // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
            unset($tokens, $rawChunk);
            gc_mem_caches();
        }

        return $output;
    }
public static function fixNamespaceDeclarations($source)
    {
        if (!function_exists('token_get_all') || !self::$useTokenizer) {
            if (preg_match('/(^|\s)namespace(.*?)\s*;/', $source)) {
                $source = preg_replace('/(^|\s)namespace(.*?)\s*;/', "$1namespace$2\n{", $source)."}\n";
            }

            return $source;
        }

        $rawChunk = '';
        $output = '';
        $inNamespace = false;
        $tokens = token_get_all($source);

        for ($i = 0; isset($tokens[$i]); ++$i) {
            $token = $tokens[$i];
            if (!isset($token[1]) || 'b"' === $token) {
                $rawChunk .= $token;
            } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
                // strip comments
                continue;
            } elseif (T_NAMESPACE === $token[0]) {
                if ($inNamespace) {
                    $rawChunk .= "}\n";
                }
                $rawChunk .= $token[1];

                // namespace name and whitespaces
                while (isset($tokens[++$i][1]) && in_array($tokens[$i][0], array(T_WHITESPACE, T_NS_SEPARATOR, T_STRING))) {
                    $rawChunk .= $tokens[$i][1];
                }
                if ('{' === $tokens[$i]) {
                    $inNamespace = false;
                    --$i;
                } else {
                    $rawChunk = rtrim($rawChunk)."\n{";
                    $inNamespace = true;
                }
            } elseif (T_START_HEREDOC === $token[0]) {
                $output .= self::compressCode($rawChunk).$token[1];
                do {
                    $token = $tokens[++$i];
                    $output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token;
                } while (T_END_HEREDOC !== $token[0]);
                $output .= "\n";
                $rawChunk = '';
            } elseif (T_CONSTANT_ENCAPSED_STRING === $token[0]) {
                $output .= self::compressCode($rawChunk).$token[1];
                $rawChunk = '';
            } else {
                $rawChunk .= $token[1];
            }
        }

        if ($inNamespace) {
            $rawChunk .= "}\n";
        }

        $output .= self::compressCode($rawChunk);

        if (\PHP_VERSION_ID >= 70000) {
            // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
            unset($tokens, $rawChunk);
            gc_mem_caches();
        }

        return $output;
    }
public static function fixNamespaceDeclarations ( $ source ) { if ( ! function_exists ( 'token_get_all' ) || ! self :: $ useTokenizer ) { if ( preg_match ( '/(^|\s)namespace(.*?)\s*;/' , $ source ) ) { $ source = preg_replace ( '/(^|\s)namespace(.*?)\s*;/' , "$1namespace$2\n{" , $ source ) . "}\n" ; } return $ source ; } $ rawChunk = '' ; $ output = '' ; $ inNamespace = false ; $ tokens = token_get_all ( $ source ) ; for ( $ i = 0 ; isset ( $ tokens [ $ i ] ) ; ++ $ i ) { $ token = $ tokens [ $ i ] ; if ( ! isset ( $ token [ 1 ] ) || 'b"' === $ token ) { $ rawChunk .= $ token ; } elseif ( in_array ( $ token [ 0 ] , array ( T_COMMENT , T_DOC_COMMENT ) ) ) { // strip comments continue ; } elseif ( T_NAMESPACE === $ token [ 0 ] ) { if ( $ inNamespace ) { $ rawChunk .= "}\n" ; } $ rawChunk .= $ token [ 1 ] ; // namespace name and whitespaces while ( isset ( $ tokens [ ++ $ i ] [ 1 ] ) && in_array ( $ tokens [ $ i ] [ 0 ] , array ( T_WHITESPACE , T_NS_SEPARATOR , T_STRING ) ) ) { $ rawChunk .= $ tokens [ $ i ] [ 1 ] ; } if ( '{' === $ tokens [ $ i ] ) { $ inNamespace = false ; -- $ i ; } else { $ rawChunk = rtrim ( $ rawChunk ) . "\n{" ; $ inNamespace = true ; } } elseif ( T_START_HEREDOC === $ token [ 0 ] ) { $ output .= self :: compressCode ( $ rawChunk ) . $ token [ 1 ] ; do { $ token = $ tokens [ ++ $ i ] ; $ output .= isset ( $ token [ 1 ] ) && 'b"' !== $ token ? $ token [ 1 ] : $ token ; } while ( T_END_HEREDOC !== $ token [ 0 ] ) ; $ output .= "\n" ; $ rawChunk = '' ; } elseif ( T_CONSTANT_ENCAPSED_STRING === $ token [ 0 ] ) { $ output .= self :: compressCode ( $ rawChunk ) . $ token [ 1 ] ; $ rawChunk = '' ; } else { $ rawChunk .= $ token [ 1 ] ; } } if ( $ inNamespace ) { $ rawChunk .= "}\n" ; } $ output .= self :: compressCode ( $ rawChunk ) ; if ( \ PHP_VERSION_ID >= 70000 ) { // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 unset ( $ tokens , $ rawChunk ) ; gc_mem_caches ( ) ; } return $ output ; }
Adds brackets around each namespace if it's not already the case.

@param string $source Namespace string

@return string Namespaces with brackets
Adds brackets around each namespace if it s not already the case .