| Conditions | 22 |
| Paths | 172 |
| Total Lines | 84 |
| Code Lines | 50 |
| 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 |
||
| 222 | private function rearrangeArray(array $original, array $new) |
||
| 223 | { |
||
| 224 | $first = reset($original); |
||
| 225 | if (!$first instanceof \stdClass) { |
||
| 226 | return $new; |
||
| 227 | } |
||
| 228 | |||
| 229 | $uniqueKey = false; |
||
| 230 | $uniqueIdx = array(); |
||
| 231 | |||
| 232 | // find unique key for all items |
||
| 233 | $f = get_object_vars($first); |
||
| 234 | foreach ($f as $key => $value) { |
||
| 235 | if (is_array($value) || $value instanceof \stdClass) { |
||
| 236 | continue; |
||
| 237 | } |
||
| 238 | |||
| 239 | $keyIsUnique = true; |
||
| 240 | $uniqueIdx = array(); |
||
| 241 | foreach ($original as $item) { |
||
| 242 | if (!$item instanceof \stdClass) { |
||
| 243 | return $new; |
||
| 244 | } |
||
| 245 | if (!isset($item->$key)) { |
||
| 246 | $keyIsUnique = false; |
||
| 247 | break; |
||
| 248 | } |
||
| 249 | $value = $item->$key; |
||
| 250 | if ($value instanceof \stdClass || is_array($value)) { |
||
| 251 | $keyIsUnique = false; |
||
| 252 | break; |
||
| 253 | } |
||
| 254 | |||
| 255 | if (isset($uniqueIdx[$value])) { |
||
| 256 | $keyIsUnique = false; |
||
| 257 | break; |
||
| 258 | } |
||
| 259 | $uniqueIdx[$value] = true; |
||
| 260 | } |
||
| 261 | |||
| 262 | if ($keyIsUnique) { |
||
| 263 | $uniqueKey = $key; |
||
| 264 | break; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | if ($uniqueKey) { |
||
| 269 | $newIdx = array(); |
||
| 270 | foreach ($new as $item) { |
||
| 271 | if (!$item instanceof \stdClass) { |
||
| 272 | return $new; |
||
| 273 | } |
||
| 274 | |||
| 275 | if (!property_exists($item, $uniqueKey)) { |
||
| 276 | return $new; |
||
| 277 | } |
||
| 278 | |||
| 279 | $value = $item->$uniqueKey; |
||
| 280 | |||
| 281 | if ($value instanceof \stdClass || is_array($value)) { |
||
| 282 | return $new; |
||
| 283 | } |
||
| 284 | |||
| 285 | if (isset($newIdx[$value])) { |
||
| 286 | return $new; |
||
| 287 | } |
||
| 288 | |||
| 289 | $newIdx[$value] = $item; |
||
| 290 | } |
||
| 291 | |||
| 292 | $newRearranged = array(); |
||
| 293 | foreach ($uniqueIdx as $key => $item) { |
||
| 294 | if (isset($newIdx[$key])) { |
||
| 295 | $newRearranged [] = $newIdx[$key]; |
||
| 296 | unset($newIdx[$key]); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | foreach ($newIdx as $item) { |
||
| 300 | $newRearranged [] = $item; |
||
| 301 | } |
||
| 302 | return $newRearranged; |
||
| 303 | } |
||
| 304 | |||
| 305 | return $new; |
||
| 306 | } |
||
| 307 | } |