Skip to content

Commit e062bee

Browse files
authored
[TypeDeclarationDocblocks] Allow named arg on AddReturnDocblockForJsonArrayRector (#7348)
1 parent b2dcde5 commit e062bee

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector\Fixture;
6+
7+
use Nette\Utils\Json;
8+
9+
final class JsonUtilsWithNamedArg
10+
{
11+
public function provide(string $contents): array
12+
{
13+
return Json::decode(forceArrays: true, json: $contents);
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
declare(strict_types=1);
22+
23+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector\Fixture;
24+
25+
use Nette\Utils\Json;
26+
27+
final class JsonUtilsWithNamedArg
28+
{
29+
/**
30+
* @return array<string, mixed>
31+
*/
32+
public function provide(string $contents): array
33+
{
34+
return Json::decode(forceArrays: true, json: $contents);
35+
}
36+
}
37+
38+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector\Fixture;
6+
7+
final class WithNamedArg
8+
{
9+
public function provide(string $contents): array
10+
{
11+
return json_decode(associative: true, json: $contents);
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
declare(strict_types=1);
20+
21+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector\Fixture;
22+
23+
final class WithNamedArg
24+
{
25+
/**
26+
* @return array<string, mixed>
27+
*/
28+
public function provide(string $contents): array
29+
{
30+
return json_decode(associative: true, json: $contents);
31+
}
32+
}
33+
34+
?>

rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForJsonArrayRector.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\TypeDeclarationDocblocks\Rector\ClassMethod;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
89
use PhpParser\Node\Expr;
910
use PhpParser\Node\Expr\FuncCall;
1011
use PhpParser\Node\Expr\StaticCall;
@@ -139,12 +140,12 @@ private function isJsonDecodeToArray(Expr $expr): bool
139140
return false;
140141
}
141142

142-
if (count($expr->getArgs()) !== 2) {
143+
$arg = $expr->getArg('associative', 1);
144+
if (! $arg instanceof Arg) {
143145
return false;
144146
}
145147

146-
$secondArg = $expr->getArgs()[1];
147-
return $this->valueResolver->isTrue($secondArg->value);
148+
return $this->valueResolver->isTrue($arg->value);
148149
}
149150

150151
if ($expr instanceof StaticCall) {
@@ -160,12 +161,12 @@ private function isJsonDecodeToArray(Expr $expr): bool
160161
return false;
161162
}
162163

163-
if (count($expr->getArgs()) !== 2) {
164+
$arg = $expr->getArg('forceArrays', 1);
165+
if (! $arg instanceof Arg) {
164166
return false;
165167
}
166168

167-
$secondArg = $expr->getArgs()[1];
168-
return $this->valueResolver->isTrue($secondArg->value);
169+
return $this->valueResolver->isTrue($arg->value);
169170
}
170171

171172
return false;

0 commit comments

Comments
 (0)