8000 Deprecate `Kernel::stripComments()` · symfony/symfony@43c5038 · GitHub
[go: up one dir, main page]

Skip to content

Commit 43c5038

Browse files
alamiraultfabpot
authored andcommitted
Deprecate Kernel::stripComments()
1 parent 7d310a3 commit 43c5038

File tree

6 files changed

+177
-3
lines changed

6 files changed

+177
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ HttpKernel
107107

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

111112
Messenger
112113
---------

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
use Symfony\Component\DependencyInjection\Variable;
4545
use Symfony\Component\ErrorHandler\DebugClassLoader;
4646
use Symfony\Component\ExpressionLanguage\Expression;
47-
use Symfony\Component\HttpKernel\Kernel;
4847

4948
/**
5049
* PhpDumper dumps a service container as a PHP class.
@@ -571,7 +570,7 @@ private function generateProxyClasses(): array
571570
$proxyClasses = [];
572571
$alreadyGenerated = [];
573572
$definitions = $this->container->getDefinitions();
574-
$strip = '' === $this->docStar && method_exists(Kernel::class, 'stripComments');
573+
$strip = '' === $this->docStar;
575574
$proxyDumper = $this->getProxyDumper();
576575
ksort($definitions);
577576
foreach ($definitions as $id => $definition) {
@@ -620,7 +619,7 @@ private function generateProxyClasses(): array
620619

621620
if ($strip) {
622621
$proxyCode = "<?php\n".$proxyCode;
623-
$proxyCode = substr(Kernel::stripComments($proxyCode), 5);
622+
$proxyCode = substr(self::stripComments($proxyCode), 5);
624623
}
625624

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

23402339
return $this->getProxyDumper()->isProxyCandidate($definition, $asGhostObject, $id) ? $definition : null;
23412340
}
2341+
2342+
/**
2343+
* Removes comments from a PHP source string.
2344+
*
2345+
* We don't use the PHP php_strip_whitespace() function
2346+
* as we want the content to be readable and well-formatted.
2347+
*/
2348+
private static function stripComments(string $source): string
2349+
{
2350+
if (!\function_exists('token_get_all')) {
2351+
return $source;
2352+
}
2353+
2354+
$rawChunk = '';
2355+
$output = '';
2356+
$tokens = token_get_all($source);
2357+
$ignoreSpace = false;
2358+
for ($i = 0; isset($tokens[$i]); ++$i) {
2359+
$token = $tokens[$i];
2360+
if (!isset($token[1]) || 'b"' === $token) {
2361+
$rawChunk .= $token;
2362+
} elseif (\T_START_HEREDOC === $token[0]) {
2363+
$output .= $rawChunk.$token[1];
2364+
do {
2365+
$token = $tokens[++$i];
2366+
$output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token;
2367+
} while (\T_END_HEREDOC !== $token[0]);
2368+
$rawChunk = '';
2369+
} elseif (\T_WHITESPACE === $token[0]) {
2370+
if ($ignoreSpace) {
2371+
$ignoreSpace = false;
2372+
2373+
continue;
2374+
}
2375+
2376+
// replace multiple new lines with a single newline
2377+
$rawChunk .= preg_replace(['/\n{2,}/S'], "\n", $token[1]);
2378+
} elseif (\in_array($token[0], [\T_COMMENT, \T_DOC_COMMENT])) {
2379+
if (!\in_array($rawChunk[\strlen($rawChunk) - 1], [' ', "\n", "\r", "\t"], true)) {
2380+
$rawChunk .= F438 ' ';
2381+
}
2382+
$ignoreSpace = true;
2383+
} else {
2384+
$rawChunk .= $token[1];
2385+
2386+
// The PHP-open tag already has a new-line
2387+
if (\T_OPEN_TAG === $token[0]) {
2388+
$ignoreSpace = true;
2389+
} else {
2390+
$ignoreSpace = false;
2391+
}
2392+
}
2393+
}
2394+
2395+
$output .= $rawChunk;
2396+
2397+
unset($tokens, $rawChunk);
2398+
gc_mem_caches();
2399+
2400+
return $output;
2401+
}
23422402
}

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,110 @@ public function testCallableAdapterConsumer()
19761976
$this->assertInstanceOf(SingleMethodInterface::class, $container->get('bar')->foo);
19771977
$this->assertInstanceOf(Foo::class, $container->get('bar')->foo->theMethod());
19781978
}
1979+
1980+
/**
1981+
* @dataProvider getStripCommentsCodes
1982+
*/
1983+
public function testStripComments(string $source, string $expected)
1984+
{
1985+
$reflection = new \ReflectionClass(PhpDumper::class);
1986+
$method = $reflection->getMethod('stripComments');
1987+
1988+
$output = $method->invoke(null, $source);
1989+
1990+
// Heredocs are preserved, making the output mixing Unix and Windows line
1991+
// endings, switching to "\n" everywhere on Windows to avoid failure.
1992+
if ('\\' === \DIRECTORY_SEPARATOR) {
1993+
$expected = str_replace("\r\n", "\n", $expected);
1994+
$output = str_replace("\r\n", "\n", $output);
1995+
}
1996+
1997+
$this->assertEquals($expected, $output);
1998+
}
1999+
2000+
public static function getStripCommentsCodes(): array
2001+
{
2002+
return [
2003+
['<?php echo foo();', '<?php echo foo();'],
2004+
['<?php echo/**/foo();', '<?php echo foo();'],
2005+
['<?php echo/** bar */foo();', '<?php echo foo();'],
2006+
['<?php /**/echo foo();', '<?php echo foo();'],
2007+
['<?php echo \foo();', '<?php echo \foo();'],
2008+
['<?php echo/**/\foo();', '<?php echo \foo();'],
2009+
['<?php echo/** bar */\foo();', '<?php echo \foo();'],
2010+
['<?php /**/echo \foo();', '<?php echo \foo();'],
2011+
[<<<'EOF'
2012+
<?php
2013+
include_once \dirname(__DIR__).'/foo.php';
2014+
2015+
$string = 'string should not be modified';
2016+
2017+
$string = 'string should not be
2018+
2019+
modified';
2020+
2021+
2022+
$heredoc = <<<HD
2023+
2024+
2025+
Heredoc should not be modified {$a[1+$b]}
2026+
2027+
2028+
HD;
2029+
2030+
$nowdoc = <<<'ND'
2031+
2032+
2033+
Nowdoc should not be modified
2034+
2035+
2036+
ND;
2037+
2038+
/**
2039+
* some class comments to strip
2040+
*/
2041+
class TestClass
2042+
{
2043+
/**
2044+
* some method comments to strip
2045+
*/
2046+
public function doStuff()
2047+
{
2048+
// inline comment
2049+
}
2050+
}
2051+
EOF
2052+
, <<<'EOF'
2053+
<?php
2054+
include_once \dirname(__DIR__).'/foo.php';
2055+
$string = 'string should not be modified';
2056+
$string = 'string should not be
2057+
2058+
modified';
2059+
$heredoc = <<<HD
2060+
2061+
2062+
Heredoc should not be modified {$a[1+$b]}
2063+
2064+
2065+
HD;
2066+
$nowdoc = <<<'ND'
2067+
2068+
2069+
Nowdoc should not be modified
2070+
2071+
2072+
ND;
2073+
class TestClass
2074+
{
2075+
public function doStuff()
2076+
{
2077+
}
2078+
}
2079+
EOF
2080+
],
2081+
];
2082+
}
19792083
}
19802084

