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')) {
return $source;
}
$output = '';
$inNamespace = false;
$tokens = token_get_all($source);
for ($i = 0, $max = count($tokens); $i < $max; $i++) {
$token = $tokens[$i];
if (is_string($token)) {
$output .= $token;
}
elseif (in_array($token[0], array(
T_COMMENT,
T_DOC_COMMENT,
))) {
// strip comments
continue;
}
elseif (T_NAMESPACE === $token[0]) {
if ($inNamespace) {
$output .= "}\n";
}
$output .= $token[1];
// namespace name and whitespaces
while (($t = $tokens[++$i]) && is_array($t) && in_array($t[0], array(
T_WHITESPACE,
T_NS_SEPARATOR,
T_STRING,
))) {
$output .= $t[1];
}
if (is_string($t) && '{' === $t) {
$inNamespace = false;
--$i;
}
else {
$output = rtrim($output);
$output .= "\n{";
$inNamespace = true;
}
}
else {
$output .= $token[1];
}
}
if ($inNamespace) {
$output .= "}\n";
}
return $output;
}