-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
StrictReturnNewAnalyzer.php
137 lines (110 loc) · 3.69 KB
/
StrictReturnNewAnalyzer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
declare(strict_types=1);
namespace Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;
final class StrictReturnNewAnalyzer
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly NodeNameResolver $nodeNameResolver,
) {
}
public function matchAlwaysReturnVariableNew(ClassMethod|Closure|Function_ $functionLike): ?string
{
if ($functionLike->stmts === null) {
return null;
}
if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($functionLike, [Yield_::class])) {
return null;
}
/** @var Return_[] $returns */
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($functionLike, Return_::class);
if ($returns === []) {
return null;
}
// is one statement depth 3?
if (! $this->areExclusiveExprReturns($returns)) {
return null;
}
// has root return?
if (! $this->hasClassMethodRootReturn($functionLike)) {
return null;
}
if (count($returns) !== 1) {
return null;
}
// exact one return of variable
$onlyReturn = $returns[0];
if (! $onlyReturn->expr instanceof Variable) {
return null;
}
$createdVariablesToTypes = $this->resolveCreatedVariablesToTypes($functionLike);
$returnedVariableName = $this->nodeNameResolver->getName($onlyReturn->expr);
return $createdVariablesToTypes[$returnedVariableName] ?? null;
}
/**
* @param Return_[] $returns
*/
private function areExclusiveExprReturns(array $returns): bool
{
foreach ($returns as $return) {
if (! $return->expr instanceof Expr) {
return false;
}
}
return true;
}
private function hasClassMethodRootReturn(ClassMethod|Function_|Closure $functionLike): bool
{
foreach ((array) $functionLike->stmts as $stmt) {
if ($stmt instanceof Return_) {
return true;
}
}
return false;
}
/**
* @return array<string, string>
*/
private function resolveCreatedVariablesToTypes(ClassMethod|Function_|Closure $functionLike): array
{
$createdVariablesToTypes = [];
// what new is assigned to it?
foreach ((array) $functionLike->stmts as $stmt) {
if (! $stmt instanceof Expression) {
continue;
}
if (! $stmt->expr instanceof Assign) {
continue;
}
$assign = $stmt->expr;
if (! $assign->expr instanceof New_) {
continue;
}
if (! $assign->var instanceof Variable) {
continue;
}
$variableName = $this->nodeNameResolver->getName($assign->var);
if (! is_string($variableName)) {
continue;
}
$className = $this->nodeNameResolver->getName($assign->expr->class);
if (! is_string($className)) {
continue;
}
$createdVariablesToTypes[$variableName] = $className;
}
return $createdVariablesToTypes;
}
}