Skip to content

Commit a4f0f0c

Browse files
committed
bug symfony#10351 [HttpKernel] fix stripComments() normalizing new-lines (sstok)
This PR was merged into the 2.3 branch. Discussion ---------- [HttpKernel] fix stripComments() normalizing new-lines | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#10227 | License | MIT | Doc PR | This makes normalizing new-lines less error-prone when a string contains multiple new line-lines Commits ------- 63032c7 fixed Kernel::stripComments() normalizing new-lines
2 parents e756686 + 63032c7 commit a4f0f0c

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,23 +759,39 @@ public static function stripComments($source)
759759
$rawChunk = '';
760760
$output = '';
761761
$tokens = token_get_all($source);
762+
$ignoreSpace = false;
762763
for (reset($tokens); false !== $token = current($tokens); next($tokens)) {
763764
if (is_string($token)) {
764765
$rawChunk .= $token;
765766
} elseif (T_START_HEREDOC === $token[0]) {
766-
$output .= preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $rawChunk).$token[1];
767+
$output .= $rawChunk.$token[1];
767768
do {
768769
$token = next($tokens);
769770
$output .= $token[1];
770771
} while ($token[0] !== T_END_HEREDOC);
771772
$rawChunk = '';
772-
} elseif (!in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
773+
} elseif (T_WHITESPACE === $token[0]) {
774+
if ($ignoreSpace) {
775+
$ignoreSpace = false;
776+
777+
continue;
778+
}
779+
780+
// replace multiple new lines with a single newline
781+
$rawChunk .= preg_replace(array('/\n{2,}/S'), "\n", $token[1]);
782+
} elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
783+
$ignoreSpace = true;
784+
} else {
773785
$rawChunk .= $token[1];
786+
787+
// The PHP-open tag already has a new-line
788+
if (T_OPEN_TAG === $token[0]) {
789+
$ignoreSpace = true;
790+
}
774791
}
775792
}
776793

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

780796
return $output;
781797
}

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ public function testStripComments()
274274
275275
$string = 'string should not be modified';
276276
277+
$string = 'string should not be
278+
279+
modified';
280+
277281
278282
$heredoc = <<<HD
279283
@@ -308,16 +312,17 @@ public function doStuff()
308312
$expected = <<<'EOF'
309313
<?php
310314
$string = 'string should not be modified';
311-
$heredoc =
312-
<<<HD
315+
$string = 'string should not be
316+
317+
modified';
318+
$heredoc = <<<HD
313319
314320
315321
Heredoc should not be modified
316322
317323
318324
HD;
319-
$nowdoc =
320-
<<<'ND'
325+
$nowdoc = <<<'ND'
321326
322327
323328
Nowdoc should not be modified
@@ -328,7 +333,7 @@ class TestClass
328333
{
329334
public function doStuff()
330335
{
331-
}
336+
}
332337
}
333338
EOF;
334339

0 commit comments

Comments
 (0)