fix ControllerMethodInjectionToConstructorRector - update call sites when params are moved to constructor#934
Merged
Conversation
…rgs when method params are moved to constructor When a helper method (e.g. problematic(LoggerInterface $logger, ?string $optional)) had a service param moved to the constructor, any $this->problematic($logger) call sites in other methods were not updated to drop the now-removed argument, producing invalid code. The fix tracks which parameter positions are removed per method and, after all existing transforms run, traverses every method body to remove the corresponding args from $this->methodName(...) call sites. Fixes rectorphp/rector#9695
Member
Author
|
Good job :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes rectorphp/rector#9695
When a controller has a helper method that takes both a service parameter and optional scalar parameters:
The rule correctly moved
LoggerInterface $loggerout of both method signatures and into the constructor, and replaced$loggerusages inside each method body with$this->logger. However, it did not update the call site$this->problematic($logger)insomeAction— after$loggerwas replaced with$this->loggerby the variable-replacement step, the call became$this->problematic($this->logger), even thoughproblematicno longer accepts that argument. This produced invalid code.Fix
After all param removals and variable replacements, a new
updateCallSitesForRemovedParamsstep traverses every method body in the class. For any$this->methodName(...)call whose method had parameters removed, the corresponding positional arguments are removed from the call.Changes
rules/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector.php— track removed param positions per method; addupdateCallSitesForRemovedParams()that strips the stale arguments from internal call sitesrules-tests/.../Fixture/internal_method_call_arg_removal.php.inc— new fixture reproducing the exact scenario from the issueGenerated by Claude Code