-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeVisitor.php
59 lines (46 loc) · 1.65 KB
/
NodeVisitor.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
<?php
declare(strict_types=1);
namespace Bigwhoop\PhpClassComponentsExtractor;
use Bigwhoop\PhpClassComponentsExtractor\Graph\Graph;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
final class NodeVisitor extends NodeVisitorAbstract
{
private const METHODS_TO_IGNORE = ['__construct', '__destruct', '__clone'];
private Graph $graph;
private string $lastMethod = '';
public function __construct()
{
$this->reset();
}
public function reset(): void
{
$this->graph = new Graph();
}
public function enterNode(Node $node): null
{
if ($node instanceof Node\Stmt\PropertyProperty) {
$this->graph->addProperty($node->name->toString());
}
if ($node instanceof Node\Stmt\ClassMethod && !in_array($node->name->toString(), self::METHODS_TO_IGNORE, true)) {
$this->graph->addMethod($node->name->toString());
$this->lastMethod = $node->name->toString();
}
if ($this->lastMethod === '') {
return null;
}
if ($node instanceof Node\Expr\PropertyFetch && $node->var instanceof Node\Expr\Variable && $node->var->name === 'this') {
// @phpstan-ignore-next-line
$this->graph->addPropertyFetch($this->lastMethod, $node->name->name);
}
if ($node instanceof Node\Expr\MethodCall && $node->var instanceof Node\Expr\Variable && $node->var->name === 'this') {
// @phpstan-ignore-next-line
$this->graph->addMethodCall($this->lastMethod, $node->name->name);
}
return null;
}
public function getGraph(): Graph
{
return $this->graph;
}
}