8000 [FrameworkBundle] Add a script that checks for missing items in the u… · symfony/symfony@007d105 · GitHub
[go: up one dir, main page]

Skip to content

Commit 007d105

Browse files
committed
[FrameworkBundle] Add a script that checks for missing items in the unused tag whitelist
1 parent b3b368b commit 007d105

File tree

4 files changed

+127
-4
lines changed

4 files changed

+127
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,21 @@ class UnusedTagsPass implements CompilerPassInterface
2323
{
2424
private $whitelist = [
2525
'annotations.cached_reader',
26+
'auto_alias',
27+
'cache.pool',
2628
'cache.pool.clearer',
29+
'chatter.transport_factory',
30+
'config_cache.resource_checker',
2731
'console.command',
32+
'container.env_var_loader',
33+
'container.env_var_processor',
2834
'container.hot_path',
2935
'container.reversible',
3036
'container.service_locator',
37+
'container.service_locator_context',
3138
'container.service_subscriber',
39+
'controller.argument_value_resolver',
3240
'controller.service_arguments',
33-
'config_cache.resource_checker',
3441
'data_collector',
3542
'form.type',
3643
'form.type_extension',
@@ -42,12 +49,20 @@ class UnusedTagsPass implements CompilerPassInterface
4249
'kernel.event_subscriber',
4350
'kernel.fragment_renderer',
4451
'kernel.locale_aware',
52+
'kernel.reset',
53+
'mailer.transport_factory',
4554
'messenger.bus',
46-
'messenger.receiver',
4755
'messenger.message_handler',
56+
'messenger.receiver',
57+
'messenger.transport_factory',
4858
'mime.mime_type_guesser',
4959
'monolog.logger',
50-
'proxy',
60+
'notifier.channel',
61+
'notifier.transport_factory',
62+
'property_info.access_extractor',
63+
'property_info.initializable_extractor',
64+
'property_info.list_extractor',
65+
'property_info.type_extractor',
5166
'routing.expression_language_provider',
5267
'routing.loader',
5368
'routing.route_loader',
@@ -56,14 +71,16 @@ class UnusedTagsPass implements CompilerPassInterface
5671
'security.voter',
5772
'serializer.encoder',
5873
'serializer.normalizer',
74+
'texter.transport_factory',
5975
'translation.dumper',
6076
'translation.extractor',
6177
'translation.loader',
6278
'twig.extension',
6379
'twig.loader',
80+
'twig.runtime',
81+
'validator.auto_mapper',
6482
'validator.constraint_validator',
6583
'validator.initializer',
66-
'validator.auto_mapper',
6784
];
6885

6986
public function process(ContainerBuilder $container)
Lines changed: 19 additions & 0 deletions
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 <fabien@symfony.com>
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);

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/UnusedTagsPassTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,23 @@ 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+
$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.');
38+
}
39+
40+
private function getWhitelistTags()
41+
{
42+
// get tags in UnusedTagsPass
43+
$target = \dirname(__DIR__, 3).'/DependencyInjection/Compiler/UnusedTagsPass.php';
44+
$contents = file_get_contents($target);
45+
preg_match('{private \$whitelist = \[(.+?)\];}sm', $contents, $matches);
46+
$tags = array_values(array_filter(array_map(function ($str) {
47+
return trim(preg_replace('{^ +\'(.+)\',}', '$1', $str));
48+
}, explode("\n", $matches[1]))));
49+
sort($tags);
50+
51+
return $tags;
52+
}
3453
}
Lines changed: 68 additions & 0 deletions
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 <fabien@symfony.com>
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(): array
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)
0