Skip to content

Commit 9a66282

Browse files
committed
Fix bug with whitespace in Kernel::stripComments()
1 parent 2a35d14 commit 9a66282

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

Kernel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,13 +858,18 @@ public static function stripComments($source)
858858
// replace multiple new lines with a single newline
859859
$rawChunk .= preg_replace(['/\n{2,}/S'], "\n", $token[1]);
860860
} elseif (\in_array($token[0], [\T_COMMENT, \T_DOC_COMMENT])) {
861+
if (!\in_array($rawChunk[\strlen($rawChunk) - 1], [' ', "\n", "\r", "\t"], true)) {
862+
$rawChunk .= ' ';
863+
}
861864
$ignoreSpace = true;
862865
} else {
863866
$rawChunk .= $token[1];
864867

865868
// The PHP-open tag already has a new-line
866869
if (\T_OPEN_TAG === $token[0]) {
867870
$ignoreSpace = true;
871+
} else {
872+
$ignoreSpace = false;
868873
}
869874
}
870875
}

Tests/KernelTest.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,37 @@ public function testHandleBootsTheKernel()
239239
$kernel->handle($request, $type, $catch);
240240
}
241241

242-
public function testStripComments()
242+
/**
243+
* @dataProvider getStripCommentsCodes
244+
*/
245+
public function testStripComments(string $source, string $expected)
246+
{
247+
$output = Kernel::stripComments($source);
248+
249+
// Heredocs are preserved, making the output mixing Unix and Windows line
250+
// endings, switching to "\n" everywhere on Windows to avoid failure.
251+
if ('\\' === \DIRECTORY_SEPARATOR) {
252+
$expected = str_replace("\r\n", "\n", $expected);
253+
$output = str_replace("\r\n", "\n", $output);
254+
}
255+
256+
$this->assertEquals($expected, $output);
257+
}
258+
259+
public function getStripCommentsCodes(): array
243260
{
244-
$source = <<<'EOF'
261+
return [
262+
['<?php echo foo();', '<?php echo foo();'],
263+
['<?php echo/**/foo();', '<?php echo foo();'],
264+
['<?php echo/** bar */foo();', '<?php echo foo();'],
265+
['<?php /**/echo foo();', '<?php echo foo();'],
266+
['<?php echo \foo();', '<?php echo \foo();'],
267+
['<?php echo/**/\foo();', '<?php echo \foo();'],
268+
['<?php echo/** bar */\foo();', '<?php echo \foo();'],
269+
['<?php /**/echo \foo();', '<?php echo \foo();'],
270+
[<<<'EOF'
245271
<?php
272+
include_once \dirname(__DIR__).'/foo.php';
246273
247274
$string = 'string should not be modified';
248275
@@ -280,9 +307,10 @@ public function doStuff()
280307
// inline comment
281308
}
282309
}
283-
EOF;
284-
$expected = <<<'EOF'
310+
EOF
311+
, <<<'EOF'
285312
<?php
313+
include_once \dirname(__DIR__).'/foo.php';
286314
$string = 'string should not be modified';
287315
$string = 'string should not be
288316
@@ -307,18 +335,9 @@ public function doStuff()
307335
{
308336
}
309337
}
310-
EOF;
311-
312-
$output = Kernel::stripComments($source);
313-
314-
// Heredocs are preserved, making the output mixing Unix and Windows line
315-
// endings, switching to "\n" everywhere on Windows to avoid failure.
316-
if ('\\' === \DIRECTORY_SEPARATOR) {
317-
$expected = str_replace("\r\n", "\n", $expected);
318-
$output = str_replace("\r\n", "\n", $output);
319-
}
320-
321-
$this->assertEquals($expected, $output);
338+
EOF
339+
],
340+
];
322341
}
323342

324343
/**

0 commit comments

Comments
 (0)