Skip to content

[HttpKernel] Fix bug with whitespace in Kernel::stripComments() #39220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -858,13 +858,18 @@ public static function stripComments($source)
// replace multiple new lines with a single newline
$rawChunk .= preg_replace(['/\n{2,}/S'], "\n", $token[1]);
} elseif (\in_array($token[0], [\T_COMMENT, \T_DOC_COMMENT])) {
if (!\in_array($rawChunk[\strlen($rawChunk) - 1], [' ', "\n", "\r", "\t"], true)) {
$rawChunk .= ' ';
}
$ignoreSpace = true;
} else {
$rawChunk .= $token[1];

// The PHP-open tag already has a new-line
if (\T_OPEN_TAG === $token[0]) {
$ignoreSpace = true;
} else {
$ignoreSpace = false;
}
}
}
Expand Down
51 changes: 35 additions & 16 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,37 @@ public function testHandleBootsTheKernel()
$kernel->handle($request, $type, $catch);
}

public function testStripComments()
/**
* @dataProvider getStripCommentsCodes
*/
public function testStripComments(string $source, string $expected)
{
$output = Kernel::stripComments($source);

// Heredocs are preserved, making the output mixing Unix and Windows line
// endings, switching to "\n" everywhere on Windows to avoid failure.
if ('\\' === \DIRECTORY_SEPARATOR) {
$expected = str_replace("\r\n", "\n", $expected);
$output = str_replace("\r\n", "\n", $output);
}

$this->assertEquals($expected, $output);
}

public function getStripCommentsCodes(): array
{
$source = <<<'EOF'
return [
['<?php echo foo();', '<?php echo foo();'],
['<?php echo/**/foo();', '<?php echo foo();'],
['<?php echo/** bar */foo();', '<?php echo foo();'],
['<?php /**/echo foo();', '<?php echo foo();'],
['<?php echo \foo();', '<?php echo \foo();'],
['<?php echo/**/\foo();', '<?php echo \foo();'],
['<?php echo/** bar */\foo();', '<?php echo \foo();'],
['<?php /**/echo \foo();', '<?php echo \foo();'],
[<<<'EOF'
<?php
include_once \dirname(__DIR__).'/foo.php';

$string = 'string should not be modified';

Expand Down Expand Up @@ -280,9 +307,10 @@ public function doStuff()
// inline comment
}
}
EOF;
$expected = <<<'EOF'
EOF
, <<<'EOF'
<?php
include_once \dirname(__DIR__).'/foo.php';
$string = 'string should not be modified';
$string = 'string should not be

Expand All @@ -307,18 +335,9 @@ public function doStuff()
{
}
}
EOF;

$output = Kernel::stripComments($source);

// Heredocs are preserved, making the output mixing Unix and Windows line
// endings, switching to "\n" everywhere on Windows to avoid failure.
if ('\\' === \DIRECTORY_SEPARATOR) {
$expected = str_replace("\r\n", "\n", $expected);
$output = str_replace("\r\n", "\n", $output);
}

$this->assertEquals($expected, $output);
EOF
],
];
}

/**
Expand Down