1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Bigwhoop\Trumpet\CodeParsing\Php; |
4
|
|
|
|
5
|
|
|
use PhpParser\Node; |
6
|
|
|
use PhpParser\NodeVisitorAbstract; |
7
|
|
|
use PhpParser\PrettyPrinterAbstract as Printer; |
8
|
|
|
|
9
|
|
|
final class NodeVisitor extends NodeVisitorAbstract |
10
|
|
|
{ |
11
|
|
|
/** @var ParserResult */ |
12
|
|
|
private $result; |
13
|
|
|
|
14
|
|
|
/** @var PhpClass|null */ |
15
|
|
|
private $currentClass; |
16
|
|
|
|
17
|
|
|
/** @var Printer */ |
18
|
|
|
private $printer; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Printer $printer |
22
|
|
|
* @param ParserResult $result |
23
|
|
|
*/ |
24
|
11 |
|
public function __construct(Printer $printer, ParserResult $result = null) |
25
|
|
|
{ |
26
|
11 |
|
$this->printer = $printer; |
27
|
11 |
|
$this->result = $result ?: new ParserResult(); |
28
|
11 |
|
} |
29
|
|
|
|
30
|
11 |
|
public function getResult(): ParserResult |
31
|
|
|
{ |
32
|
11 |
|
return $this->result; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
11 |
|
public function enterNode(Node $node) |
39
|
|
|
{ |
40
|
11 |
|
if ($node instanceof Node\Stmt\Class_) { |
41
|
10 |
|
$fullyQualifiedName = $node->namespacedName->toString(); |
|
|
|
|
42
|
10 |
|
$this->currentClass = new PhpClass($fullyQualifiedName, [], $this->getSource($node)); |
43
|
|
|
} elseif ($node instanceof Node\Stmt\ClassMethod) { |
44
|
8 |
|
$this->currentClass->addMethod(new PhpMethod($node->name, $node->isStatic(), $this->getSource($node))); |
45
|
|
|
} elseif ($node instanceof Node\Stmt\Function_) { |
46
|
8 |
|
$fullyQualifiedName = $node->namespacedName->toString(); |
|
|
|
|
47
|
8 |
|
$this->result->addFunction(new PhpFunction($fullyQualifiedName, $this->getSource($node))); |
48
|
|
|
} |
49
|
11 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
11 |
|
public function leaveNode(Node $node) |
55
|
|
|
{ |
56
|
11 |
|
if ($node instanceof Node\Stmt\Class_) { |
57
|
10 |
|
$this->result->addClass($this->currentClass); |
|
|
|
|
58
|
|
|
} |
59
|
11 |
|
} |
60
|
|
|
|
61
|
11 |
|
private function getSource(Node $node): string |
62
|
|
|
{ |
63
|
11 |
|
return $this->printer->prettyPrint([$node]); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.