8000 [FrameworkBundle] Add missing items in the unused tag pass whitelist by fabpot · Pull Request #35833 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Add missing items in the unused tag pass whitelist #35833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ class UnusedTagsPass implements CompilerPassInterface
{
private $whitelist = [
'annotations.cached_reader',
'auto_alias',
'cache.pool',
'cache.pool.clearer',
'chatter.transport_factory',
'config_cache.resource_checker',
'console.command',
'container.env_var_loader',
'container.env_var_processor',
'container.hot_path',
'container.reversible',
'container.service_locator',
'container.service_locator_context',
'container.service_subscriber',
'controller.argument_value_resolver',
'controller.service_arguments' 8000 ;,
'config_cache.resource_checker',
'data_collector',
'form.type',
'form.type_extension',
Expand All @@ -43,11 +49,20 @@ class UnusedTagsPass implements CompilerPassInterface
'kernel.event_subscriber',
'kernel.fragment_renderer',
'kernel.locale_aware',
'kernel.reset',
'mailer.transport_factory',
'messenger.bus',
'messenger.receiver',
'messenger.message_handler',
'messenger.receiver',
'messenger.transport_factory',
'mime.mime_type_guesser',
'monolog.logger',
'notifier.channel',
'notifier.transport_factory',
'property_info.access_extractor',
'property_info.initializable_extractor',
'property_info.list_extractor',
'property_info.type_extractor',
'proxy',
'routing.expression_language_provider',
'routing.loader',
Expand All @@ -63,9 +78,10 @@ class UnusedTagsPass implements CompilerPassInterface
'translation.loader',
'twig.extension',
'twig.loader',
'twig.runtime',
'validator.auto_mapper',
'validator.constraint_validator',
'validator.initializer',
'validator.auto_mapper',
];

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(): array
{
$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