Skip to content

Commit 96469d5

Browse files
authored
Fix JsonCallToExplicitJsonCallRector by skipping GET requests with data, headers, or options present (#211)
1 parent 026d322 commit 96469d5

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed

Diff for: src/Rector/MethodCall/JsonCallToExplicitJsonCallRector.php

+17-8
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,31 @@ private function updateCall(MethodCall $methodCall): ?MethodCall
6565
return null;
6666
}
6767

68-
$arg = $methodCall->getArgs()[0];
69-
$argValue = $arg->value;
68+
$methodCallArgs = $methodCall->getArgs();
7069

71-
if (! $argValue instanceof String_) {
70+
if (count($methodCallArgs) < 2) {
71+
return null;
72+
}
73+
74+
$firstArg = $methodCallArgs[0]->value;
75+
76+
if (! $firstArg instanceof String_) {
7277
return null;
7378
}
7479

7580
$supportedMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];
7681

77-
if (in_array($argValue->value, $supportedMethods, true)) {
78-
$methodCall->name = new Identifier(strtolower($argValue->value) . 'Json');
79-
$methodCall->args = array_slice($methodCall->args, 1);
82+
if (! in_array($firstArg->value, $supportedMethods, true)) {
83+
return null;
84+
}
8085

81-
return $methodCall;
86+
if ($firstArg->value === 'GET' && count($methodCallArgs) > 2) {
87+
return null;
8288
}
8389

84-
return null;
90+
$methodCall->name = new Identifier(strtolower($firstArg->value) . 'Json');
91+
$methodCall->args = array_slice($methodCall->args, 1);
92+
93+
return $methodCall;
8594
}
8695
}

Diff for: tests/Rector/MethodCall/JsonCallToExplicitJsonCallRector/Fixture/fixture_with_json_calls_to_skip.php.inc

+3-22
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,12 @@ class FixtureWithJsonCalls
1818
{
1919
$http->json('connect', '/');
2020
}
21-
}
22-
23-
?>
24-
---
25-
<?php
26-
27-
namespace RectorLaravel\Tests\Rector\MethodCall\AssertStatusToAssertMethodRector\Fixture;
28-
29-
class FixtureWithJsonCalls
30-
{
31-
public function testHead(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http)
32-
{
33-
$http->json('head', '/');
34-
}
35-
36-
public function testTrace(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http)
37-
{
38-
$http->json('trace', '/');
39-
}
4021

41-
public function testConnect(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http)
22+
public function testNotEnoughArgs(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http)
4223
{
43-
$http->json('connect', '/');
24+
$http->json('GET');
25+
$http->json();
4426
}
4527
}
4628

4729
?>
48-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\MethodCall\JsonCallToExplicitJsonCallRector\Fixture;
4+
5+
class FixtureWithGetJsonCalls
6+
{
7+
public function testGetWithAllParams(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http)
8+
{
9+
$http->json('GET', '/', ['data'], ['headers'], 1);
10+
}
11+
12+
public function testGetWithoutOptions(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http)
13+
{
14+
$http->json('GET', '/', ['data'], ['headers']);
15+
}
16+
17+
public function testGetWithoutHeaders(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http)
18+
{
19+
$http->json('GET', '/', ['data']);
20+
}
21+
}
22+
23+
?>

0 commit comments

Comments
 (0)