Skip to content

Commit

Permalink
Add ResponseHelperCallToJsonResponseRector
Browse files Browse the repository at this point in the history
  • Loading branch information
Stoffo committed Sep 17, 2024
1 parent 321a7f8 commit 4756e0b
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 3 deletions.
13 changes: 13 additions & 0 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -1409,3 +1409,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>
62 changes: 62 additions & 0 deletions src/Rector/MethodCall/ResponseHelperCallToJsonResponseRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Rector\MethodCall;

use PhpParser\Node;
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;

/**
* @changelog https://laravel.com/docs/7.x/database-testing#creating-models
* @changelog https://laravel.com/docs/8.x/database-testing#instantiating-models
*
* @see \RectorLaravel\Tests\Rector\FuncCall\FactoryFuncCallToStaticCallRector\FactoryFuncCallToStaticCallRectorTest
*/
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;
}

return new New_(new FullyQualified('Illuminate\Http\JsonResponse'), $node->args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Fixture;

use RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Source\HelperObject;
use RectorLaravel\Tests\Rector\MethodCall\ResponseHelperCallToJsonResponseRector\Source\HelperObject;

with(new HelperObject())->store();

Expand All @@ -20,7 +20,7 @@ $foo = with($user);

namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Fixture;

use RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Source\HelperObject;
use RectorLaravel\Tests\Rector\MethodCall\ResponseHelperCallToJsonResponseRector\Source\HelperObject;

(new HelperObject())->store();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Fixture;

use RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\Source\HelperObject;
use RectorLaravel\Tests\Rector\MethodCall\ResponseHelperCallToJsonResponseRector\Source\HelperObject;

with(new HelperObject(), function () {

Expand Down
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 testNoArguments()
{
return response()->json();
}
}

?>
-----
<?php

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

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

?>
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 4756e0b

Please sign in to comment.