|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Solidifier\Parser\Visitors; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Node; |
|
6
|
|
|
use PhpParser\Node\Stmt\Class_; |
|
7
|
|
|
use PhpParser\Node\Stmt\Interface_; |
|
8
|
|
|
use PhpParser\Node\Stmt\Trait_; |
|
9
|
|
|
use PhpParser\Node\Stmt\Namespace_; |
|
10
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
|
11
|
|
|
use Solidifier\Visitors\ObjectType; |
|
12
|
|
|
|
|
13
|
|
|
abstract class ContextualVisitor extends AbstractVisitor |
|
14
|
|
|
{ |
|
15
|
|
|
protected |
|
16
|
|
|
$nodeStack, |
|
|
|
|
|
|
17
|
|
|
$currentNamespace, |
|
18
|
|
|
$currentObjectType, |
|
19
|
|
|
$currentMethod; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
|
|
parent::__construct(); |
|
24
|
|
|
|
|
25
|
|
|
$this->nodeStack = new \SplStack(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
final public function beforeTraverse(array $nodes) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->currentNamespace = null; |
|
|
|
|
|
|
31
|
|
|
$this->currentObjectType = null; |
|
32
|
|
|
$this->currentMethod = null; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
$this->before($nodes); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
final public function enterNode(Node $node) |
|
38
|
|
|
{ |
|
39
|
|
|
if($node instanceof Namespace_) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->currentNamespace = $node->name; |
|
42
|
|
|
} |
|
43
|
|
|
elseif($node instanceof Class_) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->currentObjectType = new ObjectType($this->currentNamespace, $node->name); |
|
46
|
|
|
} |
|
47
|
|
|
elseif($node instanceof Interface_) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->currentObjectType = new ObjectType($this->currentNamespace, $node->name, ObjectType::TYPE_INTERFACE); |
|
50
|
|
|
} |
|
51
|
|
|
elseif($node instanceof Trait_) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->currentObjectType = new ObjectType($this->currentNamespace, $node->name, ObjectType::TYPE_TRAIT); |
|
54
|
|
|
} |
|
55
|
|
|
elseif($node instanceof ClassMethod) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->currentMethod = $node; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->enter($node); |
|
61
|
|
|
$this->nodeStack->push($node); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
final public function leaveNode(Node $node) |
|
65
|
|
|
{ |
|
66
|
|
|
if($node instanceof Namespace_) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->currentNamespace = null; |
|
69
|
|
|
} |
|
70
|
|
|
elseif($node instanceof Class_ || $node instanceof Interface_ || $node instanceof Trait_) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->currentObjectType = null; |
|
73
|
|
|
} |
|
74
|
|
|
elseif($node instanceof ClassMethod) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->currentMethod = null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->leave($node); |
|
80
|
|
|
$this->nodeStack->pop(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
final public function afterTraverse(array $nodes) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->after($nodes); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function before(array $nodes) |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
protected function enter(Node $node) |
|
94
|
|
|
{ |
|
95
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function leave(Node $node) |
|
99
|
|
|
{ |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function after(array $nodes) |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
} |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.