Skip to content

Commit 2c7bf8c

Browse files
GuilhemNnicolas-grekas
authored andcommitted
Fix comments, improve the feature
1 parent f2d2532 commit 2c7bf8c

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.4.0
55
-----
66

7+
* Added `lint:container` to check that services wiring matches type declarations
78
* Added `MailerAssertionsTrait`
89
* Deprecated support for `templating` engine in `TemplateController`, use Twig instead
910
* Deprecated the `$parser` argument of `ControllerResolver::__construct()` and `DelegatingLoader::__construct()`

Command/ContainerLintCommand.php

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

14+
use Symfony\Component\Config\ConfigCache;
15+
use Symfony\Component\Config\FileLocator;
1416
use Symfony\Component\Console\Command\Command;
1517
use Symfony\Component\Console\Input\InputInterface;
1618
use Symfony\Component\Console\Input\InputOption;
1719
use Symfony\Component\Console\Output\OutputInterface;
18-
use Symfony\Component\Config\ConfigCache;
19-
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
20-
use Symfony\Component\DependencyInjection\ContainerBuilder;
21-
use Symfony\Component\DependencyInjection\Compiler\CheckTypeHintsPass;
20+
use Symfony\Component\DependencyInjection\Compiler\CheckTypeDeclarationsPass;
2221
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
23-
use Symfony\Component\Config\FileLocator;
22+
use Symfony\Component\DependencyInjection\ContainerBuilder;
23+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2424

25-
class ContainerLintCommand extends Command
25+
final class ContainerLintCommand extends Command
2626
{
27+
protected static $defaultName = 'lint:container';
28+
2729
/**
2830
* @var ContainerBuilder
2931
*/
@@ -35,37 +37,33 @@ class ContainerLintCommand extends Command
3537
protected function configure()
3638
{
3739
$this
38-
->setDescription('Lints container for services arguments type hints')
39-
->setHelp('This command will parse all your defined services and check that you are injecting service without type error based on type hints.')
40-
->addOption('only-used-services', 'o', InputOption::VALUE_NONE, 'Check only services that are used in your application')
40+
->setDescription('Ensures that arguments injected into services match type declarations')
41+
->setHelp('This command parses service definitions and ensures that injected values match the type declarations of each services\' class.')
42+
->addOption('ignore-unused-services', 'o', InputOption::VALUE_NONE, 'Ignore unused services')
4143
;
4244
}
4345

4446
/**
4547
* {@inheritdoc}
4648
*/
47-
protected function execute(InputInterface $input, OutputInterface $output)
49+
protected function execute(InputInterface $input, OutputInterface $output): int
4850
{
4951
$container = $this->getContainerBuilder();
5052

5153
$container->setParameter('container.build_id', 'lint_container');
5254

5355
$container->addCompilerPass(
54-
new CheckTypeHintsPass(),
55-
$input->getOption('only-used-services') ? PassConfig::TYPE_AFTER_REMOVING : PassConfig::TYPE_BEFORE_OPTIMIZATION
56+
new CheckTypeDeclarationsPass(true),
57+
$input->getOption('ignore-unused-services') ? PassConfig::TYPE_AFTER_REMOVING : PassConfig::TYPE_OPTIMIZE,
58+
-5
5659
);
5760

5861
$container->compile();
62+
63+
return 0;
5964
}
6065

61-
/**
62-
* Loads the ContainerBuilder from the cache.
63-
*
64-
* @return ContainerBuilder
65-
*
66-
* @throws \LogicException
67-
*/
68-
protected function getContainerBuilder()
66+
private function getContainerBuilder(): ContainerBuilder
6967
{
7068
if ($this->containerBuilder) {
7169
return $this->containerBuilder;
@@ -74,10 +72,9 @@ protected function getContainerBuilder()
7472
$kernel = $this->getApplication()->getKernel();
7573

7674
if (!$kernel->isDebug() || !(new ConfigCache($kernel->getContainer()->getParameter('debug.container.dump'), true))->isFresh()) {
77-
$buildContainer = \Closure::bind(function () { return $this->buildContainer(); }, $kernel, get_class($kernel));
75+
$buildContainer = \Closure::bind(function () { return $this->buildContainer(); }, $kernel, \get_class($kernel));
7876
$container = $buildContainer();
79-
$container->getCompilerPassConfig()->setRemovingPasses(array());
80-
$container->compile();
77+
$container->getCompilerPassConfig()->setRemovingPasses([]);
8178
} else {
8279
(new XmlFileLoader($container = new ContainerBuilder(), new FileLocator()))->load($kernel->getContainer()->getParameter('debug.container.dump'));
8380
}

0 commit comments

Comments
 (0)