diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector/Fixture/json_utils_with_named_arg.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector/Fixture/json_utils_with_named_arg.php.inc new file mode 100644 index 00000000000..bf95e165f87 --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector/Fixture/json_utils_with_named_arg.php.inc @@ -0,0 +1,38 @@ + +----- + + */ + public function provide(string $contents): array + { + return Json::decode(forceArrays: true, json: $contents); + } +} + +?> diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector/Fixture/with_named_arg.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector/Fixture/with_named_arg.php.inc new file mode 100644 index 00000000000..eaa236e3283 --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector/Fixture/with_named_arg.php.inc @@ -0,0 +1,34 @@ + +----- + + */ + public function provide(string $contents): array + { + return json_decode(associative: true, json: $contents); + } +} + +?> diff --git a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector.php b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector.php index e88d174cc77..29d32ec4606 100644 --- a/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector.php +++ b/rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector.php @@ -5,6 +5,7 @@ namespace Rector\TypeDeclarationDocblocks\Rector\ClassMethod; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\StaticCall; @@ -139,12 +140,12 @@ private function isJsonDecodeToArray(Expr $expr): bool return false; } - if (count($expr->getArgs()) !== 2) { + $arg = $expr->getArg('associative', 1); + if (! $arg instanceof Arg) { return false; } - $secondArg = $expr->getArgs()[1]; - return $this->valueResolver->isTrue($secondArg->value); + return $this->valueResolver->isTrue($arg->value); } if ($expr instanceof StaticCall) { @@ -160,12 +161,12 @@ private function isJsonDecodeToArray(Expr $expr): bool return false; } - if (count($expr->getArgs()) !== 2) { + $arg = $expr->getArg('forceArrays', 1); + if (! $arg instanceof Arg) { return false; } - $secondArg = $expr->getArgs()[1]; - return $this->valueResolver->isTrue($secondArg->value); + return $this->valueResolver->isTrue($arg->value); } return false;