File tree Expand file tree Collapse file tree 2 files changed +30
-9
lines changed
src/Symfony/Component/HttpKernel Expand file tree Collapse file tree 2 files changed +30
-9
lines changed Original file line number Diff line number Diff line change @@ -759,23 +759,39 @@ public static function stripComments($source)
759
759
$ rawChunk = '' ;
760
760
$ output = '' ;
761
761
$ tokens = token_get_all ($ source );
762
+ $ ignoreSpace = false ;
762
763
for (reset ($ tokens ); false !== $ token = current ($ tokens ); next ($ tokens )) {
763
764
if (is_string ($ token )) {
764
765
$ rawChunk .= $ token ;
765
766
} elseif (T_START_HEREDOC === $ token [0 ]) {
766
- $ output .= preg_replace ( array ( ' /\s+$/Sm ' , ' /\n+/S ' ), "\n" , $ rawChunk) .$ token [1 ];
767
+ $ output .= $ rawChunk .$ token [1 ];
767
768
do {
768
769
$ token = next ($ tokens );
769
770
$ output .= $ token [1 ];
770
771
} while ($ token [0 ] !== T_END_HEREDOC );
771
772
$ 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 {
773
785
$ 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
+ }
774
791
}
775
792
}
776
793
777
- // replace multiple new lines with a single newline
778
- $ output .= preg_replace (array ('/\s+$/Sm ' , '/\n+/S ' ), "\n" , $ rawChunk );
794
+ $ output .= $ rawChunk ;
779
795
780
796
return $ output ;
781
797
}
Original file line number Diff line number Diff line change @@ -274,6 +274,10 @@ public function testStripComments()
274
274
275
275
$string = 'string should not be modified';
276
276
277
+ $string = 'string should not be
278
+
279
+ modified';
280
+
277
281
278
282
$heredoc = <<<HD
279
283
@@ -308,16 +312,17 @@ public function doStuff()
308
312
$ expected = <<<'EOF'
309
313
<?php
310
314
$string = 'string should not be modified';
311
- $heredoc =
312
- <<<HD
315
+ $string = 'string should not be
316
+
317
+ modified';
318
+ $heredoc = <<<HD
313
319
314
320
315
321
Heredoc should not be modified
316
322
317
323
318
324
HD;
319
- $nowdoc =
320
- <<<'ND'
325
+ $nowdoc = <<<'ND'
321
326
322
327
323
328
Nowdoc should not be modified
@@ -328,7 +333,7 @@ class TestClass
328
333
{
329
334
public function doStuff()
330
335
{
331
- }
336
+ }
332
337
}
333
338
EOF;
334
339
You can’t perform that action at this time.
0 commit comments