| Conditions | 12 |
| Paths | 14 |
| Total Lines | 62 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 18 | protected static function translateNode (Node $node, Node $owner = null, array &$definedObjects = []): string |
||
| 19 | { |
||
| 20 | $code = ''; |
||
| 21 | |||
| 22 | switch ($node->type) |
||
| 23 | { |
||
| 24 | case RUNTIME_EXECUTION: |
||
| 25 | $code .= self::formatLine ($node->args['code'], $definedObjects) ."\n\n"; |
||
| 26 | |||
| 27 | break; |
||
| 28 | |||
| 29 | case OBJECT_DEFINITION: |
||
| 30 | if (isset ($definedObjects[$node->args['name']])) |
||
| 31 | break; |
||
| 32 | |||
| 33 | $code .= '$'. $node->args['name'] .' = new '. $node->args['class'] .' ('. implode (', ', self::processArgs ($node->args['args'], $definedObjects)) .');' ."\n". '$'. $node->args['name'] .'->name = \''. $node->args['name'] .'\';' ."\n"; |
||
| 34 | |||
| 35 | $definedObjects[$node->args['name']] = $node->args['name']; |
||
| 36 | |||
| 37 | break; |
||
| 38 | |||
| 39 | case PROPERTY_SET: |
||
| 40 | $preset = ''; |
||
| 41 | |||
| 42 | if (preg_match ('/function \((.*)\) use \((.*)\)/', $node->args['property_value'])) |
||
| 43 | { |
||
| 44 | $use = substr ($node->args['property_value'], strpos ($node->args['property_value'], 'use')); |
||
| 45 | $use = $ouse = substr ($use, ($pos = strpos ($use, '(') + 1), strpos ($use, ')') - $pos); |
||
| 46 | $use = explode (' ', $use); |
||
| 47 | |||
| 48 | foreach ($use as $id => $useParam) |
||
| 49 | if (isset ($definedObjects[$useParam]) && $use[$id + 1][0] == '$') |
||
| 50 | { |
||
| 51 | $fname = $use[$id + 1]; |
||
| 52 | |||
| 53 | if (substr ($fname, strlen ($fname) - 1) == ',') |
||
| 54 | $fname = substr ($fname, 0, -1); |
||
| 55 | |||
| 56 | $preset .= "$fname = $useParam; "; |
||
| 57 | |||
| 58 | unset ($use[$id]); |
||
| 59 | } |
||
| 60 | |||
| 61 | $preset = self::formatLine ($preset, $definedObjects) ."\n"; |
||
| 62 | |||
| 63 | $node->args['property_value'] = self::formatLine (str_replace ($ouse, join (' ', $use), $node->args['property_value']), $definedObjects); |
||
| 64 | } |
||
| 65 | |||
| 66 | $code .= $preset .'$'. $owner->args['name'] .'->'. $node->args['property_name'] .' = '. self::formatLine ($node->args['property_value'], $definedObjects) .';' ."\n"; |
||
| 67 | |||
| 68 | break; |
||
| 69 | |||
| 70 | case METHOD_CALL: |
||
| 71 | $code .= '$'. $owner->args['name'] .'->'. $node->args['method_name'] .' ('. implode (', ', self::processArgs ($node->args['method_args'], $definedObjects)) .');' ."\n"; |
||
| 72 | |||
| 73 | break; |
||
| 74 | } |
||
| 75 | |||
| 76 | foreach ($node->getNodes () as $subnode) |
||
| 77 | $code .= self::translateNode ($subnode, $node, $definedObjects); |
||
| 78 | |||
| 79 | return $code; |
||
| 80 | } |
||
| 144 |