-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCollector.php
More file actions
68 lines (56 loc) · 1.76 KB
/
Collector.php
File metadata and controls
68 lines (56 loc) · 1.76 KB
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
<?php
/**
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License
*/
declare(strict_types=1);
namespace OpenCodeModeling\CodeAst\NodeVisitor;
use PhpParser\Node;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
final class Collector extends NodeVisitorAbstract
{
/**
* @var array
*/
private $visitors = [];
public function afterTraverse(array $nodes): ?array
{
$this->visitors = [];
foreach ($nodes as $node) {
$this->determineVisitor($node);
}
return null;
}
private function determineVisitor(Node $node): void
{
switch (true) {
case $node instanceof Namespace_:
$this->visitors[] = ClassNamespace::fromNode($node);
break;
case $node instanceof Node\Stmt\Class_:
$this->visitors[] = ClassFile::fromNode($node);
foreach ($node->stmts as $stmt) {
$this->determineVisitor($stmt);
}
break;
case $node instanceof Node\Stmt\ClassConst:
$this->visitors[] = ClassConstant::fromNode($node);
break;
default:
break;
}
}
public function visitors(): array
{
return $this->visitors;
}
public function injectVisitors(NodeTraverser $nodeTraverser): void
{
foreach ($this->visitors as $visitor) {
$nodeTraverser->addVisitor(clone $visitor);
}
}
}