Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cdb1883

Browse filesBrowse files
committed
bug #39220 [HttpKernel] Fix bug with whitespace in Kernel::stripComments() (ausi)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpKernel] Fix bug with whitespace in Kernel::stripComments() | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | `Kernel::stripComments()` removes significant whitespace in some cases. I noticed this in one of the generated classes, the code `<?php include_once \dirname(__DIR__).'/file.php';` got replaced with `<?php include_once\dirname(__DIR__).'/file.php';` which is a syntax error. Commits ------- 8d368e1 Fix bug with whitespace in Kernel::stripComments()
2 parents 0ded672 + 8d368e1 commit cdb1883
Copy full SHA for cdb1883

File tree

Expand file treeCollapse file tree

2 files changed

+40
-16
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+40
-16
lines changed

‎src/Symfony/Component/HttpKernel/Kernel.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Kernel.php
+5Lines 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
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/KernelTest.php
+35-16Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.