Adds brackets around each namespace if it's not already the case.
string $source Namespace string:
string Namespaces with brackets
public static function fixNamespaceDeclarations($source) {
if (!function_exists('token_get_all') || !self::$useTokenizer) {
if (preg_match('/namespace(.*?)\\s*;/', $source)) {
$source = preg_replace('/namespace(.*?)\\s*;/', "namespace\$1\n{", $source) . "}\n";
}
return $source;
}
$rawChunk = '';
$output = '';
$inNamespace = false;
$tokens = token_get_all($source);
for (reset($tokens); false !== ($token = current($tokens)); next($tokens)) {
if (is_string($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 (($t = next($tokens)) && is_array($t) && in_array($t[0], array(
T_WHITESPACE,
T_NS_SEPARATOR,
T_STRING,
))) {
$rawChunk .= $t[1];
}
if ('{' === $t) {
$inNamespace = false;
prev($tokens);
}
else {
$rawChunk = rtrim($rawChunk) . "\n{";
$inNamespace = true;
}
}
elseif (T_START_HEREDOC === $token[0]) {
$output .= self::compressCode($rawChunk) . $token[1];
do {
$token = next($tokens);
$output .= is_string($token) ? $token : $token[1];
} while ($token[0] !== T_END_HEREDOC);
$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";
}
return $output . self::compressCode($rawChunk);
}