Skip to content

Commit 4714683

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

4 files changed

Lines changed: 82 additions & 19 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: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Rector\TypeDeclarationDocblocks\Rector\ClassMethod;
66

7+
use PHPStan\Type\Type;
78
use PhpParser\Node;
89
use PhpParser\Node\Expr\Array_;
910
use PhpParser\Node\Stmt\ClassMethod;
@@ -43,9 +44,11 @@ public function getNodeTypes(): array
4344

4445
public function getRuleDefinition(): RuleDefinition
4546
{
46-
return new RuleDefinition('Add @return array docblock based on direct single level direct return of []', [
47-
new CodeSample(
48-
<<<'CODE_SAMPLE'
47+
return new RuleDefinition(
48+
'Add simple @return array docblock based on direct single level direct return of []',
49+
[
50+
new CodeSample(
51+
<<<'CODE_SAMPLE'
4952
class SomeClass
5053
{
5154
public function getItems(): array
@@ -56,8 +59,8 @@ public function getItems(): array
5659
}
5760
}
5861
CODE_SAMPLE
59-
,
60-
<<<'CODE_SAMPLE'
62+
,
63+
<<<'CODE_SAMPLE'
6164
class SomeClass
6265
{
6366
/**
@@ -71,31 +74,25 @@ public function getItems(): array
7174
}
7275
}
7376
CODE_SAMPLE
74-
),
75-
]);
77+
),
78+
79+
]
80+
);
7681
}
7782

7883
/**
7984
* @param ClassMethod $node
8085
*/
8186
public function refactor(Node $node): ?Node
8287
{
83-
if (! $node->returnType instanceof Node) {
84-
return null;
85-
}
86-
87-
if (! $this->isName($node->returnType, 'array')) {
88-
return null;
89-
}
90-
9188
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
9289

9390
// return tag is already given
9491
if ($phpDocInfo->getReturnTagValue() instanceof ReturnTagValueNode) {
9592
return null;
9693
}
9794

98-
if (count($node->stmts) !== 1) {
95+
if ($node->stmts === null || count($node->stmts) !== 1) {
9996
return null;
10097
}
10198

@@ -131,7 +128,7 @@ public function refactor(Node $node): ?Node
131128
/**
132129
* covers constant types too and makes them more generic
133130
*/
134-
private function constantToGenericType(\PHPStan\Type\Type $type): \PHPStan\Type\Type
131+
private function constantToGenericType(Type $type): Type
135132
{
136133
if ($type instanceof StringType) {
137134
return new StringType();
@@ -154,8 +151,8 @@ private function constantToGenericType(\PHPStan\Type\Type $type): \PHPStan\Type\
154151
}
155152

156153
private function createArrayGenericTypeNode(
157-
\PHPStan\Type\Type $keyType,
158-
\PHPStan\Type\Type $itemType
154+
Type $keyType,
155+
Type $itemType
159156
): GenericTypeNode {
160157
$keyDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($keyType);
161158
$itemDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($itemType);

0 commit comments

Comments
 (0)