Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ResponseHelperCallToJsonResponseRector #252

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
64 changes: 64 additions & 0 deletions src/Rector/MethodCall/ResponseHelperCallToJsonResponseRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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 PHPStan\Type\ObjectType;
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')) {
GeniJaho marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

if (! $this->isObjectType($node, new ObjectType('Illuminate\Routing\ResponseFactory'))) {
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,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,18 @@
<?php

declare(strict_types=1);

use Illuminate\Routing\ResponseFactory;
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);
};

function response(): ResponseFactory
Stoffo marked this conversation as resolved.
Show resolved Hide resolved
{
return new ResponseFactory();
}
Loading