Skip to content

Commit ac934a8

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: [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 9b836ad + 9a66282 commit ac934a8

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
@@ -791,13 +791,18 @@ public static function stripComments(string $source)
791791
// replace multiple new lines with a single newline
792792
$rawChunk .= preg_replace(['/\n{2,}/S'], "\n", $token[1]);
793793
} elseif (\in_array($token[0], [\T_COMMENT, \T_DOC_COMMENT])) {
794+
if (!\in_array($rawChunk[\strlen($rawChunk) - 1], [' ', "\n", "\r", "\t"], true)) {
795+
$rawChunk .= ' ';
796+
}
794797
$ignoreSpace = true;
795798
} else {
796799
$rawChunk .= $token[1];
797800

798801
// The PHP-open tag already has a new-line
799802
if (\T_OPEN_TAG === $token[0]) {
800803
$ignoreSpace = true;
804+
} else {
805+
$ignoreSpace = false;
801806
}
802807
}
803808
}

Tests/KernelTest.php

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

230-
public function testStripComments()
230+
/**
231+
* @dataProvider getStripCommentsCodes
232+
*/
233+
public function testStripComments(string $source, string $expected)
234+
{
235+
$output = Kernel::stripComments($source);
236+
237+
// Heredocs are preserved, making the output mixing Unix and Windows line
238+
// endings, switching to "\n" everywhere on Windows to avoid failure.
239+
if ('\\' === \DIRECTORY_SEPARATOR) {
240+
$expected = str_replace("\r\n", "\n", $expected);
241+
$output = str_replace("\r\n", "\n", $output);
242+
}
243+
244+
$this->assertEquals($expected, $output);
245+
}
246+
247+
public function getStripCommentsCodes(): array
231248
{
232-
$source = <<<'EOF'
249+
return [
250+
['<?php echo foo();', '<?php echo foo();'],
251+
['<?php echo/**/foo();', '<?php echo foo();'],
252+
['<?php echo/** bar */foo();', '<?php echo foo();'],
253+
['<?php /**/echo foo();', '<?php echo foo();'],
254+
['<?php echo \foo();', '<?php echo \foo();'],
255+
['<?php echo/**/\foo();', '<?php echo \foo();'],
256+
['<?php echo/** bar */\foo();', '<?php echo \foo();'],
257+
['<?php /**/echo \foo();', '<?php echo \foo();'],
258+
[<<<'EOF'
233259
<?php
260+
include_once \dirname(__DIR__).'/foo.php';
234261
235262
$string = 'string should not be modified';
236263
@@ -268,9 +295,10 @@ public function doStuff()
268295
// inline comment
269296
}
270297
}
271-
EOF;
272-
$expected = <<<'EOF'
298+
EOF
299+
, <<<'EOF'
273300
<?php
301+
include_once \dirname(__DIR__).'/foo.php';
274302
$string = 'string should not be modified';
275303
$string = 'string should not be
276304
@@ -295,18 +323,9 @@ public function doStuff()
295323
{
296324
}
297325
}
298-
EOF;
299-
300-
$output = Kernel::stripComments($source);
301-
302-
// Heredocs are preserved, making the output mixing Unix and Windows line
303-
// endings, switching to "\n" everywhere on Windows to avoid failure.
304-
if ('\\' === \DIRECTORY_SEPARATOR) {
305-
$expected = str_replace("\r\n", "\n", $expected);
306-
$output = str_replace("\r\n", "\n", $output);
307-
}
308-
309-
$this->assertEquals($expected, $output);
326+
EOF
327+
],
328+
];
310329
}
311330

312331
public function testSerialize()

0 commit comments

Comments
 (0)