19812085
class Rot13EnvVarProcessor implements EnvVarProcessorInterface

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* Add argument `$validationFailedStatusCode` to `#[MapQueryString]` and `#[MapRequestPayload]`
1212
* Add argument `$debug` to `Logger`
1313
* Add class `DebugLoggerConfigurator`
14+
* Deprecate `Kernel::stripComments()`
1415

1516
6.3
1617
---

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,9 +774,13 @@ private function preBoot(): ContainerInterface
774774
*
775775
* We don't use the PHP php_strip_whitespace() function
776776
* as we want the content to be readable and well-formatted.
777+
*
778+
* @deprecated since Symfony 6.4 without replacement
777779
*/
778780
public static function stripComments(string $source): string
779781
{
782+
trigger_deprecation('symfony/http-kernel', '6.4', 'Method "%s()" is deprecated without replacement.', __METHOD__);
783+
780784
if (!\function_exists('token_get_all')) {
781785
return $source;
782786
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,13 @@ public function testHandleBootsTheKernel()
249249

250250
/**
251251
* @dataProvider getStripCommentsCodes
252+
*
253+
* @group legacy
252254
*/
253255
public function testStripComments(string $source, string $expected)
254256
{
257+
$this->expectDeprecation('Since symfony/http-kernel 6.4: Method "Symfony\Component\HttpKernel\Kernel::stripComments()" is deprecated without replacement.');
258+
255259
$output = Kernel::stripComments($source);
256260

257261
// Heredocs are preserved, making the output mixing Unix and Windows line

0 commit comments

Comments
 (0)
0