1 | <?php |
||
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) |
|
76 | |||
77 | /** |
||
78 | * @param ClassMethod $methodDefintion |
||
79 | */ |
||
80 | 13 | public function addMethod(ClassMethod $methodDefintion) |
|
84 | |||
85 | /** |
||
86 | * @param Node\Stmt\Property $property |
||
87 | */ |
||
88 | 2 | public function addProperty(Node\Stmt\Property $property) |
|
92 | |||
93 | /** |
||
94 | * @param Node\Stmt\ClassConst $const |
||
95 | */ |
||
96 | public function addConst(Node\Stmt\ClassConst $const) |
||
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) |
||
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) |
|
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() |
|
233 | |||
234 | /** |
||
235 | * @param string $filepath |
||
236 | */ |
||
237 | 12 | public function setFilepath($filepath) |
|
241 | |||
242 | /** |
||
243 | * @return bool |
||
244 | */ |
||
245 | public function isAbstract() |
||
249 | |||
250 | /** |
||
251 | * @param null|string $extendsClass |
||
252 | */ |
||
253 | public function setExtendsClass($extendsClass) |
||
257 | |||
258 | /** |
||
259 | * @return null|ClassDefinition |
||
260 | */ |
||
261 | public function getExtendsClassDefinition() |
||
265 | |||
266 | /** |
||
267 | * @param ClassDefinition $extendsClassDefinition |
||
268 | */ |
||
269 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
||
273 | |||
274 | /** |
||
275 | * @param array $interface |
||
276 | */ |
||
277 | public function addInterface($interface) |
||
281 | |||
282 | /** |
||
283 | * @return null|string |
||
284 | */ |
||
285 | 12 | public function getExtendsClass() |
|
289 | } |
||
290 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.