| Conditions | 16 |
| Paths | 15 |
| Total Lines | 67 |
| Code Lines | 33 |
| 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 |
||
| 19 | public function __call($method, $args) |
||
| 20 | { |
||
| 21 | if (substr($method, 0, 3) == 'add') { |
||
| 22 | $arr = strtolower(substr($method, 3)); |
||
| 23 | |||
| 24 | if (!property_exists($this, '_'.$arr) || !is_array($this->{'_'.$arr})) { |
||
| 25 | throw new \Exception('Unknown '.$this::class.'::'.$arr); |
||
| 26 | } |
||
| 27 | |||
| 28 | if (!is_array($args)) { |
||
| 29 | throw new \Exception('Incorrect arguments to '.$method); |
||
| 30 | } |
||
| 31 | |||
| 32 | if (!isset($args[0])) { |
||
| 33 | // Argument can be empty since we trim it's value |
||
| 34 | return; |
||
| 35 | } |
||
| 36 | |||
| 37 | if (is_object($args[0])) { |
||
| 38 | // Type safety? |
||
| 39 | } |
||
| 40 | |||
| 41 | $this->{'_'.$arr}[] = $args[0]; |
||
| 42 | |||
| 43 | return $this; |
||
| 44 | } elseif (substr($method, 0, 3) == 'set') { |
||
| 45 | $arr = strtolower(substr($method, 3)); |
||
| 46 | |||
| 47 | if (!property_exists($this, '_'.$arr)) { |
||
| 48 | throw new \Exception('Unknown '.$this::class.'::'.$arr); |
||
| 49 | } |
||
| 50 | |||
| 51 | if (!is_array($args)) { |
||
| 52 | throw new \Exception('Incorrect arguments to '.$method); |
||
| 53 | } |
||
| 54 | |||
| 55 | if (!isset($args[0])) { |
||
| 56 | // Argument can be empty since we trim it's value |
||
| 57 | return; |
||
| 58 | } |
||
| 59 | |||
| 60 | if (is_object($args[0])) { |
||
| 61 | // Type safety? |
||
| 62 | } |
||
| 63 | |||
| 64 | $this->{'_'.$arr} = $args[0]; |
||
| 65 | |||
| 66 | return $this; |
||
| 67 | } elseif (substr($method, 0, 3) == 'get') { |
||
| 68 | $arr = strtolower(substr($method, 3)); |
||
| 69 | |||
| 70 | // hotfix getData |
||
| 71 | if ('data' == $arr) { |
||
| 72 | if (!property_exists($this, '_text')) { |
||
| 73 | throw new \Exception('Unknown '.$this::class.'::'.$arr); |
||
| 74 | } |
||
| 75 | |||
| 76 | return $this->{'_text'}; |
||
| 77 | } |
||
| 78 | |||
| 79 | if (!property_exists($this, '_'.$arr)) { |
||
| 80 | throw new \Exception('Unknown '.$this::class.'::'.$arr); |
||
| 81 | } |
||
| 82 | |||
| 83 | return $this->{'_'.$arr}; |
||
| 84 | } else { |
||
| 85 | throw new \Exception('Unknown method called: '.$method); |
||
| 86 | } |
||
| 108 |