Skip to content

Commit 3362d90

Browse files
committed
cover no direct return array too
1 parent a885833 commit 3362d90

4 files changed

Lines changed: 78 additions & 16 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;
4+
5+
final class IncludeMissingReturnArray
6+
{
7+
public function getNames()
8+
{
9+
return [
10+
'key' => 'value',
11+
];
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;
20+
21+
final class IncludeMissingReturnArray
22+
{
23+
/**
24+
* @return array<string, string>
25+
*/
26+
public function getNames()
27+
{
28+
return [
29+
'key' => 'value',
30+
];
31+
}
32+
}
33+
34+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;
4+
5+
final class SkipAlreadySetDocblock
6+
{
7+
/**
8+
* @return array<string, int|string>
9+
*/
10+
public function getNames(): array
11+
{
12+
return [
13+
'key' => 'value',
14+
];
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;
4+
5+
final class SkipMoreVariables
6+
{
7+
public function getNames()
8+
{
9+
$variable = [
10+
'key' => 'value',
11+
];
12+
13+
14+
return $variable;
15+
}
16+
}

rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ public function getNodeTypes(): array
4343

4444
public function getRuleDefinition(): RuleDefinition
4545
{
46-
return new RuleDefinition('Add @return array docblock based on direct single level direct return of []', [
47-
new CodeSample(
48-
<<<'CODE_SAMPLE'
46+
return new RuleDefinition(
47+
'Add simple @return array docblock based on direct single level direct return of []',
48+
[
49+
new CodeSample(
50+
<<<'CODE_SAMPLE'
4951
class SomeClass
5052
{
5153
public function getItems(): array
@@ -56,8 +58,8 @@ public function getItems(): array
5658
}
5759
}
5860
CODE_SAMPLE
59-
,
60-
<<<'CODE_SAMPLE'
61+
,
62+
<<<'CODE_SAMPLE'
6163
class SomeClass
6264
{
6365
/**
@@ -71,31 +73,25 @@ public function getItems(): array
7173
}
7274
}
7375
CODE_SAMPLE
74-
),
75-
]);
76+
),
77+
78+
]
79+
);
7680
}
7781

7882
/**
7983
* @param ClassMethod $node
8084
*/
8185
public function refactor(Node $node): ?Node
8286
{
83-
if (! $node->returnType instanceof Node) {
84-
return null;
85-
}
86-
87-
if (! $this->isName($node->returnType, 'array')) {
88-
return null;
89-
}
90-
9187
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
9288

9389
// return tag is already given
9490
if ($phpDocInfo->getReturnTagValue() instanceof ReturnTagValueNode) {
9591
return null;
9692
}
9793

98-
if (count($node->stmts) !== 1) {
94+
if ($node->stmts === null || count($node->stmts) !== 1) {
9995
return null;
10096
}
10197

0 commit comments

Comments
 (0)