Skip to content

Commit bb222ef

Browse files
authored
[type-declaration-docblocks] Add AddReturnDocblockForJsonArrayRector (#7320)
1 parent 1618d2d commit bb222ef

11 files changed

Lines changed: 335 additions & 16 deletions

File tree

config/set/type-declaration-docblocks.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockFromDimFetchAccessRector;
1515
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForArrayDimAssignedObjectRector;
1616
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector;
17+
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector;
1718
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector;
1819
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector;
1920

@@ -38,6 +39,7 @@
3839
AddReturnDocblockForScalarArrayFromAssignsRector::class,
3940
DocblockReturnArrayFromDirectArrayInstanceRector::class,
4041
AddReturnDocblockForArrayDimAssignedObjectRector::class,
42+
AddReturnDocblockForJsonArrayRector::class,
4143

4244
// tests
4345
AddParamArrayDocblockFromDataProviderRector::class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AddReturnDocblockForJsonArrayRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 38 additions & 0 deletions
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 JsonUtils
10+
{
11+
public function provide(string $contents): array
12+
{
13+
return Json::decode($contents, true);
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 JsonUtils
28+
{
29+
/**
30+
* @return array<string, mixed>
31+
*/
32+
public function provide(string $contents): array
33+
{
34+
return Json::decode($contents, true);
35+
}
36+
}
37+
38+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector\Fixture;
6+
7+
final class SkipAlreadyFilled
8+
{
9+
/**
10+
* @return array<string, array<string, mixed>>
11+
*/
12+
public function provide(string $contents): array
13+
{
14+
return json_decode($contents, true);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector\Fixture;
6+
7+
final class SkipMissingArg
8+
{
9+
public function provide(string $contents): array
10+
{
11+
return json_decode($contents, false);
12+
}
13+
}
Lines changed: 34 additions & 0 deletions
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 SomeClass
8+
{
9+
public function provide(string $contents): array
10+
{
11+
return json_decode($contents, true);
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 SomeClass
24+
{
25+
/**
26+
* @return array<string, mixed>
27+
*/
28+
public function provide(string $contents): array
29+
{
30+
return json_decode($contents, true);
31+
}
32+
}
33+
34+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([AddReturnDocblockForJsonArrayRector::class]);

rules/NetteUtils/Rector/StaticCall/UtilsJsonStaticCallNamedArgRector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\Node\Expr\StaticCall;
1010
use PhpParser\Node\Identifier;
1111
use Rector\Rector\AbstractRector;
12+
use Rector\TypeDeclarationDocblocks\Enum\NetteClassName;
1213
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1314
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1415

@@ -49,7 +50,7 @@ public function getNodeTypes(): array
4950
*/
5051
public function refactor(Node $node): ?Node
5152
{
52-
if (! $this->isName($node->class, 'Nette\Utils\Json')) {
53+
if (! $this->isName($node->class, NetteClassName::JSON)) {
5354
return null;
5455
}
5556

rules/Php84/Rector/Foreach_/ForeachToArrayAllRector.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ public function provideMinPhpVersion(): int
102102
return PhpVersionFeature::ARRAY_ALL;
103103
}
104104

105-
private function refactorBooleanAssignmentPattern(StmtsAwareInterface $node): ?Node
105+
private function refactorBooleanAssignmentPattern(StmtsAwareInterface $stmtsAware): ?Node
106106
{
107-
foreach ($node->stmts as $key => $stmt) {
107+
foreach ($stmtsAware->stmts as $key => $stmt) {
108108
if (! $stmt instanceof Foreach_) {
109109
continue;
110110
}
111111

112-
$prevStmt = $node->stmts[$key - 1] ?? null;
112+
$prevStmt = $stmtsAware->stmts[$key - 1] ?? null;
113113
if (! $prevStmt instanceof Expression) {
114114
continue;
115115
}
@@ -136,7 +136,7 @@ private function refactorBooleanAssignmentPattern(StmtsAwareInterface $node): ?N
136136
}
137137

138138
if ($this->stmtsManipulator->isVariableUsedInNextStmt(
139-
$node,
139+
$stmtsAware,
140140
$key + 1,
141141
(string) $this->getName($foreach->valueVar)
142142
)) {
@@ -174,26 +174,26 @@ private function refactorBooleanAssignmentPattern(StmtsAwareInterface $node): ?N
174174
$newAssign = new Assign($assignedVariable, $funcCall);
175175
$newExpression = new Expression($newAssign);
176176

177-
unset($node->stmts[$key - 1]);
178-
$node->stmts[$key] = $newExpression;
177+
unset($stmtsAware->stmts[$key - 1]);
178+
$stmtsAware->stmts[$key] = $newExpression;
179179

180-
$node->stmts = array_values($node->stmts);
180+
$stmtsAware->stmts = array_values($stmtsAware->stmts);
181181

182-
return $node;
182+
return $stmtsAware;
183183
}
184184

185185
return null;
186186
}
187187

188-
private function refactorEarlyReturnPattern(StmtsAwareInterface $node): ?Node
188+
private function refactorEarlyReturnPattern(StmtsAwareInterface $stmtsAware): ?Node
189189
{
190-
foreach ($node->stmts as $key => $stmt) {
190+
foreach ($stmtsAware->stmts as $key => $stmt) {
191191
if (! $stmt instanceof Foreach_) {
192192
continue;
193193
}
194194

195195
$foreach = $stmt;
196-
$nextStmt = $node->stmts[$key + 1] ?? null;
196+
$nextStmt = $stmtsAware->stmts[$key + 1] ?? null;
197197

198198
if (! $nextStmt instanceof Return_) {
199199
continue;
@@ -236,11 +236,11 @@ private function refactorEarlyReturnPattern(StmtsAwareInterface $node): ?Node
236236

237237
$funcCall = $this->nodeFactory->createFuncCall('array_all', [$foreach->expr, $arrowFunction]);
238238

239-
$node->stmts[$key] = new Return_($funcCall);
240-
unset($node->stmts[$key + 1]);
241-
$node->stmts = array_values($node->stmts);
239+
$stmtsAware->stmts[$key] = new Return_($funcCall);
240+
unset($stmtsAware->stmts[$key + 1]);
241+
$stmtsAware->stmts = array_values($stmtsAware->stmts);
242242

243-
return $node;
243+
return $stmtsAware;
244244
}
245245

246246
return null;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\TypeDeclarationDocblocks\Enum;
6+
7+
final class NetteClassName
8+
{
9+
/**
10+
* @var string
11+
*/
12+
public const JSON = 'Nette\Utils\Json';
13+
}

0 commit comments

Comments
 (0)