Conditions | 29 |
Paths | 29 |
Total Lines | 257 |
Code Lines | 193 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
126 | public function onFinishProduction(QueryAstNode $node): void |
||
127 | { |
||
128 | if ($this->hasReference($node)) { |
||
129 | return; |
||
130 | } |
||
131 | switch ($node->getName()) { |
||
132 | case QueryAstNodeType::GET_INPUT: |
||
133 | $this->addMethodCall( |
||
134 | $node, |
||
135 | 'getInput', |
||
136 | $this->input |
||
137 | ); |
||
138 | break; |
||
139 | |||
140 | case QueryAstNodeType::SET_OUTPUT: |
||
141 | $this->properties = new QueryProperties( |
||
142 | $node->getAttribute('is_definite'), |
||
143 | $node->getAttribute('is_path') |
||
144 | ); |
||
145 | $this->stmts[] = new Return_($this->getReference($node->getChild(0))); |
||
146 | break; |
||
147 | |||
148 | case QueryAstNodeType::CREATE_FILTER_CONTEXT: |
||
149 | $this->addMethodCall( |
||
150 | $node, |
||
151 | 'createFilterContext', |
||
152 | $this->getReference($node->getChild(0)) |
||
153 | ); |
||
154 | break; |
||
155 | |||
156 | case QueryAstNodeType::SPLIT: |
||
157 | $this->addMethodCall( |
||
158 | $node, |
||
159 | 'split', |
||
160 | $this->getReference($node->getChild(0)) |
||
161 | ); |
||
162 | break; |
||
163 | |||
164 | case QueryAstNodeType::EVALUATE: |
||
165 | $this->addMethodCall( |
||
166 | $node, |
||
167 | 'evaluate', |
||
168 | $this->getReference($node->getChild(0)), |
||
169 | $this->getReference($node->getChild(1)) |
||
170 | ); |
||
171 | break; |
||
172 | |||
173 | case QueryAstNodeType::FILTER: |
||
174 | $this->addMethodCall( |
||
175 | $node, |
||
176 | 'filter', |
||
177 | $this->getReference($node->getChild(0)), |
||
178 | $this->getReference($node->getChild(1)) |
||
179 | ); |
||
180 | break; |
||
181 | |||
182 | case QueryAstNodeType::EVALUATE_LOGICAL_OR: |
||
183 | $this->addMethodCall( |
||
184 | $node, |
||
185 | 'evaluateLogicalOr', |
||
186 | $this->getReference($node->getChild(0)), |
||
187 | $this->getReference($node->getChild(1)) |
||
188 | ); |
||
189 | break; |
||
190 | |||
191 | case QueryAstNodeType::EVALUATE_LOGICAL_AND: |
||
192 | $this->addMethodCall( |
||
193 | $node, |
||
194 | 'evaluateLogicalAnd', |
||
195 | $this->getReference($node->getChild(0)), |
||
196 | $this->getReference($node->getChild(1)) |
||
197 | ); |
||
198 | break; |
||
199 | |||
200 | case QueryAstNodeType::EVALUATE_LOGICAL_NOT: |
||
201 | $this->addMethodCall( |
||
202 | $node, |
||
203 | 'evaluateLogicalNot', |
||
204 | $this->getReference($node->getChild(0)) |
||
205 | ); |
||
206 | break; |
||
207 | |||
208 | case QueryAstNodeType::CALCULATE_IS_EQUAL: |
||
209 | $this->addMethodCall( |
||
210 | $node, |
||
211 | 'calculateIsEqual', |
||
212 | $this->getReference($node->getChild(0)), |
||
213 | $this->getReference($node->getChild(1)) |
||
214 | ); |
||
215 | break; |
||
216 | |||
217 | case QueryAstNodeType::CALCULATE_IS_GREATER: |
||
218 | $this->addMethodCall( |
||
219 | $node, |
||
220 | 'calculateIsGreater', |
||
221 | $this->getReference($node->getChild(0)), |
||
222 | $this->getReference($node->getChild(1)) |
||
223 | ); |
||
224 | break; |
||
225 | |||
226 | case QueryAstNodeType::CALCULATE_IS_REGEXP: |
||
227 | $this->addMethodCall( |
||
228 | $node, |
||
229 | 'calculateIsRegExp', |
||
230 | $this->php->val($node->getAttribute('pattern')), |
||
231 | $this->getReference($node->getChild(0)) |
||
232 | ); |
||
233 | break; |
||
234 | |||
235 | case QueryAstNodeType::FETCH_CHILDREN: |
||
236 | $this->addMethodCall( |
||
237 | $node, |
||
238 | 'fetchChildren', |
||
239 | $this->getReference($node->getChild(0)), |
||
240 | new Arg( |
||
241 | $this->getReference($node->getChild(1)), |
||
242 | false, |
||
243 | true |
||
244 | ) |
||
245 | ); |
||
246 | break; |
||
247 | |||
248 | case QueryAstNodeType::FETCH_CHILDREN_DEEP: |
||
249 | $this->addMethodCall( |
||
250 | $node, |
||
251 | 'fetchChildrenDeep', |
||
252 | $this->getReference($node->getChild(0)), |
||
253 | new Arg( |
||
254 | $this->getReference($node->getChild(1)), |
||
255 | false, |
||
256 | true |
||
257 | ) |
||
258 | ); |
||
259 | break; |
||
260 | |||
261 | case QueryAstNodeType::MATCH_ANY_CHILD: |
||
262 | $this->addMethodCall( |
||
263 | $node, |
||
264 | 'matchAnyChild', |
||
265 | $this->getReference($node->getChild(0)) |
||
266 | ); |
||
267 | break; |
||
268 | |||
269 | case QueryAstNodeType::MATCH_PROPERTY_STRICTLY: |
||
270 | $this->addMethodCall( |
||
271 | $node, |
||
272 | 'matchPropertyStrictly', |
||
273 | $this->getReference($node->getChild(0)), |
||
274 | ); |
||
275 | break; |
||
276 | |||
277 | case QueryAstNodeType::MATCH_ELEMENT_STRICTLY: |
||
278 | $this->addMethodCall( |
||
279 | $node, |
||
280 | 'matchElementStrictly', |
||
281 | $this->getReference($node->getChild(0)), |
||
282 | ); |
||
283 | break; |
||
284 | |||
285 | case QueryAstNodeType::AGGREGATE: |
||
286 | $this->addMethodCall( |
||
287 | $node, |
||
288 | 'aggregate', |
||
289 | $this->php->val($node->getAttribute('name')), |
||
290 | $this->getReference($node->getChild(0)), |
||
291 | ); |
||
292 | break; |
||
293 | |||
294 | case QueryAstNodeType::POPULATE_LITERAL: |
||
295 | $this->addMethodCall( |
||
296 | $node, |
||
297 | 'populateLiteral', |
||
298 | $this->getReference($node->getChild(0)), |
||
299 | $this->getReference($node->getChild(1)) |
||
300 | ); |
||
301 | break; |
||
302 | |||
303 | case QueryAstNodeType::POPULATE_LITERAL_ARRAY: |
||
304 | $this->addMethodCall( |
||
305 | $node, |
||
306 | 'populateLiteralArray', |
||
307 | $this->getReference($node->getChild(0)), |
||
308 | new Arg( |
||
309 | $this->getReference($node->getChild(1)), |
||
310 | false, |
||
311 | true |
||
312 | ) |
||
313 | ); |
||
314 | break; |
||
315 | |||
316 | case QueryAstNodeType::POPULATE_INDEX_LIST: |
||
317 | $this->addMethodCall( |
||
318 | $node, |
||
319 | 'populateIndexList', |
||
320 | $this->getReference($node->getChild(0)), |
||
321 | ...array_map( |
||
322 | [$this->php, 'val'], |
||
323 | $node->getAttribute('indexList') |
||
324 | ) |
||
325 | ); |
||
326 | break; |
||
327 | |||
328 | case QueryAstNodeType::POPULATE_INDEX_SLICE: |
||
329 | $attributes = $node->getAttributeList(); |
||
330 | $this->addMethodCall( |
||
331 | $node, |
||
332 | 'populateIndexSlice', |
||
333 | $this->getReference($node->getChild(0)), |
||
334 | $this->php->val($attributes['start'] ?? null), |
||
335 | $this->php->val($attributes['end'] ?? null), |
||
336 | $this->php->val($attributes['step'] ?? null) |
||
337 | ); |
||
338 | |||
339 | break; |
||
340 | |||
341 | case QueryAstNodeType::POPULATE_NAME_LIST: |
||
342 | $this->addMethodCall( |
||
343 | $node, |
||
344 | 'populateNameList', |
||
345 | $this->getReference($node->getChild(0)), |
||
346 | ...array_map( |
||
347 | [$this->php, 'val'], |
||
348 | $node->getAttribute('nameList') |
||
349 | ) |
||
350 | ); |
||
351 | break; |
||
352 | |||
353 | case QueryAstNodeType::CREATE_SCALAR: |
||
354 | $attributes = $node->getAttributeList(); |
||
355 | // TODO: allow accessing null attributes |
||
356 | $value = $this->php->val($attributes['value'] ?? null); |
||
357 | $this->addMethodCall( |
||
358 | $node, |
||
359 | 'createScalar', |
||
360 | $value |
||
361 | ); |
||
362 | break; |
||
363 | |||
364 | case QueryAstNodeType::CREATE_ARRAY: |
||
365 | // [ X:APPEND_TO_ARRAY ] |
||
366 | $items = []; |
||
367 | foreach ($node->getChildList() as $child) { |
||
368 | $items[] = $this->getReference($child); |
||
369 | } |
||
370 | $this->stmts[] = new Assign( |
||
371 | $this->createReference($node), |
||
372 | $this->php->val($items) |
||
373 | ); |
||
374 | break; |
||
375 | |||
376 | case QueryAstNodeType::APPEND_TO_ARRAY: |
||
377 | // [ 0:<value> ] |
||
378 | $this->setReference( |
||
379 | $node, |
||
380 | $this->getReference($node->getChild(0)) |
||
381 | ); |
||
382 | break; |
||
383 | } |
||
430 |