Conditions | 14 |
Paths | 2 |
Total Lines | 43 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
76 | public function getFieldValues($field = null) |
||
77 | { |
||
78 | $fields = func_get_args(); |
||
79 | $elements = array(); |
||
80 | if ($fields || (isset($this->elements[0]) && is_array($this->elements[0]))) { |
||
81 | $this->each(function ($element) use (&$elements, $fields) { |
||
82 | if (is_object($element)) { |
||
83 | if ($fields) { |
||
84 | foreach ($fields as $field) { |
||
85 | $methodGet = 'get' . ucfirst($field); |
||
86 | $methodIs = 'is' . ucfirst($field); |
||
87 | |||
88 | if (method_exists($element, $methodGet)) { |
||
89 | $elements[] = $element->{$methodGet}(); |
||
90 | } elseif (method_exists($element, $methodIs)) { |
||
91 | $elements[] = $element->{$methodIs}(); |
||
92 | } elseif (property_exists($element, $field)) { |
||
93 | $elements[] = $element->{$field}; |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return; |
||
99 | } |
||
100 | |||
101 | if (is_array($element)) { |
||
102 | if ($fields) { |
||
103 | foreach ($fields as $field) { |
||
104 | if (array_key_exists($field, $element)) { |
||
105 | $elements[] = $element[$field]; |
||
106 | } |
||
107 | } |
||
108 | } else { |
||
109 | $elements = array_merge($elements, array_values($element)); |
||
110 | } |
||
111 | } |
||
112 | }); |
||
113 | |||
114 | return $elements; |
||
115 | } |
||
116 | |||
117 | return array(); |
||
118 | } |
||
119 | |||
160 |