-
Notifications
You must be signed in to change notification settings - Fork 308
/
PhpDocResolver.php
304 lines (248 loc) · 9.17 KB
/
PhpDocResolver.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php declare(strict_types = 1);
namespace ApiGen\Analyzer\NodeVisitors;
use ApiGen\Analyzer\IdentifierKind;
use ApiGen\Analyzer\NameContextFrame;
use ApiGen\Info\AliasReferenceInfo;
use ApiGen\Info\ClassLikeReferenceInfo;
use ApiGen\Info\Expr\ArrayExprInfo;
use ApiGen\Info\Expr\ArrayItemExprInfo;
use ApiGen\Info\Expr\BooleanExprInfo;
use ApiGen\Info\Expr\ClassConstantFetchExprInfo;
use ApiGen\Info\Expr\ConstantFetchExprInfo;
use ApiGen\Info\Expr\FloatExprInfo;
use ApiGen\Info\Expr\IntegerExprInfo;
use ApiGen\Info\Expr\NullExprInfo;
use ApiGen\Info\Expr\StringExprInfo;
use ApiGen\Info\ExprInfo;
use PhpParser\NameContext;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprArrayNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFalseNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFloatNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNullNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasImportTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ObjectShapeItemNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use function assert;
use function get_debug_type;
use function is_array;
use function is_object;
use function sprintf;
use function str_contains;
use function strtolower;
use function substr;
class PhpDocResolver extends NodeVisitorAbstract
{
protected const NATIVE_KEYWORDS = [
'array' => true,
'bool' => true,
'callable' => true,
'false' => true,
'float' => true,
'int' => true,
'iterable' => true,
'mixed' => true,
'never' => true,
'null' => true,
'object' => true,
'parent' => true,
'self' => true,
'static' => true,
'string' => true,
'true' => true,
'void' => true,
];
protected const KEYWORDS = self::NATIVE_KEYWORDS + [
'array-key' => true,
'associative-array' => true,
'boolean' => true,
'callable-object' => true,
'callable-string' => true,
'class-string' => true,
'double' => true,
'empty' => true,
'integer' => true,
'interface-string' => true,
'key-of' => true,
'list' => true,
'literal-string' => true,
'lowercase-string' => true,
'max' => true,
'min' => true,
'negative-int' => true,
'never-return' => true,
'never-returns' => true,
'no-return' => true,
'non-empty-array' => true,
'non-empty-list' => true,
'non-empty-lowercase-string' => true,
'non-empty-string' => true,
'non-falsy-string' => true,
'non-negative-int' => true,
'non-positive-int' => true,
'noreturn' => true,
'number' => true,
'numeric' => true,
'numeric-string' => true,
'positive-int' => true,
'resource' => true,
'scalar' => true,
'trait-string' => true,
'truthy-string' => true,
'value-of' => true,
];
protected NameContextFrame $nameContextFrame;
public function __construct(
protected Lexer $lexer,
protected PhpDocParser $parser,
protected NameContext $nameContext,
) {
$this->nameContextFrame = new NameContextFrame(parent: null);
}
public function enterNode(Node $node): null|int|Node
{
$doc = $node->getDocComment();
if ($doc !== null) {
$tokens = $this->lexer->tokenize($doc->getText());
$phpDoc = $this->parser->parse(new TokenIterator($tokens));
if ($node instanceof Node\Stmt\ClassLike) {
assert($node->namespacedName !== null);
$scope = new ClassLikeReferenceInfo($node->namespacedName->toString());
$this->nameContextFrame = $this->resolveNameContext($phpDoc, $this->nameContextFrame, $scope);
} elseif ($node instanceof Node\FunctionLike) {
$scope = $this->nameContextFrame->scope;
$this->nameContextFrame = $this->resolveNameContext($phpDoc, $this->nameContextFrame, $scope);
}
$this->resolvePhpDoc($phpDoc);
$node->setAttribute('phpDoc', $phpDoc);
} elseif ($node instanceof Node\Stmt\ClassLike || $node instanceof Node\FunctionLike) {
$this->nameContextFrame = new NameContextFrame($this->nameContextFrame);
}
return null;
}
public function leaveNode(Node $node): null|int|Node|array
{
if ($node instanceof Node\Stmt\ClassLike || $node instanceof Node\FunctionLike) {
if ($this->nameContextFrame->parent === null) {
throw new \LogicException('Name context stack is empty.');
} else {
$this->nameContextFrame = $this->nameContextFrame->parent;
}
}
return null;
}
protected function resolveNameContext(PhpDocNode $doc, NameContextFrame $parent, ?ClassLikeReferenceInfo $scope): NameContextFrame
{
$frame = new NameContextFrame($parent);
foreach ($doc->children as $child) {
if ($child instanceof PhpDocTagNode) {
if ($child->value instanceof TypeAliasTagValueNode) {
assert($scope !== null);
$lower = strtolower($child->value->alias);
$frame->aliases[$lower] = new AliasReferenceInfo($scope, $child->value->alias);
} elseif ($child->value instanceof TypeAliasImportTagValueNode) {
$classLike = new ClassLikeReferenceInfo($this->resolveClassLikeIdentifier($child->value->importedFrom->name));
$lower = strtolower($child->value->importedAs ?? $child->value->importedAlias);
$frame->aliases[$lower] = new AliasReferenceInfo($classLike, $child->value->importedAlias);
} elseif ($child->value instanceof TemplateTagValueNode) {
$lower = strtolower($child->value->name);
$frame->genericParameters[$lower] = true;
}
}
}
return $frame;
}
protected function resolvePhpDoc(PhpDocNode $phpDoc): void
{
$stack = [$phpDoc];
$index = 1;
while ($index > 0) {
$value = $stack[--$index];
if ($value instanceof IdentifierTypeNode) {
$this->resolveIdentifier($value);
} elseif ($value instanceof ConstExprNode) {
$value->setAttribute('info', $this->resolveConstExpr($value));
} elseif ($value instanceof ArrayShapeItemNode || $value instanceof ObjectShapeItemNode) {
$stack[$index++] = $value->valueType; // intentionally not pushing $value->keyName
} else {
foreach ((array) $value as $item) {
if (is_array($item) || is_object($item)) {
$stack[$index++] = $item;
}
}
}
}
}
protected function resolveIdentifier(IdentifierTypeNode $identifier): void
{
$lower = strtolower($identifier->name);
if (isset(self::KEYWORDS[$identifier->name]) || isset(self::NATIVE_KEYWORDS[$lower]) || str_contains($lower, '-')) {
$identifier->setAttribute('kind', IdentifierKind::Keyword);
} elseif (isset($this->nameContextFrame->genericParameters[$lower])) {
$identifier->setAttribute('kind', IdentifierKind::Generic);
} elseif (isset($this->nameContextFrame->aliases[$lower])) {
$identifier->setAttribute('kind', IdentifierKind::Alias);
$identifier->setAttribute('aliasReference', $this->nameContextFrame->aliases[$lower]);
} else {
$classLikeReference = new ClassLikeReferenceInfo($this->resolveClassLikeIdentifier($identifier->name));
$identifier->setAttribute('kind', IdentifierKind::ClassLike);
$identifier->setAttribute('classLikeReference', $classLikeReference);
}
}
protected function resolveClassLikeIdentifier(string $identifier): string
{
if ($identifier[0] === '\\') {
return substr($identifier, 1);
} else {
return $this->nameContext->getResolvedClassName(new Node\Name($identifier))->toString();
}
}
protected function resolveConstExpr(ConstExprNode $expr): ExprInfo
{
if ($expr instanceof ConstExprTrueNode) {
return new BooleanExprInfo(true);
} elseif ($expr instanceof ConstExprFalseNode) {
return new BooleanExprInfo(false);
} elseif ($expr instanceof ConstExprNullNode) {
return new NullExprInfo();
} elseif ($expr instanceof ConstExprIntegerNode) {
$node = Node\Scalar\LNumber::fromString($expr->value);
return new IntegerExprInfo($node->value, $node->getAttribute('kind'), $expr->value);
} elseif ($expr instanceof ConstExprFloatNode) {
return new FloatExprInfo(Node\Scalar\DNumber::parse($expr->value), $expr->value);
} elseif ($expr instanceof ConstExprStringNode) {
return new StringExprInfo($expr->value, raw: null);
} elseif ($expr instanceof ConstExprArrayNode) {
$items = [];
foreach ($expr->items as $item) {
$items[] = new ArrayItemExprInfo(
$item->key ? $this->resolveConstExpr($item->key) : null,
$this->resolveConstExpr($item->value),
);
}
return new ArrayExprInfo($items);
} elseif ($expr instanceof ConstFetchNode) {
if ($expr->className === '') {
return new ConstantFetchExprInfo($expr->name);
} else {
return new ClassConstantFetchExprInfo(new ClassLikeReferenceInfo($this->resolveClassLikeIdentifier($expr->className)), $expr->name);
}
} else {
throw new \LogicException(sprintf('Unsupported const expr node %s used in PHPDoc', get_debug_type($expr)));
}
}
}