-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Extractor.php
478 lines (395 loc) · 14.5 KB
/
Extractor.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\PhpGenerator;
use Nette;
use PhpParser;
use PhpParser\Node;
use PhpParser\NodeFinder;
use PhpParser\ParserFactory;
/**
* Extracts information from PHP code.
* @internal
*/
final class Extractor
{
private string $code;
/** @var Node[] */
private array $statements;
private PhpParser\PrettyPrinterAbstract $printer;
public function __construct(string $code)
{
if (!class_exists(ParserFactory::class)) {
throw new Nette\NotSupportedException("PHP-Parser is required to load method bodies, install package 'nikic/php-parser' 4.7 or newer.");
}
$this->printer = new PhpParser\PrettyPrinter\Standard;
$this->parseCode($code);
}
private function parseCode(string $code): void
{
if (!str_starts_with($code, '<?php')) {
throw new Nette\InvalidStateException('The input string is not a PHP code.');
}
$this->code = Nette\Utils\Strings::normalizeNewlines($code);
$lexer = new PhpParser\Lexer\Emulative(['usedAttributes' => ['startFilePos', 'endFilePos', 'comments']]);
$parser = (new ParserFactory)->create(ParserFactory::ONLY_PHP7, $lexer);
$stmts = $parser->parse($this->code);
$traverser = new PhpParser\NodeTraverser;
$traverser->addVisitor(new PhpParser\NodeVisitor\ParentConnectingVisitor);
$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver(null, ['preserveOriginalNames' => true]));
$this->statements = $traverser->traverse($stmts);
}
/** @return array<string, string> */
public function extractMethodBodies(string $className): array
{
$nodeFinder = new NodeFinder;
$classNode = $nodeFinder->findFirst(
$this->statements,
fn(Node $node) => $node instanceof Node\Stmt\ClassLike && $node->namespacedName->toString() === $className,
);
$res = [];
foreach ($nodeFinder->findInstanceOf($classNode, Node\Stmt\ClassMethod::class) as $methodNode) {
assert($methodNode instanceof Node\Stmt\ClassMethod);
if ($methodNode->stmts) {
$res[$methodNode->name->toString()] = $this->getReformattedContents($methodNode->stmts, 2);
}
}
return $res;
}
public function extractFunctionBody(string $name): ?string
{
$functionNode = (new NodeFinder)->findFirst(
$this->statements,
fn(Node $node) => $node instanceof Node\Stmt\Function_ && $node->namespacedName->toString() === $name,
);
assert($functionNode instanceof Node\Stmt\Function_);
return $this->getReformattedContents($functionNode->stmts, 1);
}
/** @param Node[] $nodes */
private function getReformattedContents(array $nodes, int $level): string
{
$body = $this->getNodeContents(...$nodes);
$body = $this->performReplacements($body, $this->prepareReplacements($nodes));
return Helpers::unindent($body, $level);
}
/**
* @param Node[] $nodes
* @return array<array{int, int, string}>
*/
private function prepareReplacements(array $nodes): array
{
$start = $this->getNodeStartPos($nodes[0]);
$replacements = [];
(new NodeFinder)->find($nodes, function (Node $node) use (&$replacements, $start) {
if ($node instanceof Node\Name\FullyQualified) {
if ($node->getAttribute('originalName') instanceof Node\Name) {
$of = match (true) {
$node->getAttribute('parent') instanceof Node\Expr\ConstFetch => PhpNamespace::NameConstant,
$node->getAttribute('parent') instanceof Node\Expr\FuncCall => PhpNamespace::NameFunction,
default => PhpNamespace::NameNormal,
};
$replacements[] = [
$node->getStartFilePos() - $start,
$node->getEndFilePos() - $start,
Helpers::tagName($node->toCodeString(), $of),
];
}
} elseif ($node instanceof Node\Scalar\String_ || $node instanceof Node\Scalar\EncapsedStringPart) {
// multi-line strings => singleline
$token = $this->getNodeContents($node);
if (str_contains($token, "\n")) {
$quote = $node instanceof Node\Scalar\String_ ? '"' : '';
$replacements[] = [
$node->getStartFilePos() - $start,
$node->getEndFilePos() - $start,
$quote . addcslashes($node->value, "\x00..\x1F") . $quote,
];
}
} elseif ($node instanceof Node\Scalar\Encapsed) {
// HEREDOC => "string"
if ($node->getAttribute('kind') === Node\Scalar\String_::KIND_HEREDOC) {
$replacements[] = [
$node->getStartFilePos() - $start,
$node->parts[0]->getStartFilePos() - $start - 1,
'"',
];
$replacements[] = [
end($node->parts)->getEndFilePos() - $start + 1,
$node->getEndFilePos() - $start,
'"',
];
}
}
});
return $replacements;
}
/** @param array<array{int, int, string}> $replacements */
private function performReplacements(string $s, array $replacements): string
{
usort($replacements, fn($a, $b) => $b[0] <=> $a[0]);
foreach ($replacements as [$start, $end, $replacement]) {
$s = substr_replace($s, $replacement, $start, $end - $start + 1);
}
return $s;
}
public function extractAll(): PhpFile
{
$phpFile = new PhpFile;
if (
$this->statements
&& !$this->statements[0] instanceof Node\Stmt\ClassLike
&& !$this->statements[0] instanceof Node\Stmt\Function_
) {
$this->addCommentAndAttributes($phpFile, $this->statements[0]);
}
$namespaces = ['' => $this->statements];
foreach ($this->statements as $node) {
if ($node instanceof Node\Stmt\Declare_
&& $node->declares[0] instanceof Node\Stmt\DeclareDeclare
&& $node->declares[0]->key->name === 'strict_types'
&& $node->declares[0]->value instanceof Node\Scalar\LNumber
) {
$phpFile->setStrictTypes((bool) $node->declares[0]->value->value);
} elseif ($node instanceof Node\Stmt\Namespace_) {
$namespaces[$node->name->toString()] = $node->stmts;
}
}
foreach ($namespaces as $name => $nodes) {
foreach ($nodes as $node) {
match (true) {
$node instanceof Node\Stmt\Use_ => $this->addUseToNamespace($phpFile->addNamespace($name), $node),
$node instanceof Node\Stmt\ClassLike => $this->addClassLikeToFile($phpFile, $node),
$node instanceof Node\Stmt\Function_ => $this->addFunctionToFile($phpFile, $node),
default => null,
};
}
}
return $phpFile;
}
private function addUseToNamespace(PhpNamespace $namespace, Node\Stmt\Use_ $node): void
{
$of = [
$node::TYPE_NORMAL => PhpNamespace::NameNormal,
$node::TYPE_FUNCTION => PhpNamespace::NameFunction,
$node::TYPE_CONSTANT => PhpNamespace::NameConstant,
][$node->type];
foreach ($node->uses as $use) {
$namespace->addUse($use->name->toString(), $use->alias?->toString(), $of);
}
}
private function addClassLikeToFile(PhpFile $phpFile, Node\Stmt\ClassLike $node): ClassLike
{
if ($node instanceof Node\Stmt\Class_) {
$class = $phpFile->addClass($node->namespacedName->toString());
$class->setFinal($node->isFinal());
$class->setAbstract($node->isAbstract());
$class->setReadOnly(method_exists($node, 'isReadonly') && $node->isReadonly());
if ($node->extends) {
$class->setExtends($node->extends->toString());
}
foreach ($node->implements as $item) {
$class->addImplement($item->toString());
}
} elseif ($node instanceof Node\Stmt\Interface_) {
$class = $phpFile->addInterface($node->namespacedName->toString());
foreach ($node->extends as $item) {
$class->addExtend($item->toString());
}
} elseif ($node instanceof Node\Stmt\Trait_) {
$class = $phpFile->addTrait($node->namespacedName->toString());
} elseif ($node instanceof Node\Stmt\Enum_) {
$class = $phpFile->addEnum($node->namespacedName->toString());
$class->setType($node->scalarType?->toString());
foreach ($node->implements as $item) {
$class->addImplement($item->toString());
}
}
$this->addCommentAndAttributes($class, $node);
$this->addClassMembers($class, $node);
return $class;
}
private function addClassMembers(ClassLike $class, Node\Stmt\ClassLike $node): void
{
foreach ($node->stmts as $stmt) {
match (true) {
$stmt instanceof Node\Stmt\TraitUse => $this->addTraitToClass($class, $stmt),
$stmt instanceof Node\Stmt\Property => $this->addPropertyToClass($class, $stmt),
$stmt instanceof Node\Stmt\ClassMethod => $this->addMethodToClass($class, $stmt),
$stmt instanceof Node\Stmt\ClassConst => $this->addConstantToClass($class, $stmt),
$stmt instanceof Node\Stmt\EnumCase => $this->addEnumCaseToClass($class, $stmt),
default => null,
};
}
}
private function addTraitToClass(ClassLike $class, Node\Stmt\TraitUse $node): void
{
foreach ($node->traits as $item) {
$trait = $class->addTrait($item->toString());
}
foreach ($node->adaptations as $item) {
$trait->addResolution(rtrim($this->getReformattedContents([$item], 0), ';'));
}
$this->addCommentAndAttributes($trait, $node);
}
private function addPropertyToClass(ClassLike $class, Node\Stmt\Property $node): void
{
foreach ($node->props as $item) {
$prop = $class->addProperty($item->name->toString());
$prop->setStatic($node->isStatic());
$prop->setVisibility($this->toVisibility($node->flags));
$prop->setType($node->type ? $this->toPhp($node->type) : null);
if ($item->default) {
$prop->setValue($this->toValue($item->default));
}
$prop->setReadOnly(method_exists($node, 'isReadonly') && $node->isReadonly());
$this->addCommentAndAttributes($prop, $node);
}
}
private function addMethodToClass(ClassLike $class, Node\Stmt\ClassMethod $node): void
{
$method = $class->addMethod($node->name->toString());
$method->setAbstract($node->isAbstract());
$method->setFinal($node->isFinal());
$method->setStatic($node->isStatic());
$method->setVisibility($this->toVisibility($node->flags));
$this->setupFunction($method, $node);
}
private function addConstantToClass(ClassLike $class, Node\Stmt\ClassConst $node): void
{
foreach ($node->consts as $item) {
$const = $class->addConstant($item->name->toString(), $this->toValue($item->value));
$const->setVisibility($this->toVisibility($node->flags));
$const->setFinal(method_exists($node, 'isFinal') && $node->isFinal());
$this->addCommentAndAttributes($const, $node);
}
}
private function addEnumCaseToClass(EnumType $class, Node\Stmt\EnumCase $node): void
{
$value = match (true) {
$node->expr === null => null,
$node->expr instanceof Node\Scalar\LNumber, $node->expr instanceof Node\Scalar\String_ => $node->expr->value,
default => $this->toValue($node->expr),
};
$case = $class->addCase($node->name->toString(), $value);
$this->addCommentAndAttributes($case, $node);
}
private function addFunctionToFile(PhpFile $phpFile, Node\Stmt\Function_ $node): void
{
$function = $phpFile->addFunction($node->namespacedName->toString());
$this->setupFunction($function, $node);
}
private function addCommentAndAttributes(
PhpFile|ClassLike|Constant|Property|GlobalFunction|Method|Parameter|EnumCase|TraitUse $element,
Node $node,
): void
{
if ($node->getDocComment()) {
$comment = $node->getDocComment()->getReformattedText();
$comment = Helpers::unformatDocComment($comment);
$element->setComment($comment);
$node->setDocComment(new PhpParser\Comment\Doc(''));
}
foreach ($node->attrGroups ?? [] as $group) {
foreach ($group->attrs as $attribute) {
$args = [];
foreach ($attribute->args as $arg) {
if ($arg->name) {
$args[$arg->name->toString()] = $this->toValue($arg->value);
} else {
$args[] = $this->toValue($arg->value);
}
}
$element->addAttribute($attribute->name->toString(), $args);
}
}
}
private function setupFunction(GlobalFunction|Method $function, Node\FunctionLike $node): void
{
$function->setReturnReference($node->returnsByRef());
$function->setReturnType($node->getReturnType() ? $this->toPhp($node->getReturnType()) : null);
foreach ($node->getParams() as $item) {
$visibility = $this->toVisibility($item->flags);
$isReadonly = (bool) ($item->flags & Node\Stmt\Class_::MODIFIER_READONLY);
$param = $visibility
? ($function->addPromotedParameter($item->var->name))->setVisibility($visibility)->setReadonly($isReadonly)
: $function->addParameter($item->var->name);
$param->setType($item->type ? $this->toPhp($item->type) : null);
$param->setReference($item->byRef);
$function->setVariadic($item->variadic);
if ($item->default) {
$param->setDefaultValue($this->toValue($item->default));
}
$this->addCommentAndAttributes($param, $item);
}
$this->addCommentAndAttributes($function, $node);
if ($node->getStmts()) {
$indent = $function instanceof GlobalFunction ? 1 : 2;
$function->setBody($this->getReformattedContents($node->getStmts(), $indent));
}
}
private function toValue(Node\Expr $node): mixed
{
if ($node instanceof Node\Expr\ConstFetch) {
return match ($node->name->toLowerString()) {
'null' => null,
'true' => true,
'false' => false,
default => new Literal($this->getReformattedContents([$node], 0)),
};
} elseif ($node instanceof Node\Scalar\LNumber
|| $node instanceof Node\Scalar\DNumber
|| $node instanceof Node\Scalar\String_
) {
return $node->value;
} elseif ($node instanceof Node\Expr\Array_) {
$res = [];
foreach ($node->items as $item) {
if ($item->unpack) {
return new Literal($this->getReformattedContents([$node], 0));
} elseif ($item->key) {
$key = $item->key instanceof Node\Identifier
? $item->key->name
: $this->toValue($item->key);
if ($key instanceof Literal) {
return new Literal($this->getReformattedContents([$node], 0));
}
$res[$key] = $this->toValue($item->value);
} else {
$res[] = $this->toValue($item->value);
}
}
return $res;
} else {
return new Literal($this->getReformattedContents([$node], 0));
}
}
private function toVisibility(int $flags): ?string
{
return match (true) {
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PUBLIC) => ClassType::VisibilityPublic,
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) => ClassType::VisibilityProtected,
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) => ClassType::VisibilityPrivate,
default => null,
};
}
private function toPhp(Node $value): string
{
$dolly = clone $value;
$dolly->setAttribute('comments', []);
return $this->printer->prettyPrint([$dolly]);
}
private function getNodeContents(Node ...$nodes): string
{
$start = $this->getNodeStartPos($nodes[0]);
return substr($this->code, $start, end($nodes)->getEndFilePos() - $start + 1);
}
private function getNodeStartPos(Node $node): int
{
return ($comments = $node->getComments())
? $comments[0]->getStartFilePos()
: $node->getStartFilePos();
}
}