From 6c9a160aa6bb234f4179b12528f3d3c2dbc2862f Mon Sep 17 00:00:00 2001 From: Stefan Lange Date: Thu, 4 Jan 2024 00:37:31 +0100 Subject: [PATCH] Add JsonCallToExplicitJsonCallRector (#158) --- .../JsonCallToExplicitJsonCallRector.php | 85 +++++++++++++++++++ .../Fixture/fixture_with_json_calls.php.inc | 78 +++++++++++++++++ .../fixture_with_json_calls_to_skip.php.inc | 48 +++++++++++ .../JsonCallToExplicitJsonCallRectorTest.php | 31 +++++++ .../config/configured_rule.php | 12 +++ 5 files changed, 254 insertions(+) create mode 100644 src/Rector/MethodCall/JsonCallToExplicitJsonCallRector.php create mode 100644 tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/Fixture/fixture_with_json_calls.php.inc create mode 100644 tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/Fixture/fixture_with_json_calls_to_skip.php.inc create mode 100644 tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/JsonCallToExplicitJsonCallRectorTest.php create mode 100644 tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/config/configured_rule.php diff --git a/src/Rector/MethodCall/JsonCallToExplicitJsonCallRector.php b/src/Rector/MethodCall/JsonCallToExplicitJsonCallRector.php new file mode 100644 index 00000000..e7f732af --- /dev/null +++ b/src/Rector/MethodCall/JsonCallToExplicitJsonCallRector.php @@ -0,0 +1,85 @@ +json to $this->postJson, $this->putJson, etc.', + [ + new CodeSample( + // code before + '$this->json("POST", "/api/v1/users", $data);', + // code after + '§this->postJson("/api/v1/users", $data);' + ), + ] + ); + } + + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + public function refactor(Node $node): ?Node + { + if ($node instanceof MethodCall) { + return $this->updateCall($node); + } + + return null; + } + + private function updateCall(MethodCall $methodCall): ?MethodCall + { + $methodCallName = $this->getName($methodCall->name); + if ($methodCallName === null) { + return null; + } + + if (! $this->isObjectType( + $methodCall->var, + new ObjectType('Illuminate\Foundation\Testing\Concerns\MakesHttpRequests') + )) { + return null; + } + + if ($methodCallName !== 'json') { + return null; + } + + $arg = $methodCall->getArgs()[0]; + $argValue = $arg->value; + + if (! $argValue instanceof Node\Scalar\String_) { + return null; + } + + $supportedMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']; + + if (in_array($argValue->value, $supportedMethods, true)) { + $methodCall->name = new Identifier(strtolower($argValue->value) . 'Json'); + $methodCall->args = array_slice($methodCall->args, 1); + + return $methodCall; + } + + return null; + } +} diff --git a/tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/Fixture/fixture_with_json_calls.php.inc b/tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/Fixture/fixture_with_json_calls.php.inc new file mode 100644 index 00000000..49e8bcd3 --- /dev/null +++ b/tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/Fixture/fixture_with_json_calls.php.inc @@ -0,0 +1,78 @@ +json('get', '/'); + } + + public function testPost(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http) + { + $http->json('post', '/'); + } + + public function testPut(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http) + { + $http->json('put', '/'); + } + + public function testPatch(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http) + { + $http->json('patch', '/'); + } + + public function testDelete(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http) + { + $http->json('delete', '/'); + } + + public function testOptions(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http) + { + $http->json('options', '/'); + } +} + +?> +--- +getJson('/'); + } + + public function testPost(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http) + { + $http->postJson('/'); + } + + public function testPut(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http) + { + $http->putJson('/'); + } + + public function testPatch(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http) + { + $http->patchJson('/'); + } + + public function testDelete(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http) + { + $http->deleteJson('/'); + } + + public function testOptions(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http) + { + $http->optionsJson('/'); + } +} + +?> + diff --git a/tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/Fixture/fixture_with_json_calls_to_skip.php.inc b/tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/Fixture/fixture_with_json_calls_to_skip.php.inc new file mode 100644 index 00000000..a452df07 --- /dev/null +++ b/tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/Fixture/fixture_with_json_calls_to_skip.php.inc @@ -0,0 +1,48 @@ +json('head', '/'); + } + + public function testTrace(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http) + { + $http->json('trace', '/'); + } + + public function testConnect(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http) + { + $http->json('connect', '/'); + } +} + +?> +--- +json('head', '/'); + } + + public function testTrace(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http) + { + $http->json('trace', '/'); + } + + public function testConnect(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http) + { + $http->json('connect', '/'); + } +} + +?> + diff --git a/tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/JsonCallToExplicitJsonCallRectorTest.php b/tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/JsonCallToExplicitJsonCallRectorTest.php new file mode 100644 index 00000000..96bd2d75 --- /dev/null +++ b/tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/JsonCallToExplicitJsonCallRectorTest.php @@ -0,0 +1,31 @@ +doTestFile($filePath); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/config/configured_rule.php b/tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/config/configured_rule.php new file mode 100644 index 00000000..44471c24 --- /dev/null +++ b/tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/config/configured_rule.php @@ -0,0 +1,12 @@ +import(__DIR__ . '/../../../../../config/config.php'); + + $rectorConfig->rule(JsonCallToExplicitJsonCallRector::class); +};