forked from vimeo/psalm
/
ExpressionIdentifier.php
224 lines (187 loc) · 8.31 KB
/
ExpressionIdentifier.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace Psalm\Internal\Analyzer\Statements\Expression;
use PhpParser;
use Psalm\Config;
use Psalm\FileSource;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use function count;
use function implode;
use function in_array;
use function is_string;
use function strtolower;
class ExpressionIdentifier
{
public static function getVarId(
PhpParser\Node\Expr $stmt,
?string $this_class_name,
?FileSource $source = null,
?int &$nesting = null
): ?string {
if ($stmt instanceof PhpParser\Node\Expr\Variable && is_string($stmt->name)) {
return '$' . $stmt->name;
}
if ($stmt instanceof PhpParser\Node\Expr\StaticPropertyFetch
&& $stmt->name instanceof PhpParser\Node\Identifier
&& $stmt->class instanceof PhpParser\Node\Name
) {
if (count($stmt->class->parts) === 1
&& in_array(strtolower($stmt->class->parts[0]), ['self', 'static', 'parent'], true)
) {
if (!$this_class_name) {
$fq_class_name = $stmt->class->parts[0];
} else {
$fq_class_name = $this_class_name;
}
} else {
$fq_class_name = $source
? ClassLikeAnalyzer::getFQCLNFromNameObject(
$stmt->class,
$source->getAliases()
)
: implode('\\', $stmt->class->parts);
}
return $fq_class_name . '::$' . $stmt->name->name;
}
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch && $stmt->name instanceof PhpParser\Node\Identifier) {
$object_id = self::getVarId($stmt->var, $this_class_name, $source);
if (!$object_id) {
return null;
}
return $object_id . '->' . $stmt->name->name;
}
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch && $nesting !== null) {
++$nesting;
return self::getVarId($stmt->var, $this_class_name, $source, $nesting);
}
return null;
}
public static function getRootVarId(
PhpParser\Node\Expr $stmt,
?string $this_class_name,
?FileSource $source = null
): ?string {
if ($stmt instanceof PhpParser\Node\Expr\Variable
|| $stmt instanceof PhpParser\Node\Expr\StaticPropertyFetch
) {
return self::getVarId($stmt, $this_class_name, $source);
}
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch && $stmt->name instanceof PhpParser\Node\Identifier) {
$property_root = self::getRootVarId($stmt->var, $this_class_name, $source);
if ($property_root) {
return $property_root . '->' . $stmt->name->name;
}
}
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
return self::getRootVarId($stmt->var, $this_class_name, $source);
}
return null;
}
public static function getArrayVarId(
PhpParser\Node\Expr $stmt,
?string $this_class_name,
?FileSource $source = null
): ?string {
if ($stmt instanceof PhpParser\Node\Expr\Assign) {
return self::getArrayVarId($stmt->var, $this_class_name, $source);
}
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
$root_var_id = self::getArrayVarId($stmt->var, $this_class_name, $source);
$offset = null;
if ($root_var_id) {
if ($stmt->dim instanceof PhpParser\Node\Scalar\String_
|| $stmt->dim instanceof PhpParser\Node\Scalar\LNumber
) {
$offset = $stmt->dim instanceof PhpParser\Node\Scalar\String_
? '\'' . $stmt->dim->value . '\''
: $stmt->dim->value;
} elseif ($stmt->dim instanceof PhpParser\Node\Expr\Variable
&& is_string($stmt->dim->name)
) {
$offset = '$' . $stmt->dim->name;
} elseif ($stmt->dim instanceof PhpParser\Node\Expr\ConstFetch) {
$offset = implode('\\', $stmt->dim->name->parts);
} elseif ($stmt->dim instanceof PhpParser\Node\Expr\PropertyFetch) {
$object_id = self::getArrayVarId($stmt->dim->var, $this_class_name, $source);
if ($object_id && $stmt->dim->name instanceof PhpParser\Node\Identifier) {
$offset = $object_id . '->' . $stmt->dim->name;
}
} elseif ($stmt->dim instanceof PhpParser\Node\Expr\ClassConstFetch
&& $stmt->dim->name instanceof PhpParser\Node\Identifier
&& $stmt->dim->class instanceof PhpParser\Node\Name
&& $stmt->dim->class->parts[0] === 'static'
) {
$offset = 'static::' . $stmt->dim->name;
} elseif ($stmt->dim
&& $source instanceof StatementsAnalyzer
&& ($stmt_dim_type = $source->node_data->getType($stmt->dim))
&& (!$stmt->dim instanceof PhpParser\Node\Expr\ClassConstFetch
|| !$stmt->dim->name instanceof PhpParser\Node\Identifier
|| $stmt->dim->name->name !== 'class'
)
) {
if ($stmt_dim_type->isSingleStringLiteral()) {
$offset = '\'' . $stmt_dim_type->getSingleStringLiteral()->value . '\'';
} elseif ($stmt_dim_type->isSingleIntLiteral()) {
$offset = $stmt_dim_type->getSingleIntLiteral()->value;
}
} elseif ($stmt->dim instanceof PhpParser\Node\Expr\ClassConstFetch
&& $stmt->dim->name instanceof PhpParser\Node\Identifier
) {
/** @var string|null */
$resolved_name = $stmt->dim->class->getAttribute('resolvedName');
if ($resolved_name) {
$offset = $resolved_name . '::' . $stmt->dim->name;
}
}
return $offset !== null ? $root_var_id . '[' . $offset . ']' : null;
}
}
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch) {
$object_id = self::getArrayVarId($stmt->var, $this_class_name, $source);
if (!$object_id) {
return null;
}
if ($stmt->name instanceof PhpParser\Node\Identifier) {
return $object_id . '->' . $stmt->name;
}
if ($source instanceof StatementsAnalyzer
&& ($stmt_name_type = $source->node_data->getType($stmt->name))
&& $stmt_name_type->isSingleStringLiteral()) {
return $object_id . '->' . $stmt_name_type->getSingleStringLiteral()->value;
}
return null;
}
if ($stmt instanceof PhpParser\Node\Expr\ClassConstFetch
&& $stmt->name instanceof PhpParser\Node\Identifier
) {
/** @var string|null */
$resolved_name = $stmt->class->getAttribute('resolvedName');
if ($resolved_name) {
if (($resolved_name === 'self' || $resolved_name === 'static') && $this_class_name) {
$resolved_name = $this_class_name;
}
return $resolved_name . '::' . $stmt->name;
}
}
if ($stmt instanceof PhpParser\Node\Expr\MethodCall
&& $stmt->name instanceof PhpParser\Node\Identifier
&& !$stmt->isFirstClassCallable()
&& !$stmt->getArgs()
) {
$config = Config::getInstance();
if ($config->memoize_method_calls || $stmt->getAttribute('memoizable', false)) {
$lhs_var_name = self::getArrayVarId(
$stmt->var,
$this_class_name,
$source
);
if (!$lhs_var_name) {
return null;
}
return $lhs_var_name . '->' . strtolower($stmt->name->name) . '()';
}
}
return self::getVarId($stmt, $this_class_name, $source);
}
}