-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ResponseHelperCallToJsonResponseRector
- Loading branch information
Showing
7 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
59 changes: 59 additions & 0 deletions
59
src/Rector/MethodCall/ResponseHelperCallToJsonResponseRector.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?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; | ||
|
||
/** | ||
* @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; | ||
} | ||
|
||
return new New_(new FullyQualified('Illuminate\Http\JsonResponse'), $node->args); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/Rector/MethodCall/ResponseHelperCallToJsonResponseRector/Fixture/fixture.php.inc
This file contains 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
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(); | ||
} | ||
} | ||
|
||
?> |
49 changes: 49 additions & 0 deletions
49
...s/Rector/MethodCall/ResponseHelperCallToJsonResponseRector/Fixture/with_arguments.php.inc
This file contains 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
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); | ||
} | ||
} | ||
|
||
?> |
27 changes: 27 additions & 0 deletions
27
tests/Rector/MethodCall/ResponseHelperCallToJsonResponseRector/Fixture/with_chaining.php.inc
This file contains 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
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(); | ||
} | ||
} | ||
|
||
?> |
30 changes: 30 additions & 0 deletions
30
...all/ResponseHelperCallToJsonResponseRector/ResponseHelperCallToJsonResponseRectorTest.php
This file contains 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
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'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Rector/MethodCall/ResponseHelperCallToJsonResponseRector/config/configured_rule.php
This file contains 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
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); | ||
}; |