function PhpDumper::stripComments
Removes comments from a PHP source string.
We don't use the PHP php_strip_whitespace() function as we want the content to be readable and well-formatted.
1 call to PhpDumper::stripComments()
- PhpDumper::generateProxyClasses in vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php
File
-
vendor/
symfony/ dependency-injection/ Dumper/ PhpDumper.php, line 2364
Class
- PhpDumper
- PhpDumper dumps a service container as a PHP class.
Namespace
Symfony\Component\DependencyInjection\DumperCode
private static function stripComments(string $source) : string {
if (!\function_exists('token_get_all')) {
return $source;
}
$rawChunk = '';
$output = '';
$tokens = token_get_all($source);
$ignoreSpace = false;
for ($i = 0; isset($tokens[$i]); ++$i) {
$token = $tokens[$i];
if (!isset($token[1]) || 'b"' === $token) {
$rawChunk .= $token;
}
elseif (\T_START_HEREDOC === $token[0]) {
$output .= $rawChunk . $token[1];
do {
$token = $tokens[++$i];
$output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token;
} while (\T_END_HEREDOC !== $token[0]);
$rawChunk = '';
}
elseif (\T_WHITESPACE === $token[0]) {
if ($ignoreSpace) {
$ignoreSpace = false;
continue;
}
// replace multiple new lines with a single newline
$rawChunk .= preg_replace([
'/\\n{2,}/S',
], "\n", $token[1]);
}
elseif (\in_array($token[0], [
\T_COMMENT,
\T_DOC_COMMENT,
])) {
if (!\in_array($rawChunk[\strlen($rawChunk) - 1], [
' ',
"\n",
"\r",
"\t",
], true)) {
$rawChunk .= ' ';
}
$ignoreSpace = true;
}
else {
$rawChunk .= $token[1];
// The PHP-open tag already has a new-line
if (\T_OPEN_TAG === $token[0]) {
$ignoreSpace = true;
}
else {
$ignoreSpace = false;
}
}
}
$output .= $rawChunk;
unset($tokens, $rawChunk);
gc_mem_caches();
return $output;
}