Skip to content

Commit e1ddbc7

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

2 files changed

Lines changed: 39 additions & 10 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+
?>

rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ 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 []', [
46+
return new RuleDefinition(
47+
'Add simple @return array docblock based on direct single level direct return of []',
48+
[
4749
new CodeSample(
4850
<<<'CODE_SAMPLE'
4951
class SomeClass
@@ -72,6 +74,7 @@ public function getItems(): array
7274
}
7375
CODE_SAMPLE
7476
),
77+
7578
]);
7679
}
7780

@@ -80,22 +83,14 @@ public function getItems(): array
8083
*/
8184
public function refactor(Node $node): ?Node
8285
{
83-
if (! $node->returnType instanceof Node) {
84-
return null;
85-
}
86-
87-
if (! $this->isName($node->returnType, 'array')) {
88-
return null;
89-
}
90-
9186
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
9287

9388
// return tag is already given
9489
if ($phpDocInfo->getReturnTagValue() instanceof ReturnTagValueNode) {
9590
return null;
9691
}
9792

98-
if (count($node->stmts) !== 1) {
93+
if ($node->stmts === null || count($node->stmts) !== 1) {
9994
return null;
10095
}
10196

0 commit comments

Comments
 (0)