Skip to content

Commit

Permalink
Fix JsonCallToExplicitJsonCallRector by skipping GET requests with da…
Browse files Browse the repository at this point in the history
…ta, headers, or options present (#211)
  • Loading branch information
GeniJaho authored May 3, 2024
1 parent 026d322 commit 96469d5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
25 changes: 17 additions & 8 deletions src/Rector/MethodCall/JsonCallToExplicitJsonCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,31 @@ private function updateCall(MethodCall $methodCall): ?MethodCall
return null;
}

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

if (! $argValue instanceof String_) {
if (count($methodCallArgs) < 2) {
return null;
}

$firstArg = $methodCallArgs[0]->value;

if (! $firstArg instanceof 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);
if (! in_array($firstArg->value, $supportedMethods, true)) {
return null;
}

return $methodCall;
if ($firstArg->value === 'GET' && count($methodCallArgs) > 2) {
return null;
}

return null;
$methodCall->name = new Identifier(strtolower($firstArg->value) . 'Json');
$methodCall->args = array_slice($methodCall->args, 1);

return $methodCall;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,12 @@ class FixtureWithJsonCalls
{
$http->json('connect', '/');
}
}

?>
---
<?php

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

class FixtureWithJsonCalls
{
public function testHead(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http)
{
$http->json('head', '/');
}

public function testTrace(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http)
{
$http->json('trace', '/');
}

public function testConnect(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http)
public function testNotEnoughArgs(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http)
{
$http->json('connect', '/');
$http->json('GET');
$http->json();
}
}

?>

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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

class FixtureWithGetJsonCalls
{
public function testGetWithAllParams(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http)
{
$http->json('GET', '/', ['data'], ['headers'], 1);
}

public function testGetWithoutOptions(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http)
{
$http->json('GET', '/', ['data'], ['headers']);
}

public function testGetWithoutHeaders(\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests $http)
{
$http->json('GET', '/', ['data']);
}
}

?>

0 comments on commit 96469d5

Please sign in to comment.