Conditions | 10 |
Paths | 30 |
Total Lines | 34 |
Code Lines | 23 |
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 |
||
67 | protected function _format($object) |
||
68 | { |
||
69 | $value = null; |
||
70 | if (is_array($object)) { |
||
71 | if (isset($object[$this->field])) { |
||
72 | $value = $object[$this->field]; |
||
73 | } |
||
74 | } elseif (is_object($object)) { |
||
75 | $funcPrefix = [ |
||
76 | 'get', |
||
77 | 'is', |
||
78 | 'has', |
||
79 | ]; |
||
80 | foreach ($funcPrefix as $prefix) { |
||
81 | $methodName = $prefix.$this->field; |
||
82 | if (method_exists($object, $methodName)) { |
||
83 | $value = $object->$methodName(); |
||
84 | break; |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | if (is_object($value)) { |
||
89 | if ($value instanceof \DateTime) { |
||
90 | return $value->format(\DateTime::ISO8601); |
||
91 | } |
||
92 | |||
93 | return 'object: '.get_class($value); |
||
94 | } elseif (is_scalar($value)) { |
||
95 | return $value; |
||
96 | } elseif (is_array($value)) { |
||
97 | return 'array: '.print_r($value, true); |
||
98 | } |
||
99 | |||
100 | return $value; |
||
101 | } |
||
127 |