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

Skip to content

Commit 218a291

Browse files
committed
[FrameworkBundle] Add a script that checks for missing items in the unused tag whitelist
1 parent 1676e3a commit 218a291

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
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,11 @@ 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',
4351
'routing.expression_language_provider',
4452
'routing.loader',
4553
'security.expression_language_provider',
@@ -53,8 +61,10 @@ class UnusedTagsPass implements CompilerPassInterface
5361
'translation.loader',
5462
'twig.extension',
5563
'twig.loader',
64+
'twig.runtime',
5665
'validator.constraint_validator',
5766
'validator.initializer',
67+
'workflow.definition',
5868
];
5969

6070
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\Finder\Finder;
1718

1819
class UnusedTagsPassTest extends TestCase
1920
{
@@ -31,4 +32,23 @@ public function testProcess()
3132

3233
$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());
3334
}
35+
36+
public function testMissingWhitelistTags()
37+
{
38+
$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.');
39+
}
40+
41+
private function getWhitelistTags()
42+
{
43+
// get tags in UnusedTagsPass
44+
$target = dirname(__DIR__, 3).'/DependencyInjection/Compiler/UnusedTagsPass.php';
45+
$contents = file_get_contents($target);
46+
preg_match('{private \$whitelist = \[(.+?)\];}sm', $contents, $matches);
47+
$tags = array_values(array_filter(array_map(function ($str) {
48+
return trim(preg_replace('{^ +\'(.+)\',}', '$1', $str));
49+
}, explode("\n", $matches[1]))));
50+
sort($tags);
51+
52+
return $tags;
53+
}
3454
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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()
19+
{
20+
$tags = [];
21+
22+
// get all tags used in XML configs
23+
$files = Finder::create()->files()->name('*.xml')->path('Resources')->notPath('Tests')->in(dirname(__DIR__, 5));
24+
foreach ($files as $file) {
25+
$contents = file_get_contents($file);
26+
if (preg_match_all('{<tag name="([^"]+)"}', $contents, $matches)) {
27+
foreach ($matches[1] as $match) {
28+
$tags[$match] = true;
29+
}
30+
}
31+
if (preg_match_all('{<argument type="tagged_.+?" tag="([^"]+)"}', $contents, $matches)) {
32+
foreach ($matches[1] as $match) {
33+
$tags[$match] = true;
34+
}
35+
}
36+
}
37+
38+
// get all tags used in findTaggedServiceIds calls()
39+
$files = Finder::create()->files()->name('*.php')->path('DependencyInjection')->notPath('Tests')->in(dirname(__DIR__, 5));
40+
foreach ($files as $file) {
41+
$contents = file_get_contents($file);
42+
if (preg_match_all('{findTaggedServiceIds\(\'([^\']+)\'}', $contents, $matches)) {
43+
foreach ($matches[1] as $match) {
44+
if ('my.tag' === $match) {
45+
continue;
46+
}
47+
$tags[$match] = true;
48+
}
49+
}
50+
if (preg_match_all('{findTaggedServiceIds\(\$this->([^,\)]+)}', $contents, $matches)) {
51+
foreach ($matches[1] as $var) {
52+
if (preg_match_all('{\$'.$var.' = \'([^\']+)\'}', $contents, $matches)) {
53+
foreach ($matches[1] as $match) {
54+
$tags[$match] = true;
55+
}
56+
}
57+
}
58+
}
59+
}
60+
61+
$tags = array_keys($tags);
62+
sort($tags);
63+
64+
return $tags;
65+
}
66+
}

0 commit comments

Comments
 (0)
0