From b12efc021b895d2493548c5b990b80d469d27fd4 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 27 Sep 2025 20:41:53 +0700 Subject: [PATCH] [TypeDeclarationDocblocks] Allow named arg on AddReturnDocblockForJsonArrayRector --- .../Fixture/json_utils_with_named_arg.php.inc | 38 +++++++++++++++++++ .../Fixture/with_named_arg.php.inc | 34 +++++++++++++++++ .../AddReturnDocblockForJsonArrayRector.php | 13 ++++--- 3 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector/Fixture/json_utils_with_named_arg.php.inc create mode 100644 rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector/Fixture/with_named_arg.php.inc 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;