Skip to content

Commit a76e41e

Browse files
committed
Merge branch '5.1' into 5.2
* 5.1: [Messenger] StopWorkersCommand improve doc helper Added compatibility with PHPunit 9.5 do not apply the Valid constraint on scalar form data [Test] Reproduce issue with cascading validation [SecurityBundle] Don't use the container as resource type in fixtures. Fix bug with whitespace in Kernel::stripComments()
2 parents 57e8b0f + ac934a8 commit a76e41e

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
@@ -831,13 +831,18 @@ public static function stripComments(string $source)
831831
// replace multiple new lines with a single newline
832832
$rawChunk .= preg_replace(['/\n{2,}/S'], "\n", $token[1]);
833833
} elseif (\in_array($token[0], [\T_COMMENT, \T_DOC_COMMENT])) {
834+
if (!\in_array($rawChunk[\strlen($rawChunk) - 1], [' ', "\n", "\r", "\t"], true)) {
835+
$rawChunk .= ' ';
836+
}
834837
$ignoreSpace = true;
835838
} else {
836839
$rawChunk .= $token[1];
837840

838841
// The PHP-open tag already has a new-line
839842
if (\T_OPEN_TAG === $token[0]) {
840843
$ignoreSpace = true;
844+
} else {
845+
$ignoreSpace = false;
841846
}
842847
}
843848
}

Tests/KernelTest.php

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

229-
public function testStripComments()
229+
/**
230+
* @dataProvider getStripCommentsCodes
231+
*/
232+
public function testStripComments(string $source, string $expected)
233+
{
234+
$output = Kernel::stripComments($source);
235+
236+
// Heredocs are preserved, making the output mixing Unix and Windows line
237+
// endings, switching to "\n" everywhere on Windows to avoid failure.
238+
if ('\\' === \DIRECTORY_SEPARATOR) {
239+
$expected = str_replace("\r\n", "\n", $expected);
240+
$output = str_replace("\r\n", "\n", $output);
241+
}
242+
243+
$this->assertEquals($expected, $output);
244+
}
245+
246+
public function getStripCommentsCodes(): array
230247
{
231-
$source = <<<'EOF'
248+
return [
249+
['<?php echo foo();', '<?php echo foo();'],
250+
['<?php echo/**/foo();', '<?php echo foo();'],
251+
['<?php echo/** bar */foo();', '<?php echo foo();'],
252+
['<?php /**/echo foo();', '<?php echo foo();'],
253+
['<?php echo \foo();', '<?php echo \foo();'],
254+
['<?php echo/**/\foo();', '<?php echo \foo();'],
255+
['<?php echo/** bar */\foo();', '<?php echo \foo();'],
256+
['<?php /**/echo \foo();', '<?php echo \foo();'],
257+
[<<<'EOF'
232258
<?php
259+
include_once \dirname(__DIR__).'/foo.php';
233260
234261
$string = 'string should not be modified';
235262
@@ -267,9 +294,10 @@ public function doStuff()
267294
// inline comment
268295
}
269296
}
270-
EOF;
271-
$expected = <<<'EOF'
297+
EOF
298+
, <<<'EOF'
272299
<?php
300+
include_once \dirname(__DIR__).'/foo.php';
273301
$string = 'string should not be modified';
274302
$string = 'string should not be
275303
@@ -294,18 +322,9 @@ public function doStuff()
294322
{
295323
}
296324
}
297-
EOF;
298-
299-
$output = Kernel::stripComments($source);
300-
301-
// Heredocs are preserved, making the output mixing Unix and Windows line
302-
// endings, switching to "\n" everywhere on Windows to avoid failure.
303-
if ('\\' === \DIRECTORY_SEPARATOR) {
304-
$expected = str_replace("\r\n", "\n", $expected);
305-
$output = str_replace("\r\n", "\n", $output);
306-
}
307-
308-
$this->assertEquals($expected, $output);
325+
EOF
326+
],
327+
];
309328
}
310329

311330
public function testSerialize()

0 commit comments

Comments
 (0)