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