-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Remove skipping magic for autowired methods #22030
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
Conversation
For reference, here are the original decisions we made about how to try to autowire setter methods, but not fail as often / consistently when compared with constructor autowiring - items 2, 3 and 4 in my comment: #17608 (comment) I think this PR is wonderful! The @nicolas-grekas the only thing I noticed when trying this was that if I had Thanks! |
@weaverryan As |
Nicolas and I were talking off-GitHub, and we agree - we should always call methods with What I forgot was that a method with no arguments is getter-injected. In other words, it's incorrect for me to assume that I could add |
I think throwing an exception when there is a |
3fe448f
to
a6bfe1c
Compare
Exceptions added, all code related to skipping logic removed. |
$isConstructor = $reflectionMethod->isConstructor(); | ||
|
||
if (!$isConstructor && !$arguments && !$reflectionMethod->getNumberOfRequiredParameters()) { | ||
throw new RuntimeException(sprintf('Cannot autowire service "%s": method %s::%s() has only optional arguments, thus must be wired explicitly.', $this->currentId, $reflectionMethod->class, $reflectionMethod->name)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can also be hit when there is no args at all, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a method with no argument at all is considered as a getter, thus can't hit this method, see
https://github.com/symfony/symfony/pull/22030/files#diff-62df969ae028c559d33ffd256de1ac49R219
minor comment, 👍 |
…las-grekas) This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Remove skipping magic for autowired methods | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no (master only) | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Wildcard based autowiring made it required to auto-skip methods that were not wireable. Now that things need to be explicit (ie via the `@required` annotation, or via configuration), this "automagic" behavior is not required anymore. Since it can lead to wtf moments ("*I* did *put that `@required` annotation, why is it ignored by autowiring?*"), I think we should remove it. This also fixes another issue where configured method calls had their optional arguments wired, while we want only the constructor's to behave as such. Commits ------- a6bfe1c [DI] Remove skipping magic for autowired methods
…as-grekas) This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Restore skipping logic when autowiring getters | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Partial revert for #22030: the skipping logic is part of the getter injection experience, which provides laziness, thus shouldn't bother you until you actually call the getter, if you do. Commits ------- b3f494f [DI] Restore skipping logic when autowiring getters
Wildcard based autowiring made it required to auto-skip methods that were not wireable.
Now that things need to be explicit (ie via the
@required
annotation, or via configuration), this "automagic" behavior is not required anymore.Since it can lead to wtf moments ("I did put that
@required
annotation, why is it ignored by autowiring?"), I think we should remove it.This also fixes another issue where configured method calls had their optional arguments wired, while we want only the constructor's to behave as such.