8000 [FrameworkBundle], [DependencyInjection] added logging of unused tags by Marmelatze · Pull Request #11744 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle], [DependencyInjection] added logging of unused tags #11744

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

Closed
wants to merge 1 commit into from
Closed
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
[FrameworkBundle], [DependencyInjection] added logging of unused tags…
… during container compilation
  • Loading branch information
Florian Pfitzer committed Oct 29, 2014
commit 415f0687206f4afe69a9b57cc67618c9c1f28a14
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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\DependencyInjection\Compiler;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're missing the license file header, take a look at the other files


use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Find all service tags which are defined, but not used and yield a warning log message.
*
* @author Florian Pfitzer <pfitzer@wurzel3.de>
*/
class UnusedTagsPass implements CompilerPassInterface
{
/**
* whitelisted tags
*
* @var array
*/
protected $whitelist = array(
"console.command",
"data_collector",
"form.type",
"form.type_extension",
"form.type_guesser",
"kernel.cache_clearer",
"kernel.cache_warmer",
"kernel.event_listener",
"kernel.event_subscriber",
"kernel.fragment_renderer",
"monolog.logger",
"routing.loader",
"security.remember_me_aware",
"security.voter",
"serializer.encoder",
"templating.helper",
"translation.dumper",
"translation.extractor",
"translation.loader",
"twig.extension",
"twig.loader",
"validator.constraint_validator",
"validator.initializer",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to myself: #12006 is going to add 2 more tags. Should be taken into account based on which one is merged first.

);

public function process(ContainerBuilder $container)
{
$compiler = $container->getCompiler();
$formatter = $compiler->getLoggingFormatter();
$tags = $container->findTags();

$unusedTags = $container->findUnusedTags();
foreach ($unusedTags as $tag) {
// skip whitelisted tags
if (in_array($tag, $this->whitelist)) {
continue;
}
// check for typos
$candidates = array();
foreach ($tags as $definedTag) {
if ($definedTag === $tag) {
continue;
}
if (false !== strpos($definedTag, $tag) || levenshtein($tag, $definedTag) <= strlen($tag) / 3) {
$candidates[] = $definedTag;
}
}

$services = array_keys($container->findTaggedServiceIds($tag));
$message = sprintf('Tag "%s" was defined on the service(s) %s, but was never used.', $tag, implode(',', $services));
if (!empty($candidates)) {
$message .= sprintf(' Did you mean "%s"?', implode('", "', $candidates));
}
$compiler->addLogMessage($formatter->format($this, $message));
}
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationDumperPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FragmentRendererPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
Expand Down Expand Up @@ -90,6 +91,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new TranslationDumperPass());
$container->addCompilerPass(new FragmentRendererPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new SerializerPass());
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);

if ($container->getParameter('kernel.debug')) {
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
*/
private $expressionLanguageProviders = array();

/**
* @var array with tag names used by findTaggedServiceIds
*/
private $usedTags = array();

/**
* Sets the track resources flag.
*
Expand Down Expand Up @@ -1045,6 +1050,7 @@ public function resolveServices($value)
*/
public function findTaggedServiceIds($name)
{
$this->usedTags[] = $name;
$tags = array();
foreach ($this->getDefinitions() as $id => $definition) {
if ($definition->hasTag($name)) {
Expand All @@ -1070,6 +1076,19 @@ public function findTags()
return array_unique($tags);
}

/**
* Returns all tags not queried by findTaggedServiceIds
*
* @return array An array of tags
*/
public function findUnusedTags()
{
$tags = array_values(array_diff($this->findTags(), $this->usedTags));
$tags = array_unique($tags);

return $tags;
}

public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
{
$this->expressionLanguageProviders[] = $provider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,18 @@ public function testfindTaggedServiceIds()
$this->assertEquals(array(), $builder->findTaggedServiceIds('foobar'), '->findTaggedServiceIds() returns an empty array if there is annotated services');
}

public function testFindUnusedTags()
{
$builder = new ContainerBuilder();
$builder
->register('foo', 'Bar\FooClass')
->addTag('kernel.event_listener', array('foo' => 'foo'))
->addTag('kenrel.event_listener', array('bar' => 'bar'))
;
$builder->findTaggedServiceIds('kernel.event_listener');
$this->assertEquals(array('kenrel.event_listener'), $builder->findUnusedTags(), '->findUnusedTags() returns an array with unused tags');
}

/**
* @covers Symfony\Component\DependencyInjection\ContainerBuilder::findDefinition
*/
Expand Down
0