Skip to content

Commit fd7d88d

Browse files
committed
feature #50745 [DependencyInjection] Add CheckAliasValidityPass to check interface compatibility (n-valverde)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [DependencyInjection] Add `CheckAliasValidityPass` to check interface compatibility | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes<!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no | Tickets | / | License | MIT | Doc PR | TODO Hello, I would like to propose adding a pass to the compiler, to make sure that when defining an alias which happens to be an interface, the resolved service actually implements that interface. See example below. ```xml <services> <service id="a_vendor.service" class="Foo\Bar" /> <service id="Foo\BarInterface" alias="a_vendor.service" /> </services> ``` ```php namespace Foo; interface BarInterface {} interface BazInterface {} class Bar implements BazInterface { } ``` ```php public function __construct(private BarInterface $bar) // Type error, $bar must be of type BarInterface ``` Currently the above situation is allowed, and will ultimately lead to a type error at runtime when trying to inject `BarInterface`, because `Bar` is not compatible. I think this situation should be raised as a configuration error, as I can't think of any valid use case for this. I might be missing some use cases, and this might have been already discussed in some way, so any input appreciated, even if you think this is a bad idea for some reason 🙃 Thanks! Commits ------- c5dff263ed [DependencyInjection] Add `CheckAliasValidityPass` to check interface compatibility
2 parents d9befd5 + e166138 commit fd7d88d

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

CHANGELOG.md

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

7+
* Add `CheckAliasValidityPass` to `lint:container` command
78
* Add `private_ranges` as a shortcut for private IP address ranges to the `trusted_proxies` option
89
* Mark classes `ConfigBuilderCacheWarmer`, `Router`, `SerializerCacheWarmer`, `TranslationsCacheWarmer`, `Translator` and `ValidatorCacheWarmer` as `final`
910
* Move the Router `cache_dir` to `kernel.build_dir`

Command/ContainerLintCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Console\Input\InputInterface;
2020
use Symfony\Component\Console\Output\OutputInterface;
2121
use Symfony\Component\Console\Style\SymfonyStyle;
22+
use Symfony\Component\DependencyInjection\Compiler\CheckAliasValidityPass;
2223
use Symfony\Component\DependencyInjection\Compiler\CheckTypeDeclarationsPass;
2324
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
2425
use Symfony\Component\DependencyInjection\Compiler\ResolveFactoryClassPass;
@@ -107,6 +108,7 @@ private function getContainerBuilder(): ContainerBuilder
107108
$container->setParameter('container.build_hash', 'lint_container');
108109
$container->setParameter('container.build_id', 'lint_container');
109110

111+
$container->addCompilerPass(new CheckAliasValidityPass(), PassConfig::TYPE_BEFORE_REMOVING, -100);
110112
$container->addCompilerPass(new CheckTypeDeclarationsPass(true), PassConfig::TYPE_AFTER_REMOVING, -100);
111113

112114
return $this->container = $container;

0 commit comments

Comments
 (0)