Skip to content

Commit 66440d8

Browse files
minor #35833 [FrameworkBundle] Add missing items in the unused tag pass whitelist (fabpot)
This PR was merged into the 5.1-dev branch. Discussion ---------- [FrameworkBundle] Add missing items in the unused tag pass whitelist | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | n/a <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | n/a We have some missing tags in the whitelist. I've added a script that adds the missing ones, and added a test to avoid forgetting about updating the whitelist. Commits ------- d1bcc0fc5e [FrameworkBundle] Add a script that checks for missing items in the unused tag whitelist
1 parent 40d1b97 commit 66440d8

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed

DependencyInjection/Compiler/UnusedTagsPass.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ class UnusedTagsPass implements CompilerPassInterface
2323
{
2424
private $whitelist = [
2525
'annotations.cached_reader',
26+
'auto_alias',
27+
'cache.pool',
2628
'cache.pool.clearer',
29+
'config_cache.resource_checker',
2730
'console.command',
31+
'container.env_var_processor',
2832
'container.hot_path',
2933
'container.service_locator',
3034
'container.service_subscriber',
35+
'controller.argument_value_resolver',
3136
'controller.service_arguments',
32-
'config_cache.resource_checker',
3337
'data_collector',
3438
'form.type',
3539
'form.type_extension',
@@ -39,7 +43,12 @@ class UnusedTagsPass implements CompilerPassInterface
3943
'kernel.event_listener',
4044
'kernel.event_subscriber',
4145
'kernel.fragment_renderer',
46+
'kernel.reset',
4247
'monolog.logger',
48+
'property_info.access_extractor',
49+
'property_info.list_extractor',
50+
'property_info.type_extractor',
51+
'proxy',
4352
'routing.expression_language_provider',
4453
'routing.loader',
4554
'security.expression_language_provider',
@@ -53,8 +62,10 @@ class UnusedTagsPass implements CompilerPassInterface
5362
'translation.loader',
5463
'twig.extension',
5564
'twig.loader',
65+
'twig.runtime',
5666
'validator.constraint_validator',
5767
'validator.initializer',
68+
'workflow.definition',
5869
];
5970

6071
public function process(ContainerBuilder $container)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
require dirname(__DIR__, 6).'/vendor/autoload.php';
13+
14+
use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\UnusedTagsPassUtils;
15+
16+
$target = dirname(__DIR__, 2).'/DependencyInjection/Compiler/UnusedTagsPass.php';
17+
$contents = file_get_contents($target);
18+
$contents = preg_replace('{private \$whitelist = \[(.+?)\];}sm', "private \$whitelist = [\n '".implode("',\n '", UnusedTagsPassUtils::getDefinedTags())."',\n ];", $contents);
19+
file_put_contents($target, $contents);

Tests/DependencyInjection/Compiler/UnusedTagsPassTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,27 @@ public function testProcess()
3131

3232
$this->assertSame([sprintf('%s: Tag "kenrel.event_subscriber" was defined on service(s) "foo", "bar", but was never used. Did you mean "kernel.event_subscriber"?', UnusedTagsPass::class)], $container->getCompiler()->getLog());
3333
}
34+
35+
public function testMissingWhitelistTags()
36+
{
37+
if (\dirname((new \ReflectionClass(ContainerBuilder::class))->getFileName(), 3) !== \dirname(__DIR__, 5)) {
38+
$this->markTestSkipped('Tests are not run from the root symfony/symfony metapackage.');
39+
}
40+
41+
$this->assertSame(UnusedTagsPassUtils::getDefinedTags(), $this->getWhitelistTags(), 'The src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php file must be updated; run src/Symfony/Bundle/FrameworkBundle/Resources/bin/check-unused-tags-whitelist.php.');
42+
}
43+
44+
private function getWhitelistTags()
45+
{
46+
// get tags in UnusedTagsPass
47+
$target = \dirname(__DIR__, 3).'/DependencyInjection/Compiler/UnusedTagsPass.php';
48+
$contents = file_get_contents($target);
49+
preg_match('{private \$whitelist = \[(.+?)\];}sm', $contents, $matches);
50+
$tags = array_values(array_filter(array_map(function ($str) {
51+
return trim(preg_replace('{^ +\'(.+)\',}', '$1', $str));
52+
}, explode("\n", $matches[1]))));
53+
sort($tags);
54+
55+
return $tags;
56+
}
3457
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\Finder\Finder;
15+
16+
class UnusedTagsPassUtils
17+
{
18+
public static function getDefinedTags()
19+
{
20+
$tags = [
21+
'proxy' => true,
22+
];
23+
24+
// get all tags used in XML configs
25+
$files = Finder::create()->files()->name('*.xml')->path('Resources')->notPath('Tests')->in(\dirname(__DIR__, 5));
26+
foreach ($files as $file) {
27+
$contents = file_get_contents($file);
28+
if (preg_match_all('{<tag name="([^"]+)"}', $contents, $matches)) {
29+
foreach ($matches[1] as $match) {
30+
$tags[$match] = true;
31+
}
32+
}
33+
if (preg_match_all('{<argument type="tagged_.+?" tag="([^"]+)"}', $contents, $matches)) {
34+
foreach ($matches[1] as $match) {
35+
$tags[$match] = true;
36+
}
37+
}
38+
}
39+
40+
// get all tags used in findTaggedServiceIds calls()
41+
$files = Finder::create()->files()->name('*.php')->path('DependencyInjection')->notPath('Tests')->in(\dirname(__DIR__, 5));
42+
foreach ($files as $file) {
43+
$contents = file_get_contents($file);
44+
if (preg_match_all('{findTaggedServiceIds\(\'([^\']+)\'}', $contents, $matches)) {
45+
foreach ($matches[1] as $match) {
46+
if ('my.tag' === $match) {
47+
continue;
48+
}
49+
$tags[$match] = true;
50+
}
51+
}
52+
if (preg_match_all('{findTaggedServiceIds\(\$this->([^,\)]+)}', $contents, $matches)) {
53+
foreach ($matches[1] as $var) {
54+
if (preg_match_all('{\$'.$var.' = \'([^\']+)\'}', $contents, $matches)) {
55+
foreach ($matches[1] as $match) {
56+
$tags[$match] = true;
57+
}
58+
}
59+
}
60+
}
61+
}
62+
63+
$tags = array_keys($tags);
64+
sort($tags);
65+
66+
return $tags;
67+
}
68+
}

0 commit comments

Comments
 (0)