1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PHPSA\Compiler; |
7
|
|
|
|
8
|
|
|
use PhpParser\Node; |
9
|
|
|
use PhpParser\NodeVisitorAbstract; |
10
|
|
|
use PhpParser\Node\Stmt; |
11
|
|
|
use PHPSA\Compiler; |
12
|
|
|
use PHPSA\Definition\ClassDefinition; |
13
|
|
|
use PHPSA\Definition\ClassMethod; |
14
|
|
|
use PHPSA\Definition\FunctionDefinition; |
15
|
|
|
use PHPSA\Definition\TraitDefinition; |
16
|
|
|
|
17
|
|
|
class DefinitionVisitor extends NodeVisitorAbstract |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Compiler |
21
|
|
|
*/ |
22
|
|
|
protected $compiler; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string|null |
26
|
|
|
*/ |
27
|
|
|
protected $filepath; |
28
|
|
|
|
29
|
1 |
|
public function __construct(Compiler $compiler) |
30
|
|
|
{ |
31
|
1 |
|
$this->compiler = $compiler; |
32
|
1 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Node $node |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
1 |
|
public function enterNode(Node $node) |
39
|
|
|
{ |
40
|
1 |
|
if ($node instanceof Stmt\Class_) { |
41
|
1 |
|
$this->prepareClass($node); |
42
|
1 |
|
} elseif ($node instanceof Stmt\Function_) { |
43
|
|
|
$this->prepareFunction($node); |
44
|
1 |
|
} elseif ($node instanceof Stmt\Trait_) { |
45
|
|
|
$this->prepareTrait($node); |
46
|
|
|
} |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Stmt\Trait_ $statement |
51
|
|
|
*/ |
52
|
|
|
public function prepareTrait(Stmt\Trait_ $statement) |
53
|
|
|
{ |
54
|
|
|
$definition = new TraitDefinition($statement->name, $statement); |
55
|
|
|
$definition->setFilepath($this->filepath); |
56
|
|
|
|
57
|
|
|
if (isset($statement->namespace)) { |
58
|
|
|
/** @var \PhpParser\Node\Name $namespace */ |
59
|
|
|
$namespace = $statement->namespace; |
|
|
|
|
60
|
|
|
$definition->setNamespace($namespace->toString()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$definition->precompile(); |
64
|
|
|
$this->compiler->addTrait($definition); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Stmt\Function_ $statement |
69
|
|
|
*/ |
70
|
|
|
public function prepareFunction(Stmt\Function_ $statement) |
71
|
|
|
{ |
72
|
|
|
$definition = new FunctionDefinition($statement->name, $statement); |
73
|
|
|
$definition->setFilepath($this->filepath); |
74
|
|
|
|
75
|
|
|
if (isset($statement->namespace)) { |
76
|
|
|
/** @var \PhpParser\Node\Name $namespace */ |
77
|
|
|
$namespace = $statement->namespace; |
|
|
|
|
78
|
|
|
$definition->setNamespace($namespace->toString()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->compiler->addFunction($definition); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Stmt\Class_ $statement |
86
|
|
|
*/ |
87
|
1 |
|
public function prepareClass(Stmt\Class_ $statement) |
88
|
|
|
{ |
89
|
1 |
|
$definition = new ClassDefinition($statement->name, $statement, $statement->flags); |
90
|
1 |
|
$definition->setFilepath($this->filepath); |
91
|
|
|
|
92
|
1 |
|
if (isset($statement->namespace)) { |
93
|
|
|
/** @var \PhpParser\Node\Name $namespace */ |
94
|
1 |
|
$namespace = $statement->namespace; |
|
|
|
|
95
|
1 |
|
$definition->setNamespace($namespace->toString()); |
96
|
1 |
|
} |
97
|
|
|
|
98
|
1 |
|
if ($statement->extends) { |
99
|
|
|
$definition->setExtendsClass($statement->extends->toString()); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
if ($statement->implements) { |
|
|
|
|
103
|
|
|
foreach ($statement->implements as $interface) { |
104
|
|
|
$definition->addInterface($interface->toString()); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
foreach ($statement->stmts as $stmt) { |
109
|
1 |
|
if ($stmt instanceof Node\Stmt\ClassMethod) { |
110
|
1 |
|
$definition->addMethod( |
111
|
1 |
|
new ClassMethod($stmt->name, $stmt, $stmt->flags) |
112
|
1 |
|
); |
113
|
1 |
|
} elseif ($stmt instanceof Node\Stmt\Property) { |
114
|
|
|
$definition->addProperty($stmt); |
115
|
|
|
} elseif ($stmt instanceof Node\Stmt\TraitUse) { |
116
|
|
|
foreach ($stmt->traits as $traitPart) { |
117
|
|
|
$traitDefinition = $this->compiler->getTrait($traitPart->toString()); |
|
|
|
|
118
|
|
|
if (!$traitDefinition && $definition->getNamespace()) { |
119
|
|
|
$traitDefinition = $this->compiler->getTrait($definition->getNamespace() . '\\' . $traitPart); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($traitDefinition) { |
123
|
|
|
$definition->mergeTrait($traitDefinition, $stmt->adaptations); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} elseif ($stmt instanceof Node\Stmt\ClassConst) { |
127
|
|
|
$definition->addConst($stmt); |
128
|
|
|
} |
129
|
1 |
|
} |
130
|
|
|
|
131
|
1 |
|
$this->compiler->addClass($definition); |
132
|
1 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $filepath |
136
|
|
|
*/ |
137
|
1 |
|
public function setFilePath($filepath) |
138
|
|
|
{ |
139
|
1 |
|
$this->filepath = $filepath; |
140
|
1 |
|
} |
141
|
|
|
} |
142
|
|
|
|
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.