-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Target
attribute with invalid name should throw exception
#45020
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
Comments
The Target attribute only overrides the name used for named autowiring aliases (instead of using the parameter name). So we cannot throw an exception easily in such case. It would require making the usage of a named alias mandatory in some cases but not in others. |
I just realized we have the same problem without public function __construct(LoggerInterface $someLogger) It just feels more explicit when using the |
@kbond but we cannot throw an exception for such case where there is no |
Understood, this issue may very well be invalid or not possible/practical to fix. Just wanted to bring up a pain point I had. |
I'm waiting to be on php 8.1 to test it but would this work? namespace App;
enum Logger {
case SomeLogger;
public function alias(): string {
// here convert this::class to the alias name automatically
}
} and then:
and, if
so we could do |
This would feel über complex and serve no practical purpose to me @mathroc Giving arguments a name is something that should remain local to a class. About your concern @kbond, it is resolved if one doesn't define a generic autowiring alias (non-named one) next to named-ones. But as described, this is expected to me. No bug here. If we're looking at this issue as a feature request, we could maybe add an argument to the attribute to explicitly exclude the generic alias from matching. I don't know how we would implement that. Feel free to dig and submit a PR :) |
Got it, I'm going to leave alone for now. As |
Symfony version(s) affected
5.3+
Description
There is nothing stopping you from using an invalid name for the
Target
DI attribute.How to reproduce
In the above example, autowiring is not configured for
Psr\Log\LoggerInterface $someLogger
, the default logger is injected. I can imagine a scenario where you have a service injected but after an upgrade, the name is no longer valid so it is silently swapped for the default.Possible Solution
I think the
Target
name should be validated when building the container and an exception thrown if invalid.Additional Context
No response
The text was updated successfully, but these errors were encountered: