Skip to content

Commit def7e79

Browse files
committed
Add multiple stmts above in DocblockReturnArrayFromDirectArrayInstanceRector
1 parent ead2e71 commit def7e79

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;
4+
5+
final class SomeItemsAbove
6+
{
7+
private static function getValues(): array
8+
{
9+
$list = [1, 2, 3];
10+
11+
return [
12+
[
13+
'key1' => 100,
14+
'key2' => '-25.5%',
15+
],
16+
];
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;
25+
26+
final class SomeItemsAbove
27+
{
28+
/**
29+
* @return array<int, array<string, int|string>>
30+
*/
31+
private static function getValues(): array
32+
{
33+
$list = [1, 2, 3];
34+
35+
return [
36+
[
37+
'key1' => 100,
38+
'key2' => '-25.5%',
39+
],
40+
];
41+
}
42+
}
43+
44+
?>

rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
use PhpParser\Node\Expr\Array_;
99
use PhpParser\Node\Stmt\ClassMethod;
1010
use PhpParser\Node\Stmt\Function_;
11-
use PhpParser\Node\Stmt\Return_;
1211
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
1312
use PHPStan\Type\Constant\ConstantArrayType;
1413
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1514
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
1615
use Rector\Rector\AbstractRector;
16+
use Rector\TypeDeclarationDocblocks\NodeFinder\ReturnNodeFinder;
1717
use Rector\TypeDeclarationDocblocks\TypeResolver\ConstantArrayTypeGeneralizer;
1818
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1919
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -27,6 +27,7 @@ public function __construct(
2727
private readonly PhpDocInfoFactory $phpDocInfoFactory,
2828
private readonly DocBlockUpdater $docBlockUpdater,
2929
private readonly ConstantArrayTypeGeneralizer $constantArrayTypeGeneralizer,
30+
private readonly ReturnNodeFinder $returnNodeFinder,
3031
) {
3132
}
3233

@@ -85,15 +86,11 @@ public function refactor(Node $node): ?Node
8586
return null;
8687
}
8788

88-
if ($node->stmts === null || count($node->stmts) !== 1) {
89-
return null;
90-
}
91-
92-
$soleReturn = $node->stmts[0];
93-
if (! $soleReturn instanceof Return_) {
89+
if ($node->stmts === null) {
9490
return null;
9591
}
9692

93+
$soleReturn = $this->returnNodeFinder->findOnlyReturnWithExpr($node);
9794
if (! $soleReturn->expr instanceof Array_) {
9895
return null;
9996
}

0 commit comments

Comments
 (0)