| Conditions | 21 |
| Paths | 172 |
| Total Lines | 80 |
| Code Lines | 47 |
| 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 |
||
| 203 | private function rearrangeArray(array $original, array $new) |
||
| 204 | { |
||
| 205 | $first = reset($original); |
||
| 206 | if (!$first instanceof \stdClass) { |
||
| 207 | return $new; |
||
| 208 | } |
||
| 209 | |||
| 210 | $uniqueKey = false; |
||
| 211 | $uniqueIdx = array(); |
||
| 212 | |||
| 213 | // find unique key for all items |
||
| 214 | $f = get_object_vars($first); |
||
| 215 | foreach ($f as $key => $value) { |
||
| 216 | if (is_array($value) || $value instanceof \stdClass) { |
||
| 217 | continue; |
||
| 218 | } |
||
| 219 | |||
| 220 | $keyIsUnique = true; |
||
| 221 | $uniqueIdx = array(); |
||
| 222 | foreach ($original as $item) { |
||
| 223 | if (!$item instanceof \stdClass) { |
||
| 224 | return $new; |
||
| 225 | } |
||
| 226 | $value = $item->$key; |
||
| 227 | if ($value instanceof \stdClass || is_array($value)) { |
||
| 228 | $keyIsUnique = false; |
||
| 229 | break; |
||
| 230 | } |
||
| 231 | |||
| 232 | if (isset($uniqueIdx[$value])) { |
||
| 233 | $keyIsUnique = false; |
||
| 234 | break; |
||
| 235 | } |
||
| 236 | $uniqueIdx[$value] = true; |
||
| 237 | } |
||
| 238 | |||
| 239 | if ($keyIsUnique) { |
||
| 240 | $uniqueKey = $key; |
||
| 241 | break; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | if ($uniqueKey) { |
||
| 246 | $newIdx = array(); |
||
| 247 | foreach ($new as $item) { |
||
| 248 | if (!$item instanceof \stdClass) { |
||
| 249 | return $new; |
||
| 250 | } |
||
| 251 | |||
| 252 | if (!property_exists($item, $uniqueKey)) { |
||
| 253 | return $new; |
||
| 254 | } |
||
| 255 | |||
| 256 | $value = $item->$uniqueKey; |
||
| 257 | |||
| 258 | if ($value instanceof \stdClass || is_array($value)) { |
||
| 259 | return $new; |
||
| 260 | } |
||
| 261 | |||
| 262 | if (isset($newIdx[$value])) { |
||
| 263 | return $new; |
||
| 264 | } |
||
| 265 | |||
| 266 | $newIdx[$value] = $item; |
||
| 267 | } |
||
| 268 | |||
| 269 | $newRearranged = array(); |
||
| 270 | foreach ($uniqueIdx as $key => $item) { |
||
| 271 | if (isset($newIdx[$key])) { |
||
| 272 | $newRearranged [] = $newIdx[$key]; |
||
| 273 | unset($newIdx[$key]); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | foreach ($newIdx as $item) { |
||
| 277 | $newRearranged [] = $item; |
||
| 278 | } |
||
| 279 | return $newRearranged; |
||
| 280 | } |
||
| 281 | |||
| 282 | return $new; |
||
| 283 | } |
||
| 297 | } |