Skip to content

Commit

Permalink
fixed Kernel::stripComments() normalizing new-lines
Browse files Browse the repository at this point in the history
This makes normalizing new-lines less error-prone
when a string contains multiple new line-lines
  • Loading branch information
sstok committed Feb 28, 2014
1 parent e756686 commit 63032c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
24 changes: 20 additions & 4 deletions src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -759,23 +759,39 @@ public static function stripComments($source)
$rawChunk = '';
$output = '';
$tokens = token_get_all($source);
$ignoreSpace = false;
for (reset($tokens); false !== $token = current($tokens); next($tokens)) {
if (is_string($token)) {
$rawChunk .= $token;
} elseif (T_START_HEREDOC === $token[0]) {
$output .= preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $rawChunk).$token[1];
$output .= $rawChunk.$token[1];
do {
$token = next($tokens);
$output .= $token[1];
} while ($token[0] !== T_END_HEREDOC);
$rawChunk = '';
} elseif (!in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
} elseif (T_WHITESPACE === $token[0]) {
if ($ignoreSpace) {
$ignoreSpace = false;

continue;
}

// replace multiple new lines with a single newline
$rawChunk .= preg_replace(array('/\n{2,}/S'), "\n", $token[1]);
} elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
$ignoreSpace = true;
} else {
$rawChunk .= $token[1];

// The PHP-open tag already has a new-line
if (T_OPEN_TAG === $token[0]) {
$ignoreSpace = true;
}
}
}

// replace multiple new lines with a single newline
$output .= preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $rawChunk);
$output .= $rawChunk;

return $output;
}
Expand Down
15 changes: 10 additions & 5 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Expand Up @@ -274,6 +274,10 @@ public function testStripComments()
$string = 'string should not be modified';
$string = 'string should not be
modified';
$heredoc = <<<HD
Expand Down Expand Up @@ -308,16 +312,17 @@ public function doStuff()
$expected = <<<'EOF'
<?php
$string = 'string should not be modified';
$heredoc =
<<<HD
$string = 'string should not be
modified';
$heredoc = <<<HD
Heredoc should not be modified
HD;
$nowdoc =
<<<'ND'
$nowdoc = <<<'ND'
Nowdoc should not be modified
Expand All @@ -328,7 +333,7 @@ class TestClass
{
public function doStuff()
{
}
}
}
EOF;

Expand Down

0 comments on commit 63032c7

Please sign in to comment.