Skip to content

Commit

Permalink
[Php73] Handle named argument on JsonThrowOnErrorRector (#1021)
Browse files Browse the repository at this point in the history
* Test case for Incorrect behavior of JsonThrowOnErrorRector #6752

* Closes #1020 Fixes rectorphp/rector#6752

* allow named on json or value key

* not allowed named argument

* clean up

* clean up

Co-authored-by: Björn Bösel <bjoernboesel@gmail.com>
  • Loading branch information
samsonasik and fivetide committed Oct 18, 2021
1 parent 33e5cab commit 34b254a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\Tests\Php73\Rector\FuncCall\JsonThrowOnErrorRector\Fixture;

function skipNamedArgumentFlags($str){
json_decode($str, flags: JSON_THROW_ON_ERROR);
}

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

namespace Rector\Tests\Php73\Rector\FuncCall\JsonThrowOnErrorRector\Fixture;

function skipNamedArgumentValueOrJson($str, array $data){
json_decode(json: $str);
json_encode(value: $data);
}
8 changes: 8 additions & 0 deletions rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\LNumber;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -82,6 +83,13 @@ private function shouldSkip(FuncCall $funcCall): bool
return true;
}

$args = $funcCall->getArgs();
foreach ($args as $arg) {
if ($arg->name instanceof Identifier) {
return true;
}
}

return (bool) $this->betterNodeFinder->findFirstNext($funcCall, function (Node $node): bool {
if (! $node instanceof FuncCall) {
return false;
Expand Down

0 comments on commit 34b254a

Please sign in to comment.