fixed Kernel::stripComments() normalizing new-lines
This makes normalizing new-lines less error-prone when a string contains multiple new line-lines
This commit is contained in:
parent
e756686bb4
commit
63032c7c89
@ -759,23 +759,39 @@ abstract class Kernel implements KernelInterface, TerminableInterface
|
||||
$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;
|
||||
}
|
||||
|
@ -274,6 +274,10 @@ class KernelTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$string = 'string should not be modified';
|
||||
|
||||
$string = 'string should not be
|
||||
|
||||
modified';
|
||||
|
||||
|
||||
$heredoc = <<<HD
|
||||
|
||||
@ -308,16 +312,17 @@ EOF;
|
||||
$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
|
||||
@ -328,7 +333,7 @@ class TestClass
|
||||
{
|
||||
public function doStuff()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
EOF;
|
||||
|
||||
|
Reference in New Issue
Block a user