Skip to content

Commit aee3cc5

Browse files
committed
add static all support to AddReturnDocblockFromMethodCallDocblockRector
1 parent b304146 commit aee3cc5

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed
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\AddReturnDocblockFromMethodCallDocblockRector\Fixture;
6+
7+
use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockFromMethodCallDocblockRector\Source\SomeRepository;
8+
9+
final class HandleStaticCall
10+
{
11+
public function getAll(): array
12+
{
13+
return SomeRepository::staticFindAll();
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
declare(strict_types=1);
22+
23+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockFromMethodCallDocblockRector\Fixture;
24+
25+
use Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockFromMethodCallDocblockRector\Source\SomeRepository;
26+
27+
final class HandleStaticCall
28+
{
29+
/**
30+
* @return SomeEntity[]
31+
*/
32+
public function getAll(): array
33+
{
34+
return SomeRepository::staticFindAll();
35+
}
36+
}
37+
38+
?>

rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector/Source/SomeRepository.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@ public function findAllWithoutArray()
2121
{
2222
return [];
2323
}
24+
25+
/**
26+
* @return SomeEntity[]
27+
*/
28+
public static function staticFindAll(): array
29+
{
30+
return [];
31+
}
2432
}

rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr\MethodCall;
9+
use PhpParser\Node\Expr\StaticCall;
910
use PhpParser\Node\Identifier;
1011
use PhpParser\Node\Stmt\ClassMethod;
1112
use PhpParser\Node\Stmt\Return_;
@@ -116,14 +117,16 @@ public function refactor(Node $node): ?Node
116117
}
117118

118119
$onlyReturnWithExpr = $this->returnNodeFinder->findOnlyReturnWithExpr($node);
119-
if (! $onlyReturnWithExpr instanceof Return_ || ! $onlyReturnWithExpr->expr instanceof MethodCall) {
120+
if (! $onlyReturnWithExpr instanceof Return_ || (! $onlyReturnWithExpr->expr instanceof MethodCall && ! $onlyReturnWithExpr->expr instanceof StaticCall)) {
120121
return null;
121122
}
122123

123124
$returnedMethodCall = $onlyReturnWithExpr->expr;
124125

125126
// skip doctrine connection calls, as to generic and not helpful
126-
$callerType = $this->getType($returnedMethodCall->var);
127+
$callerType = $this->getType(
128+
$returnedMethodCall instanceof MethodCall ? $returnedMethodCall->var : $returnedMethodCall->class
129+
);
127130
if ($callerType instanceof ObjectType && $callerType->isInstanceOf(DoctrineClass::CONNECTION)->yes()) {
128131
return null;
129132
}

0 commit comments

Comments
 (0)