Skip to content

Commit

Permalink
minor #35833 [FrameworkBundle] Add missing items in the unused tag pa…
Browse files Browse the repository at this point in the history
…ss 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
-------

d1bcc0f [FrameworkBundle] Add a script that checks for missing items in the unused tag whitelist
  • Loading branch information
nicolas-grekas committed Feb 25, 2020
1 parent d7250ef commit b515bc9
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ class UnusedTagsPass implements CompilerPassInterface
{
private $whitelist = [
'annotations.cached_reader',
'auto_alias',
'cache.pool',
'cache.pool.clearer',
'config_cache.resource_checker',
'console.command',
'container.env_var_processor',
'container.hot_path',
'container.service_locator',
'container.service_subscriber',
'controller.argument_value_resolver',
'controller.service_arguments',
'config_cache.resource_checker',
'data_collector',
'form.type',
'form.type_extension',
Expand All @@ -39,7 +43,12 @@ class UnusedTagsPass implements CompilerPassInterface
'kernel.event_listener',
'kernel.event_subscriber',
'kernel.fragment_renderer',
'kernel.reset',
'monolog.logger',
'property_info.access_extractor',
'property_info.list_extractor',
'property_info.type_extractor',
'proxy',
'routing.expression_language_provider',
'routing.loader',
'security.expression_language_provider',
Expand All @@ -53,8 +62,10 @@ class UnusedTagsPass implements CompilerPassInterface
'translation.loader',
'twig.extension',
'twig.loader',
'twig.runtime',
'validator.constraint_validator',
'validator.initializer',
'workflow.definition',
];

public function process(ContainerBuilder $container)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

require dirname(__DIR__, 6).'/vendor/autoload.php';

use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\UnusedTagsPassUtils;

$target = dirname(__DIR__, 2).'/DependencyInjection/Compiler/UnusedTagsPass.php';
$contents = file_get_contents($target);
$contents = preg_replace('{private \$whitelist = \[(.+?)\];}sm', "private \$whitelist = [\n '".implode("',\n '", UnusedTagsPassUtils::getDefinedTags())."',\n ];", $contents);
file_put_contents($target, $contents);
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,27 @@ public function testProcess()

$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());
}

public function testMissingWhitelistTags()
{
if (\dirname((new \ReflectionClass(ContainerBuilder::class))->getFileName(), 3) !== \dirname(__DIR__, 5)) {
$this->markTestSkipped('Tests are not run from the root symfony/symfony metapackage.');
}

$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.');
}

private function getWhitelistTags()
{
// get tags in UnusedTagsPass
$target = \dirname(__DIR__, 3).'/DependencyInjection/Compiler/UnusedTagsPass.php';
$contents = file_get_contents($target);
preg_match('{private \$whitelist = \[(.+?)\];}sm', $contents, $matches);
$tags = array_values(array_filter(array_map(function ($str) {
return trim(preg_replace('{^ +\'(.+)\',}', '$1', $str));
}, explode("\n", $matches[1]))));
sort($tags);

return $tags;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;

use Symfony\Component\Finder\Finder;

class UnusedTagsPassUtils
{
public static function getDefinedTags()
{
$tags = [
'proxy' => true,
];

// get all tags used in XML configs
$files = Finder::create()->files()->name('*.xml')->path('Resources')->notPath('Tests')->in(\dirname(__DIR__, 5));
foreach ($files as $file) {
$contents = file_get_contents($file);
if (preg_match_all('{<tag name="([^"]+)"}', $contents, $matches)) {
foreach ($matches[1] as $match) {
$tags[$match] = true;
}
}
if (preg_match_all('{<argument type="tagged_.+?" tag="([^"]+)"}', $contents, $matches)) {
foreach ($matches[1] as $match) {
$tags[$match] = true;
}
}
}

// get all tags used in findTaggedServiceIds calls()
$files = Finder::create()->files()->name('*.php')->path('DependencyInjection')->notPath('Tests')->in(\dirname(__DIR__, 5));
foreach ($files as $file) {
$contents = file_get_contents($file);
if (preg_match_all('{findTaggedServiceIds\(\'([^\']+)\'}', $contents, $matches)) {
foreach ($matches[1] as $match) {
if ('my.tag' === $match) {
continue;
}
$tags[$match] = true;
}
}
if (preg_match_all('{findTaggedServiceIds\(\$this->([^,\)]+)}', $contents, $matches)) {
foreach ($matches[1] as $var) {
if (preg_match_all('{\$'.$var.' = \'([^\']+)\'}', $contents, $matches)) {
foreach ($matches[1] as $match) {
$tags[$match] = true;
}
}
}
}
}

$tags = array_keys($tags);
sort($tags);

return $tags;
}
}

0 comments on commit b515bc9

Please sign in to comment.