1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PHPSA\Definition; |
7
|
|
|
|
8
|
|
|
use PHPSA\CompiledExpression; |
9
|
|
|
use PHPSA\Context; |
10
|
|
|
use PhpParser\Node; |
11
|
|
|
use PHPSA\Variable; |
12
|
|
|
use PHPSA\Compiler\Event; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ClassDefinition |
16
|
|
|
* @package PHPSA\Definition |
17
|
|
|
*/ |
18
|
|
|
class ClassDefinition extends ParentDefinition |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
protected $type; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class methods |
27
|
|
|
* |
28
|
|
|
* @var ClassMethod[] |
29
|
|
|
*/ |
30
|
|
|
protected $methods = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class properties |
34
|
|
|
* |
35
|
|
|
* @var Node\Stmt\Property[] |
36
|
|
|
*/ |
37
|
|
|
protected $properties = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Class constants |
41
|
|
|
* |
42
|
|
|
* @var Node\Stmt\Const_[] |
43
|
|
|
*/ |
44
|
|
|
protected $constants = array(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @todo Use Finder |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $filepath; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var Node\Stmt\Class_|null |
55
|
|
|
*/ |
56
|
|
|
protected $statement; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string|null |
60
|
|
|
*/ |
61
|
|
|
protected $extendsClass; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var ClassDefinition|null |
65
|
|
|
*/ |
66
|
|
|
protected $extendsClassDefinition; |
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
protected $interfaces = array(); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $name |
75
|
|
|
* @param Node\Stmt\Class_ $statement |
76
|
|
|
* @param integer $type |
77
|
|
|
*/ |
78
|
447 |
|
public function __construct($name, Node\Stmt\Class_ $statement = null, $type = 0) |
79
|
|
|
{ |
80
|
447 |
|
$this->name = $name; |
81
|
447 |
|
$this->statement = $statement; |
82
|
447 |
|
$this->type = $type; |
83
|
447 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param ClassMethod $methodDefintion |
87
|
|
|
*/ |
88
|
20 |
|
public function addMethod(ClassMethod $methodDefintion) |
89
|
|
|
{ |
90
|
20 |
|
$this->methods[$methodDefintion->getName()] = $methodDefintion; |
91
|
20 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Node\Stmt\Property $property |
95
|
|
|
*/ |
96
|
3 |
|
public function addProperty(Node\Stmt\Property $property) |
97
|
|
|
{ |
98
|
3 |
|
foreach ($property->props as $propertyDefinition) { |
99
|
3 |
|
$this->properties[$propertyDefinition->name] = $propertyDefinition; |
100
|
3 |
|
} |
101
|
3 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param Node\Stmt\ClassConst $const |
105
|
|
|
*/ |
106
|
2 |
|
public function addConst(Node\Stmt\ClassConst $const) |
107
|
|
|
{ |
108
|
2 |
|
$this->constants[$const->consts[0]->name] = $const; |
109
|
2 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param Context $context |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
19 |
|
public function compile(Context $context) |
116
|
|
|
{ |
117
|
19 |
|
if ($this->compiled) { |
118
|
|
|
return true; |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
19 |
|
$context->getEventManager()->fire( |
122
|
19 |
|
Event\StatementBeforeCompile::EVENT_NAME, |
123
|
19 |
|
new Event\StatementBeforeCompile( |
124
|
19 |
|
$this->statement, |
|
|
|
|
125
|
|
|
$context |
126
|
19 |
|
) |
127
|
19 |
|
); |
128
|
|
|
|
129
|
19 |
|
$this->compiled = true; |
130
|
19 |
|
$context->setFilepath($this->filepath); |
131
|
19 |
|
$context->setScope($this); |
132
|
|
|
|
133
|
19 |
|
foreach ($this->properties as $property) { |
134
|
2 |
|
if ($property->default == null) { |
|
|
|
|
135
|
1 |
|
continue; |
136
|
|
|
} |
137
|
|
|
// fire expression event for property default |
138
|
1 |
|
$context->getEventManager()->fire( |
139
|
1 |
|
Event\ExpressionBeforeCompile::EVENT_NAME, |
140
|
1 |
|
new Event\ExpressionBeforeCompile( |
141
|
1 |
|
$property->default, |
142
|
|
|
$context |
143
|
1 |
|
) |
144
|
1 |
|
); |
145
|
19 |
|
} |
146
|
|
|
|
147
|
19 |
|
foreach ($this->methods as $method) { |
148
|
19 |
|
$context->clearSymbols(); |
149
|
|
|
|
150
|
19 |
|
if (!$method->isStatic()) { |
151
|
19 |
|
$thisPtr = new Variable('this', $this, CompiledExpression::OBJECT); |
152
|
19 |
|
$thisPtr->incGets(); |
153
|
19 |
|
$context->addVariable($thisPtr); |
154
|
19 |
|
} |
155
|
|
|
|
156
|
19 |
|
$method->compile($context); |
157
|
|
|
|
158
|
19 |
|
$symbols = $context->getSymbols(); |
159
|
19 |
|
if (count($symbols) > 0) { |
160
|
19 |
|
foreach ($symbols as $name => $variable) { |
161
|
19 |
|
if ($variable->isUnused()) { |
162
|
2 |
|
$context->warning( |
163
|
2 |
|
'unused-' . $variable->getSymbolType(), |
164
|
2 |
|
sprintf( |
165
|
2 |
|
'Unused ' . $variable->getSymbolType() . ' $%s in method %s()', |
166
|
2 |
|
$variable->getName(), |
167
|
2 |
|
$method->getName() |
168
|
2 |
|
) |
169
|
2 |
|
); |
170
|
2 |
|
} |
171
|
19 |
|
} |
172
|
19 |
|
} |
173
|
19 |
|
} |
174
|
|
|
|
175
|
19 |
|
return $this; |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $name |
180
|
|
|
* @param boolean|false $inherit |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
1 |
|
public function hasMethod($name, $inherit = false) |
184
|
|
|
{ |
185
|
1 |
|
if (isset($this->methods[$name])) { |
186
|
1 |
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
1 |
|
if ($inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasMethod($name, $inherit)) { |
190
|
|
|
$method = $this->extendsClassDefinition->getMethod($name, $inherit); |
191
|
|
|
return $method && ($method->isPublic() || $method->isProtected()); |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
return false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $name |
199
|
|
|
* @param bool $inherit |
200
|
|
|
* @return bool |
201
|
|
|
*/ |
202
|
2 |
|
public function hasConst($name, $inherit = false) |
203
|
|
|
{ |
204
|
2 |
|
if ($inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasConst($name, $inherit)) { |
205
|
1 |
|
return true; |
206
|
|
|
} |
207
|
|
|
|
208
|
2 |
|
return isset($this->constants[$name]); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param $name |
213
|
|
|
* @param boolean|false $inherit |
214
|
|
|
* @return ClassMethod|null |
215
|
|
|
*/ |
216
|
1 |
|
public function getMethod($name, $inherit = false) |
217
|
|
|
{ |
218
|
1 |
|
if (isset($this->methods[$name])) { |
219
|
1 |
|
return $this->methods[$name]; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if ($inherit && $this->extendsClassDefinition) { |
223
|
|
|
return $this->extendsClassDefinition->getMethod($name, $inherit); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return null; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param $name |
231
|
|
|
* @param bool $inherit |
232
|
|
|
* @return bool |
233
|
|
|
*/ |
234
|
1 |
|
public function hasProperty($name, $inherit = false) |
235
|
|
|
{ |
236
|
1 |
|
if (isset($this->properties[$name])) { |
237
|
1 |
|
return isset($this->properties[$name]); |
238
|
|
|
} |
239
|
|
|
|
240
|
1 |
|
return $inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasProperty($name, true); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param string $name |
245
|
|
|
* @param bool $inherit |
246
|
|
|
* @return Node\Stmt\Property |
247
|
|
|
*/ |
248
|
|
|
public function getProperty($name, $inherit = false) |
249
|
|
|
{ |
250
|
|
|
assert($this->hasProperty($name, $inherit)); |
251
|
|
|
|
252
|
|
|
if (isset($this->properties[$name])) { |
253
|
|
|
return $this->properties[$name]; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
if ($inherit && $this->extendsClassDefinition) { |
257
|
|
|
return $this->extendsClassDefinition->getProperty($name, true); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return null; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
19 |
|
public function getFilepath() |
267
|
|
|
{ |
268
|
19 |
|
return $this->filepath; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param string $filepath |
273
|
|
|
*/ |
274
|
19 |
|
public function setFilepath($filepath) |
275
|
|
|
{ |
276
|
19 |
|
$this->filepath = $filepath; |
277
|
19 |
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return bool |
281
|
|
|
*/ |
282
|
|
|
public function isAbstract() |
283
|
|
|
{ |
284
|
|
|
return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_ABSTRACT); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param null|string $extendsClass |
289
|
|
|
*/ |
290
|
|
|
public function setExtendsClass($extendsClass) |
291
|
|
|
{ |
292
|
|
|
$this->extendsClass = $extendsClass; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @return null|ClassDefinition |
297
|
|
|
*/ |
298
|
|
|
public function getExtendsClassDefinition() |
299
|
|
|
{ |
300
|
|
|
return $this->extendsClassDefinition; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param ClassDefinition $extendsClassDefinition |
305
|
|
|
*/ |
306
|
1 |
|
public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
|
|
|
307
|
|
|
{ |
308
|
1 |
|
$this->extendsClassDefinition = $extendsClassDefinition; |
309
|
1 |
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param array $interface |
313
|
|
|
*/ |
314
|
|
|
public function addInterface($interface) |
315
|
|
|
{ |
316
|
|
|
$this->interfaces[] = $interface; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @return null|string |
321
|
|
|
*/ |
322
|
19 |
|
public function getExtendsClass() |
323
|
|
|
{ |
324
|
19 |
|
return $this->extendsClass; |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.