Skip to content

Commit

Permalink
minor #51774 [HttpKernel] Remove deprecated Kernel::stripComment() me…
Browse files Browse the repository at this point in the history
…thod (nicolas-grekas)

This PR was merged into the 7.0 branch.

Discussion
----------

[HttpKernel] Remove deprecated Kernel::stripComment() method

| Q             | A
| ------------- | ---
| Branch?       | 7.0
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT

Commits
-------

2391a33 [HttpKernel] Remove deprecated Kernel::stripComment() method
  • Loading branch information
nicolas-grekas committed Sep 28, 2023
2 parents 9d6eb8c + 2391a33 commit a231130
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 170 deletions.
1 change: 1 addition & 0 deletions UPGRADE-7.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ HttpKernel
* Remove `HttpKernelInterface::MASTER_REQUEST`
* Remove `terminate_on_cache_hit` option from `HttpCache`
* Require explicit argument when calling `ConfigDataCollector::setKernel()`, `RouterListener::setCurrentRequest()`
* Remove `Kernel::stripComments()`

Lock
----
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
* Remove `HttpKernelInterface::MASTER_REQUEST`
* Remove `terminate_on_cache_hit` option from `HttpCache`
* Require explicit argument when calling `ConfigDataCollector::setKernel()`, `RouterListener::setCurrentRequest()`
* Remove `Kernel::stripComments()`

6.4
---
Expand Down
65 changes: 0 additions & 65 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -731,71 +731,6 @@ private function preBoot(): ContainerInterface
return $container;
}

/**
* Removes comments from a PHP source string.
*
* We don't use the PHP php_strip_whitespace() function
* as we want the content to be readable and well-formatted.
*
* @deprecated since Symfony 6.4 without replacement
*/
public static function stripComments(string $source): string
{
trigger_deprecation('symfony/http-kernel', '6.4', 'Method "%s()" is deprecated without replacement.', __METHOD__);

if (!\function_exists('token_get_all')) {
return $source;
}

$rawChunk = '';
$output = '';
$tokens = token_get_all($source);
$ignoreSpace = false;
for ($i = 0; isset($tokens[$i]); ++$i) {
$token = $tokens[$i];
if (!isset($token[1]) || 'b"' === $token) {
$rawChunk .= $token;
} elseif (\T_START_HEREDOC === $token[0]) {
$output .= $rawChunk.$token[1];
do {
$token = $tokens[++$i];
$output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token;
} while (\T_END_HEREDOC !== $token[0]);
$rawChunk = '';
} elseif (\T_WHITESPACE === $token[0]) {
if ($ignoreSpace) {
$ignoreSpace = false;

continue;
}

// 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;
}
}
}

$output .= $rawChunk;

unset($tokens, $rawChunk);
gc_mem_caches();

return $output;
}

public function __sleep(): array
{
return ['environment', 'debug'];
Expand Down
105 changes: 0 additions & 105 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,111 +241,6 @@ public function testHandleBootsTheKernel()
$kernel->handle($request, $type, $catch);
}

/**
* @dataProvider getStripCommentsCodes
*
* @group legacy
*/
public function testStripComments(string $source, string $expected)
{
$this->expectDeprecation('Since symfony/http-kernel 6.4: Method "Symfony\Component\HttpKernel\Kernel::stripComments()" is deprecated without replacement.');

$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 static function getStripCommentsCodes(): array
{
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';
$string = 'string should not be
modified';
$heredoc = <<<HD
Heredoc should not be modified {$a[1+$b]}
HD;
$nowdoc = <<<'ND'
Nowdoc should not be modified
ND;
/**
* some class comments to strip
*/
class TestClass
{
/**
* some method comments to strip
*/
public function doStuff()
{
// inline comment
}
}
EOF
, <<<'EOF'
<?php
include_once \dirname(__DIR__).'/foo.php';
$string = 'string should not be modified';
$string = 'string should not be
modified';
$heredoc = <<<HD
Heredoc should not be modified {$a[1+$b]}
HD;
$nowdoc = <<<'ND'
Nowdoc should not be modified
ND;
class TestClass
{
public function doStuff()
{
}
}
EOF
],
];
}

public function testSerialize()
{
$env = 'test_env';
Expand Down

0 comments on commit a231130

Please sign in to comment.