Conditions | 25 |
Paths | 4 |
Total Lines | 163 |
Code Lines | 52 |
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 |
||
239 | public function yieldArgumentsFromRule(Rule $rule, array $tokens): array |
||
240 | { |
||
241 | |||
242 | // Create an array of new Argument |
||
243 | // In most cases, a single Rule will create a single Arugment |
||
244 | // Unless the Rule contains an ARGH_SEMANTICS_FLAGS, which creates an Argument for each flag |
||
245 | |||
246 | $argument = array(); |
||
247 | |||
248 | // Loop through $tokens and define Argument(s) based on the current rules semantics |
||
249 | $count_tokens = count($tokens); |
||
250 | |||
251 | for($i=1; $i<$count_tokens; $i++) |
||
252 | { |
||
253 | $token = $tokens[$i]; |
||
254 | $semantics = $rule->semantics()[$i-1]; |
||
255 | |||
256 | //echo __METHOD__ . ": token: $token (" . Rule::semanticsToString($semantics) . ")\n"; |
||
257 | |||
258 | switch( $semantics ) |
||
259 | { |
||
260 | case ARGH_SEMANTICS_FLAG: |
||
261 | |||
262 | if( $this->parameterCollection->exists($token) ) |
||
263 | { |
||
264 | // This Rule will create a single Argument |
||
265 | if(count($argument)==0) $argument[0] = new Argument($token); |
||
266 | } |
||
267 | else |
||
268 | { |
||
269 | // This token does NOT match the flag of any defined parameter |
||
270 | // This Rule will NOT yield any arguments |
||
271 | break 2; // Break from this switch and for loop |
||
272 | } |
||
273 | |||
274 | break; |
||
275 | |||
276 | case ARGH_SEMANTICS_FLAGS: |
||
277 | |||
278 | // Check every character of this $token for a matching parameter 'flag' |
||
279 | for($j=0; $j<strlen($token); $j++) |
||
280 | { |
||
281 | if( $this->parameterCollection->exists( $token{$j} ) ) |
||
282 | { |
||
283 | // This Rule can only apply to ARGH_TYPE_BOOLEAN Parameters |
||
284 | if( ARGH_TYPE_BOOLEAN == $this->parameterCollection->get($token{$j})->getParameterType() ) |
||
285 | { |
||
286 | // Create new Argument for each flag |
||
287 | if( !array_key_exists($j, $argument) ) $argument[$j] = new Argument($token{$j}); |
||
288 | } |
||
289 | } |
||
290 | else |
||
291 | { |
||
292 | // A character in $token, does not match a defined Parameter flag |
||
293 | // This Rule will NOT yield any arguments |
||
294 | // Undo the creation of new Arguments under this Rule |
||
295 | $argument = array(); |
||
296 | |||
297 | break; // Break from this for loop |
||
298 | } |
||
299 | |||
300 | } // END: for($j=0; $j<strlen($token); $j++) |
||
301 | |||
302 | break; |
||
303 | |||
304 | case ARGH_SEMANTICS_NAME: |
||
305 | |||
306 | if( $this->parameterCollection->exists($token) ) |
||
307 | { |
||
308 | // This Rule will create a single Argument |
||
309 | if(count($argument)==0) $argument[0] = new Argument($token); |
||
310 | } |
||
311 | else |
||
312 | { |
||
313 | // This token does NOT match the flag of any defined parameter |
||
314 | // This Rule will NOT yield any arguments |
||
315 | break 2; // Break from this switch and for loop |
||
316 | } |
||
317 | |||
318 | break; |
||
319 | |||
320 | case ARGH_SEMANTICS_VALUE: |
||
321 | |||
322 | // Usually, the Argument will have already been created by another token in this Rule |
||
323 | |||
324 | // If no new Argument created by this Rule yet, create one now |
||
325 | if(count($argument)==0) $argument[0] = new Argument(); |
||
326 | |||
327 | // The new Argument's 'key' should be set by another token in this Rule |
||
328 | $argument[0]->setValue($token); |
||
329 | |||
330 | break; |
||
331 | |||
332 | case ARGH_SEMANTICS_LIST: |
||
333 | |||
334 | // Usually, the Argument will have already been created by another token in this Rule |
||
335 | |||
336 | // If no new Argument created by this Rule yet, create one now |
||
337 | if(count($argument)==0) $argument[0] = new Argument(); |
||
338 | |||
339 | // Trim brackets from the $token (list) |
||
340 | $token = trim($token, "[]"); |
||
341 | |||
342 | // Explode comma seperated list into elements |
||
343 | $elements = explode(',', $token); |
||
344 | |||
345 | // Use the $elements array as the 'value' for all new Argument created by this Rule |
||
346 | // Usually, this will only apply to a single Argument, unless this Rule contains ARGH_SEMANTICS_FLAGS |
||
347 | foreach($argument as &$a) $a->setValue($elements); |
||
348 | |||
349 | break; |
||
350 | |||
351 | case ARGH_SEMANTICS_COMMAND: |
||
352 | |||
353 | // Check if ParameterCollection contains any commands |
||
354 | if($this->parameterCollection->hasCommand()) |
||
355 | { |
||
356 | // Retrieve all ARGH_TYPE_COMMAND Parameters |
||
357 | $commands = $this->parameterCollection->getCommands(); |
||
358 | |||
359 | foreach($commands as $p) |
||
360 | { |
||
361 | if($p->hasOptions()) |
||
362 | { |
||
363 | if( in_array($token, $p->getOptions()) ) |
||
364 | { |
||
365 | // $token matches an option of this ARGH_TYPE_COMMAND Parameter |
||
366 | |||
367 | // If no new Argument created by this Rule yet, create one now |
||
368 | if(count($argument)==0) $argument[0] = new Argument($p->getName(), $token); |
||
369 | |||
370 | // Stop searching this Parameters options |
||
371 | break; |
||
372 | |||
373 | } // END: if( in_array($token, $p->options()) ) |
||
374 | } // END: if($p->hasOptions()) |
||
375 | } // END: foreach($commands as $p) |
||
376 | |||
377 | |||
378 | } // END: if($this->parameterCollection->hasCommand()) |
||
379 | |||
380 | break; |
||
381 | |||
382 | case ARGH_SEMANTICS_VARIABLE: |
||
383 | |||
384 | // Create a new Argument to hold values |
||
385 | $argument[0] = new Argument(Parameter::ARGH_NAME_VARIABLE, $token); |
||
386 | |||
387 | break; |
||
388 | |||
389 | default: |
||
390 | |||
391 | throw new ArghException(__CLASS__ . ': Token has unknown semantic meaning.'); |
||
392 | } |
||
393 | |||
394 | } // END: for($j=1; $j<count($matches); $j++) |
||
395 | |||
396 | |||
397 | //echo 'Yielded Arguments:' . "\n"; |
||
398 | //print_r($argument); |
||
399 | |||
400 | // Return an array of Arguments yielded by this Rule |
||
401 | return $argument; |
||
402 | |||
405 | } |