| Total Complexity | 78 |
| Total Lines | 508 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Arrays often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Arrays, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Arrays |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Taken from Kohana's Arr class. |
||
| 11 | * |
||
| 12 | * Tests if an array is associative or not. |
||
| 13 | * |
||
| 14 | * // Returns TRUE |
||
| 15 | * Arr::isAssoc(array('username' => 'john.doe')); |
||
| 16 | * |
||
| 17 | * // Returns FALSE |
||
| 18 | * Arr::isAssoc('foo', 'bar'); |
||
| 19 | * |
||
| 20 | * @param array $array array to check |
||
| 21 | * @return boolean |
||
| 22 | */ |
||
| 23 | public static function isAssociative(array $array) |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Taken from Kohana's Arr class. |
||
| 35 | * |
||
| 36 | * Recursively merge two or more arrays. Values in an associative array |
||
| 37 | * overwrite previous values with the same key. Values in an indexed array |
||
| 38 | * are appended, but only when they do not already exist in the result. |
||
| 39 | * |
||
| 40 | * Note that this does not work the same as [array_merge_recursive](http://php.net/array_merge_recursive)! |
||
| 41 | * |
||
| 42 | * $john = array('name' => 'john', 'children' => array('fred', 'paul', 'sally', 'jane')); |
||
| 43 | * $mary = array('name' => 'mary', 'children' => array('jane')); |
||
| 44 | * |
||
| 45 | * // John and Mary are married, merge them together |
||
| 46 | * $john = Arr::merge($john, $mary); |
||
| 47 | * |
||
| 48 | * // The output of $john will now be: |
||
| 49 | * array('name' => 'mary', 'children' => array('fred', 'paul', 'sally', 'jane')) |
||
| 50 | * |
||
| 51 | * @param array $array1 initial array |
||
| 52 | * @param array $array2,... array to merge |
||
| 53 | * @return array |
||
| 54 | */ |
||
| 55 | public static function merge($array1, $array2) |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Equivalent of array_merge_recursive with more options. |
||
| 124 | * |
||
| 125 | * @param array $existing_row |
||
| 126 | * @param array $conflict_row |
||
| 127 | * @param callable|null $merge_resolver |
||
| 128 | * @param int $max_depth |
||
| 129 | * |
||
| 130 | * + If exist only in conflict row => add |
||
| 131 | * + If same continue |
||
| 132 | * + If different merge as array |
||
| 133 | */ |
||
| 134 | public static function mergeRecursiveCustom( |
||
| 135 | $existing_row, |
||
| 136 | $conflict_row, |
||
| 137 | callable $merge_resolver=null, |
||
| 138 | $max_depth=null |
||
| 139 | ){ |
||
| 140 | static::mustBeCountable($existing_row); |
||
| 141 | static::mustBeCountable($conflict_row); |
||
| 142 | |||
| 143 | foreach ($conflict_row as $column => $conflict_value) { |
||
| 144 | |||
| 145 | // not existing in first array |
||
| 146 | if (!isset($existing_row[$column])) { |
||
| 147 | $existing_row[$column] = $conflict_value; |
||
| 148 | continue; |
||
| 149 | } |
||
| 150 | |||
| 151 | $existing_value = $existing_row[$column]; |
||
| 152 | |||
| 153 | // two arrays so we recurse |
||
| 154 | if (is_array($existing_value) && is_array($conflict_value)) { |
||
| 155 | |||
| 156 | if ($max_depth === null || $max_depth > 0) { |
||
| 157 | $existing_row[$column] = static::mergeRecursiveCustom( |
||
| 158 | $existing_value, |
||
| 159 | $conflict_value, |
||
| 160 | $merge_resolver, |
||
| 161 | $max_depth - 1 |
||
| 162 | ); |
||
| 163 | continue; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | if ($merge_resolver) { |
||
| 168 | $existing_row[$column] = call_user_func_array( |
||
| 169 | $merge_resolver, |
||
| 170 | [ |
||
| 171 | $existing_value, |
||
| 172 | $conflict_value, |
||
| 173 | $column, |
||
| 174 | ] |
||
| 175 | ); |
||
| 176 | } |
||
| 177 | else { |
||
| 178 | // same resolution as array_merge_recursive |
||
| 179 | if (!is_array($existing_value)) { |
||
| 180 | $existing_row[$column] = [$existing_value]; |
||
| 181 | } |
||
| 182 | |||
| 183 | // We store the new value with their previous ones |
||
| 184 | $existing_row[$column][] = $conflict_value; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | return $existing_row; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Merges two rows |
||
| 193 | * |
||
| 194 | * @param array $existing_row |
||
| 195 | * @param array $conflict_row |
||
| 196 | * |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | public static function mergePreservingDistincts( |
||
| 200 | $existing_row, |
||
| 201 | $conflict_row |
||
| 202 | ){ |
||
| 203 | static::mustBeCountable($existing_row); |
||
| 204 | static::mustBeCountable($conflict_row); |
||
| 205 | |||
| 206 | $merge = static::mergeRecursiveCustom( |
||
| 207 | $existing_row, |
||
| 208 | $conflict_row, |
||
| 209 | function ($existing_value, $conflict_value, $column) { |
||
| 210 | |||
| 211 | if ( ! $existing_value instanceof MergeBucket) { |
||
| 212 | $existing_value = MergeBucket::from()->push($existing_value); |
||
| 213 | } |
||
| 214 | |||
| 215 | // We store the new value with their previous ones |
||
| 216 | if ( ! $conflict_value instanceof MergeBucket) { |
||
| 217 | $conflict_value = MergeBucket::from()->push($conflict_value); |
||
| 218 | } |
||
| 219 | |||
| 220 | foreach ($conflict_value->toArray() as $conflict_key => $conflict_entry) { |
||
| 221 | $existing_value->push($conflict_entry); |
||
| 222 | } |
||
| 223 | |||
| 224 | return $existing_value; |
||
| 225 | }, |
||
| 226 | 0 |
||
| 227 | ); |
||
| 228 | |||
| 229 | return $merge; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * This is the cleaning part of self::mergePreservingDistincts() |
||
| 234 | * |
||
| 235 | * @param array|Countable $row |
||
| 236 | * @param array $options : 'excluded_columns' |
||
| 237 | */ |
||
| 238 | public static function cleanMergeDuplicates($row, array $options=[]) |
||
| 239 | { |
||
| 240 | static::mustBeCountable($row); |
||
| 241 | |||
| 242 | $excluded_columns = isset($options['excluded_columns']) |
||
| 243 | ? $options['excluded_columns'] |
||
| 244 | : [] |
||
| 245 | ; |
||
| 246 | |||
| 247 | foreach ($row as $column => &$values) { |
||
| 248 | if ( ! $values instanceof MergeBucket) |
||
| 249 | continue; |
||
| 250 | |||
| 251 | if (in_array($column, $excluded_columns)) |
||
| 252 | continue; |
||
| 253 | |||
| 254 | $values = Arrays::unique($values); |
||
| 255 | if (count($values) == 1) |
||
| 256 | $values = $values[0]; |
||
| 257 | } |
||
| 258 | |||
| 259 | return $row; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * This is the cleaning last part of self::mergePreservingDistincts() |
||
| 264 | * |
||
| 265 | * @param array|Countable $row |
||
| 266 | * @param array $options : 'excluded_columns' |
||
| 267 | * |
||
| 268 | * @see mergePreservingDistincts() |
||
| 269 | * @see cleanMergeDuplicates() |
||
| 270 | */ |
||
| 271 | public static function cleanMergeBuckets($row, array $options=[]) |
||
| 272 | { |
||
| 273 | static::mustBeCountable($row); |
||
| 274 | |||
| 275 | $excluded_columns = isset($options['excluded_columns']) |
||
| 276 | ? $options['excluded_columns'] |
||
| 277 | : [] |
||
| 278 | ; |
||
| 279 | |||
| 280 | foreach ($row as $column => &$values) { |
||
| 281 | if (in_array($column, $excluded_columns)) |
||
| 282 | continue; |
||
| 283 | |||
| 284 | if ($values instanceof MergeBucket) |
||
| 285 | $values = $values->toArray(); |
||
| 286 | } |
||
| 287 | |||
| 288 | return $row; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Replacement of array_unique, keeping the first key. |
||
| 293 | * |
||
| 294 | * @param array|\Traversable $array |
||
| 295 | * @return array|\Traversable With unique values |
||
| 296 | * |
||
| 297 | * @todo Options to keep another key than the first one? |
||
| 298 | */ |
||
| 299 | public static function unique($array) |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | */ |
||
| 326 | public static function keyExists($key, $array) |
||
| 327 | { |
||
| 328 | static::mustBeTraversable($array); |
||
| 329 | |||
| 330 | if (is_array($array)) { |
||
| 331 | return array_key_exists($key, $array); |
||
| 332 | } |
||
| 333 | elseif ($array instanceof ChainableArray || method_exists($array, 'keyExists')) { |
||
| 334 | return $array->keyExists($key); |
||
| 335 | } |
||
| 336 | else { |
||
| 337 | throw new \InvalidArgumentException( |
||
| 338 | "keyExists() method missing on :\n". var_export($array, true) |
||
| 339 | ); |
||
| 340 | } |
||
| 341 | |||
| 342 | return $array; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Replacement of array_sum wich throws exceptions instead of skipping |
||
| 347 | * bad operands. |
||
| 348 | * |
||
| 349 | * @param array|\Traversable $array |
||
| 350 | * @return int|double The sum |
||
| 351 | * |
||
| 352 | * @todo Support options like 'strict', 'skip_non_scalars', 'native' |
||
| 353 | */ |
||
| 354 | public static function sum($array) |
||
| 355 | { |
||
| 356 | static::mustBeCountable($array); |
||
| 357 | |||
| 358 | $sum = 0; |
||
| 359 | foreach ($array as $key => &$value) { // &for optimization |
||
| 360 | if (is_scalar($value)) { |
||
| 361 | $sum += $value; |
||
| 362 | } |
||
| 363 | elseif (is_null($value)) { |
||
| 364 | continue; |
||
| 365 | } |
||
| 366 | elseif (is_array($value)) { |
||
| 367 | throw new \InvalidArgumentException( |
||
| 368 | "Trying to sum an array with '$sum': ".var_export($value, true) |
||
| 369 | ); |
||
| 370 | } |
||
| 371 | elseif (is_object($value)) { |
||
| 372 | if ( ! method_exists($value, 'toNumber')) { |
||
| 373 | throw new \InvalidArgumentEXception( |
||
| 374 | "Trying to sum a ".get_class($value)." object which cannot be casted as a number. " |
||
| 375 | ."Please add a toNumber() method." |
||
| 376 | ); |
||
| 377 | } |
||
| 378 | |||
| 379 | $sum += $value->toNumber(); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | |||
| 383 | return $sum; |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * This method returns a classical mathemartic weighted mean. |
||
| 388 | * |
||
| 389 | * @todo It would ideally handled by a bridge with this fantastic math |
||
| 390 | * lib https://github.com/markrogoyski/math-php/ but we need the support |
||
| 391 | * of PHP 7 first. |
||
| 392 | * |
||
| 393 | * @see https://en.wikipedia.org/wiki/Weighted_arithmetic_mean |
||
| 394 | * @see https://github.com/markrogoyski/math-php/ |
||
| 395 | */ |
||
| 396 | public static function weightedMean($values, $weights) |
||
| 397 | { |
||
| 398 | if ($values instanceof ChainableArray) |
||
| 399 | $values = $values->toArray(); |
||
| 400 | |||
| 401 | if ($weights instanceof ChainableArray) |
||
| 402 | $weights = $weights->toArray(); |
||
| 403 | |||
| 404 | if ( ! is_array($values)) |
||
| 405 | $values = [$values]; |
||
| 406 | |||
| 407 | if ( ! is_array($weights)) |
||
| 408 | $weights = [$weights]; |
||
| 409 | |||
| 410 | if (count($values) != count($weights)) { |
||
| 411 | throw new \InvalidArgumentException( |
||
| 412 | "Different number of " |
||
| 413 | ." values and weights for weight mean calculation: \n" |
||
| 414 | .var_export($values, true)."\n\n" |
||
| 415 | .var_export($weights, true) |
||
| 416 | ); |
||
| 417 | } |
||
| 418 | |||
| 419 | if (!$values) |
||
| 420 | return null; |
||
| 421 | |||
| 422 | $weights_sum = array_sum($weights); |
||
| 423 | if (!$weights_sum) |
||
| 424 | return 0; |
||
| 425 | |||
| 426 | $weighted_sum = 0; |
||
| 427 | foreach ($values as $i => $value) { |
||
| 428 | $weighted_sum += $value * $weights[$i]; |
||
| 429 | } |
||
| 430 | |||
| 431 | return $weighted_sum / $weights_sum; |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * This is not required anymore with PHP 7. |
||
| 436 | * |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | public static function isTraversable($value) |
||
| 440 | { |
||
| 441 | return $value instanceof \Traversable || is_array($value); |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * This is not required anymore with PHP 7. |
||
| 446 | * |
||
| 447 | * @return bool |
||
| 448 | */ |
||
| 449 | public static function isCountable($value) |
||
| 450 | { |
||
| 451 | return $value instanceof \Countable || is_array($value); |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | */ |
||
| 456 | public static function mustBeCountable($value) |
||
| 483 | } |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | */ |
||
| 488 | public static function mustBeTraversable($value) |
||
| 515 | } |
||
| 516 | } |
||
| 517 | |||
| 518 | /**/ |
||
| 519 | } |
||
| 520 |