Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Deprecate Kernel::stripComments() #51712

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
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
1 change: 1 addition & 0 deletions UPGRADE-6.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ HttpKernel

* [BC break] `BundleInterface` no longer extends `ContainerAwareInterface`
* [BC break] Add native return types to `TraceableEventDispatcher` and to `MergeExtensionConfigurationPass`
* Deprecate `Kernel::stripComments()`

Messenger
---------
Expand Down
66 changes: 63 additions & 3 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
use Symfony\Component\DependencyInjection\Variable;
use Symfony\Component\ErrorHandler\DebugClassLoader;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpKernel\Kernel;

/**
* PhpDumper dumps a service container as a PHP class.
Expand Down Expand Up @@ -571,7 +570,7 @@ private function generateProxyClasses(): array
$proxyClasses = [];
$alreadyGenerated = [];
$definitions = $this->container->getDefinitions();
$strip = '' === $this->docStar && method_exists(Kernel::class, 'stripComments');
$strip = '' === $this->docStar;
$proxyDumper = $this->getProxyDumper();
ksort($definitions);
foreach ($definitions as $id => $definition) {
Expand Down Expand Up @@ -620,7 +619,7 @@ private function generateProxyClasses(): array

if ($strip) {
$proxyCode = "<?php\n".$proxyCode;
$proxyCode = substr(Kernel::stripComments($proxyCode), 5);
$proxyCode = substr(self::stripComments($proxyCode), 5);
}

$proxyClass = explode(' ', $this->inlineRequires ? substr($proxyCode, \strlen($code)) : $proxyCode, 3)[1];
Expand Down Expand Up @@ -2339,4 +2338,65 @@ private function isProxyCandidate(Definition $definition, ?bool &$asGhostObject,

return $this->getProxyDumper()->isProxyCandidate($definition, $asGhostObject, $id) ? $definition : null;
}

/**
* 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.
*/
private static function stripComments(string $source): string
{
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,110 @@ public function testCallableAdapterConsumer()
$this->assertInstanceOf(SingleMethodInterface::class, $container->get('bar')->foo);
$this->assertInstanceOf(Foo::class, $container->get('bar')->foo->theMethod());
}

/**
* @dataProvider getStripCommentsCodes
*/
public function testStripComments(string $source, string $expected)
{
$reflection = new \ReflectionClass(PhpDumper::class);
$method = $reflection->getMethod('stripComments');

$output = $method->invoke(null, $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
],
];
}
}

class Rot13EnvVarProcessor implements EnvVarProcessorInterface
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
* Add argument `$validationFailedStatusCode` to `#[MapQueryString]` and `#[MapRequestPayload]`
* Add argument `$debug` to `Logger`
* Add class `DebugLoggerConfigurator`
* Deprecate `Kernel::stripComments()`

6.3
---
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,9 +774,13 @@ private function preBoot(): ContainerInterface
*
* 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;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,13 @@ public function testHandleBootsTheKernel()

/**
* @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
Expand Down