| Conditions | 10 |
| Paths | 13 |
| Total Lines | 41 |
| Code Lines | 30 |
| 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 |
||
| 71 | public function __call($name, $args) |
||
| 72 | { |
||
| 73 | try { |
||
| 74 | preg_match('/^[a-z]*/', $name, $matches); |
||
| 75 | $action = $matches[0]; |
||
| 76 | preg_match('/[A-Z][a-zA-Z]*/', $name, $matches); |
||
| 77 | $target = lcfirst($matches[0]); |
||
| 78 | } catch (\Exception $e) { |
||
| 79 | throw new \BadMethodCallException("Error while parsing method name '$name()'"); |
||
| 80 | } |
||
| 81 | |||
| 82 | switch ($action) { |
||
| 83 | case 'get': |
||
| 84 | if (isset($this->$target)) { |
||
| 85 | return $this->$target; |
||
| 86 | } |
||
| 87 | break; |
||
| 88 | |||
| 89 | case 'add': |
||
| 90 | $arg = $args[0]; |
||
| 91 | $entityName = isset(self::$plurals[$target]) ? self::$plurals[$target] : $target . 's'; |
||
| 92 | |||
| 93 | if (isset($this->$entityName) && $this->$entityName instanceof ViewModelNode) { |
||
| 94 | return $this->{$entityName}->add($arg); |
||
| 95 | } else { |
||
| 96 | $this->$entityName = new ViewModelNode($args); |
||
| 97 | return $this->{$entityName}[0]; |
||
| 98 | } |
||
| 99 | break; |
||
| 100 | |||
| 101 | case 'set': |
||
| 102 | $arg = $args[0]; |
||
| 103 | $this->$target = is_array($arg) ? new ViewModelNode($arg) : $arg; |
||
| 104 | break; |
||
| 105 | |||
| 106 | default: |
||
| 107 | throw new \BadMethodCallException("$name(): Undefined method"); |
||
| 108 | break; |
||
| 109 | } |
||
| 110 | return null; |
||
| 111 | } |
||
| 112 | |||
| 124 |