Skip to content

Commit

Permalink
Add ResponseHelperCallToJsonResponseRector (#252)
Browse files Browse the repository at this point in the history
* Add ResponseHelperCallToJsonResponseRector

* Fix Checks in ResponseHelperCallToJsonResponseRector

---------

Co-authored-by: Geni Jaho <[email protected]>
  • Loading branch information
Stoffo and GeniJaho authored Oct 1, 2024
1 parent 62598cb commit 7f8c107
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -1360,3 +1360,16 @@ Convert string validation rules into arrays for Laravel's Validator.
```

<br>

## ResponseHelperCallToJsonResponseRector

Change `response()->json()` to `new JsonResponse()`

- class: [`RectorLaravel\Rector\MethodCall\ResponseHelperCallToJsonResponseRector`](../src/Rector/MethodCall/ResponseHelperCallToJsonResponseRector.php)

```diff
- return response()->json(['message' => 'Hello World']);
+ return new \Illuminate\Http\JsonResponse(['message' => 'Hello World']);
```

<br>
68 changes: 68 additions & 0 deletions src/Rector/MethodCall/ResponseHelperCallToJsonResponseRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name\FullyQualified;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \RectorLaravel\Tests\Rector\MethodCall\ResponseHelperCallToJsonResponseRector\ResponseHelperCallToJsonResponseRectorTest
*/
final class ResponseHelperCallToJsonResponseRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Use new JsonResponse instead of response()->json()', [
new CodeSample(
<<<'CODE_SAMPLE'
response()->json(['key' => 'value']);
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
return new JsonResponse(['key' => 'value']);
CODE_SAMPLE
),
]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node->name, 'json')) {
return null;
}

if (! $this->isName($node->var, 'response')) {
return null;
}

if (! $node->var instanceof FuncCall) {
return null;
}

if (count($node->var->args) > 0) {
return null;
}

return new New_(new FullyQualified('Illuminate\Http\JsonResponse'), $node->args);
}
}
11 changes: 11 additions & 0 deletions stubs/Illuminate/Contracts/Routing/ResponseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Illuminate\Contracts\Routing;

if (class_exists('Illuminate\Contracts\Routing\ResponseFactory')) {
return;
}

interface ResponseFactory
{
}
13 changes: 13 additions & 0 deletions stubs/Illuminate/Routing/ResponseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Illuminate\Routing;

if (class_exists('Illuminate\Routing\ResponseFactory')) {
return;
}

class ResponseFactory implements \Illuminate\Contracts\Routing\ResponseFactory
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\ResponseHelperCallToJsonResponseRector\Fixture;

class FixtureWithResponseHelperCalls
{
public function testNoArguments()
{
return response()->json();
}

public function testVariableWithSameNameIsNotChanged()
{
return $response->json();
}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\ResponseHelperCallToJsonResponseRector\Fixture;

class FixtureWithResponseHelperCalls
{
public function testNoArguments()
{
return new \Illuminate\Http\JsonResponse();
}

public function testVariableWithSameNameIsNotChanged()
{
return $response->json();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\ResponseHelperCallToJsonResponseRector\Fixture;

class FixtureWithResponseHelperCalls
{
public function testWithArguments()
{
return response()->json(['key' => 'value']);
}

public function testWithVariableAsArgument()
{
$a = new \stdClass();
return response()->json($a);
}

public function testWithAllArguments()
{
return response()->json(['key' => 'value'], 200, ['Content-Type' => 'application/json'], JSON_PRETTY_PRINT, true);
}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\ResponseHelperCallToJsonResponseRector\Fixture;

class FixtureWithResponseHelperCalls
{
public function testWithArguments()
{
return new \Illuminate\Http\JsonResponse(['key' => 'value']);
}

public function testWithVariableAsArgument()
{
$a = new \stdClass();
return new \Illuminate\Http\JsonResponse($a);
}

public function testWithAllArguments()
{
return new \Illuminate\Http\JsonResponse(['key' => 'value'], 200, ['Content-Type' => 'application/json'], JSON_PRETTY_PRINT, true);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\ResponseHelperCallToJsonResponseRector\Fixture;

class FixtureWithResponseHelperCalls
{
public function testWithChaining()
{
return response()->json()->status();
}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\ResponseHelperCallToJsonResponseRector\Fixture;

class FixtureWithResponseHelperCalls
{
public function testWithChaining()
{
return (new \Illuminate\Http\JsonResponse())->status();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Tests\Rector\MethodCall\ResponseHelperCallToJsonResponseRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ResponseHelperCallToJsonResponseRectorTest extends AbstractRectorTestCase
{
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

#[Test]
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\MethodCall\ResponseHelperCallToJsonResponseRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');

$rectorConfig->rule(ResponseHelperCallToJsonResponseRector::class);
};

0 comments on commit 7f8c107

Please sign in to comment.