Skip to content

[Deprecation] Deprecate ReturnDirectJsonResponseRector - #987

Merged
TomasVotruba merged 1 commit into
mainfrom
deprecate-return-direct-json-response
Aug 2, 2026
Merged

[Deprecation] Deprecate ReturnDirectJsonResponseRector#987
TomasVotruba merged 1 commit into
mainfrom
deprecate-return-direct-json-response

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

ReturnDirectJsonResponseRector collapses a JsonResponse assignment plus setData() 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:

public function index()
{
    $response = new JsonResponse();
    $response->setCallback('handleData');   // JSONP
    $response->setData(['key' => 'value']);

    return $response;
}

setData() re-encodes the payload and re-applies the callback, so ordering is load-bearing here. The rule rewrites it to return new JsonResponse(['key' => 'value']); and silently drops the setCallback() statement position semantics. Same class of problem for setStatusCode() and $response->headers->set() sequences.

Hardening it means teaching the rule about every JsonResponse setter 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:

// enable once tested
// ReturnDirectJsonResponseRector::class,

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) and setData() both exist since Symfony 2.1.

Changes

  • rule keeps its class and RuleDefinition, implements DeprecatedInterface, refactor() throws
  • commented-out entry removed from symfony-code-quality.php
  • test, fixture and rule config deleted

ControllerMethodAnalyzer and ResponseClass::JSON stay — both still used by other rules.

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.
@TomasVotruba
TomasVotruba merged commit e59ca46 into main Aug 2, 2026
7 checks passed
@TomasVotruba
TomasVotruba deleted the deprecate-return-direct-json-response branch August 2, 2026 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant