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

Skip to content

Commit d1bcc0f

Browse files
fabpotnicolas-grekas
authored andcommitted
[FrameworkBundle] Add a script that checks for missing items in the unused tag whitelist
1 parent f4332cb commit d1bcc0f

File tree

4 files changed

+129
-3
lines changed

4 files changed

+129
-3
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,21 @@ class UnusedTagsPass implements CompilerPassInterface
2323
{
2424
private $whitelist = [
2525
'annotations.cached_reader',
26+
'auto_alias',
27+
'cache.pool',
2628
'cache.pool.clearer',
2729
'chatter.transport_factory',
30+
'config_cache.resource_checker',
2831
'console.command',
32+
'container.env_var_loader',
33+
'container.env_var_processor',
2934
'container.hot_path',
3035
'container.reversible',
3136
'container.service_locator',
37+
'container.service_locator_context',
3238
'container.service_subscriber',
39+
'controller.argument_value_resolver',
3340
'controller.service_arguments',
34-
'config_cache.resource_checker',
3541
'data_collector',
3642
'form.type',
3743
'form.type_extension',
@@ -43,11 +49,20 @@ class UnusedTagsPass implements CompilerPassInterface
4349
'kernel.event_subscriber',
4450
'kernel.fragment_renderer',
4551
'kernel.locale_aware',
52+
'kernel.reset',
53+
'mailer.transport_factory',
4654
'messenger.bus',
47-
'messenger.receiver',
4855
'messenger.message_handler',
56+
'messenger.receiver',
57+
'messenger.transport_factory',
4958
'mime.mime_type_guesser',
5059
'monolog.logger',
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
'proxy',
5267
'routing.expression_language_provider',
5368
'routing.loader',
@@ -63,9 +78,10 @@ class UnusedTagsPass implements CompilerPassInterface
6378
'translation.loader',
6479
'twig.extension',
6580
'twig.loader',
81+
'twig.runtime',
82+
'validator.auto_mapper',
6683
'validator.constraint_validator',
6784
'validator.initializer',
68-
'validator.auto_mapper',
6985
];
7086

7187
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: 23 additions & 0 deletions
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< F438 /code>+
}
3457
}
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;< 4161 /div>
67+
}
68+
}

0 commit comments

Comments
 (0)
0