Commit fd7d88d
committed
feature #50745 [DependencyInjection] Add
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 compatibilityCheckAliasValidityPass to check interface compatibility (n-valverde)2 files changed
+3
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
107 | 108 | | |
108 | 109 | | |
109 | 110 | | |
| 111 | + | |
110 | 112 | | |
111 | 113 | | |
112 | 114 | | |
| |||
0 commit comments