[Deprecation] Deprecate ReturnDirectJsonResponseRector - #987
Merged
Conversation
Merging setData() into the JsonResponse constructor only saves a line, but the rule moves the data ahead of any setStatusCode(), setCallback() or header call sitting between the assignment and the return. The rule was never enabled in the symfony-code-quality set and had a single fixture, so drop it instead of hardening it.
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.
ReturnDirectJsonResponseRectorcollapses aJsonResponseassignment plussetData()into a single constructor call:class SomeController extends AbstractController { public function index() { - $response = new JsonResponse(); - $response->setData(['key' => 'value']); - - return $response; + return new JsonResponse(['key' => 'value']); } }Why deprecate
The rule matches on statement position, not on the full statement list between the assignment and the
return. Anything else touching the response gets hoisted past:setData()re-encodes the payload and re-applies the callback, so ordering is load-bearing here. The rule rewrites it toreturn new JsonResponse(['key' => 'value']);and silently drops thesetCallback()statement position semantics. Same class of problem forsetStatusCode()and$response->headers->set()sequences.Hardening it means teaching the rule about every
JsonResponsesetter and bailing on any interleaved statement — a lot of surface for a change that saves one line and no behavior.Why now
The rule was never actually shipped. It sat commented out in
symfony-code-quality.php:so it is registered in no set, reachable only by direct registration, and covered by a single fixture. No version constraint is involved —
JsonResponse::__construct($data)andsetData()both exist since Symfony 2.1.Changes
RuleDefinition, implementsDeprecatedInterface,refactor()throwssymfony-code-quality.phpControllerMethodAnalyzerandResponseClass::JSONstay — both still used by other rules.