| Conditions | 14 |
| Paths | 2 |
| Total Lines | 43 |
| Code Lines | 26 |
| 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 |
||
| 115 | public function getFieldValues($field = null) |
||
| 116 | { |
||
| 117 | $fields = func_get_args(); |
||
| 118 | $elements = array(); |
||
| 119 | if ($fields || (isset($this->elements[0]) && is_array($this->elements[0]))) { |
||
| 120 | $this->each(function ($element) use (&$elements, $fields) { |
||
| 121 | if (is_object($element)) { |
||
| 122 | if ($fields) { |
||
| 123 | foreach ($fields as $field) { |
||
| 124 | $methodGet = 'get' . ucfirst($field); |
||
| 125 | $methodIs = 'is' . ucfirst($field); |
||
| 126 | |||
| 127 | if (method_exists($element, $methodGet)) { |
||
| 128 | $elements[] = $element->{$methodGet}(); |
||
| 129 | } elseif (method_exists($element, $methodIs)) { |
||
| 130 | $elements[] = $element->{$methodIs}(); |
||
| 131 | } elseif (property_exists($element, $field)) { |
||
| 132 | $elements[] = $element->{$field}; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | return; |
||
| 138 | } |
||
| 139 | |||
| 140 | if (is_array($element)) { |
||
| 141 | if ($fields) { |
||
|
|
|||
| 142 | foreach ($fields as $field) { |
||
| 143 | if (array_key_exists($field, $element)) { |
||
| 144 | $elements[] = $element[$field]; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } else { |
||
| 148 | $elements = array_merge($elements, array_values($element)); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | }); |
||
| 152 | |||
| 153 | return $elements; |
||
| 154 | } |
||
| 155 | |||
| 156 | return array(); |
||
| 157 | } |
||
| 158 | |||
| 206 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.