| Total Complexity | 91 |
| Total Lines | 625 |
| 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) |
||
| 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) |
||
| 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) |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param mixed $value |
||
| 456 | * @return bool Is the $value countable or not |
||
| 457 | * @throws InvalidArgumentException |
||
| 458 | * |
||
| 459 | * @todo NotCountableException |
||
| 460 | */ |
||
| 461 | public static function mustBeCountable($value) |
||
| 462 | { |
||
| 463 | if (static::isCountable($value)) |
||
| 464 | return true; |
||
| 465 | |||
| 466 | $exception = new \InvalidArgumentException( |
||
| 467 | "A value must be Countable instead of: \n" |
||
| 468 | .var_export($value, true) |
||
| 469 | ); |
||
| 470 | |||
| 471 | // The true location of the throw is still available through the backtrace |
||
| 472 | $trace_location = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]; |
||
| 473 | $reflectionClass = new \ReflectionClass( get_class($exception) ); |
||
| 474 | |||
| 475 | // file |
||
| 476 | if (isset($trace_location['file'])) { |
||
| 477 | $reflectionProperty = $reflectionClass->getProperty('file'); |
||
| 478 | $reflectionProperty->setAccessible(true); |
||
| 479 | $reflectionProperty->setValue($exception, $trace_location['file']); |
||
| 480 | } |
||
| 481 | |||
| 482 | // line |
||
| 483 | if (isset($trace_location['line'])) { |
||
| 484 | $reflectionProperty = $reflectionClass->getProperty('line'); |
||
| 485 | $reflectionProperty->setAccessible(true); |
||
| 486 | $reflectionProperty->setValue($exception, $trace_location['line']); |
||
| 487 | } |
||
| 488 | |||
| 489 | throw $exception; |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param mixed $value |
||
| 494 | * @return bool Is the $value traversable or not |
||
| 495 | * @throws InvalidArgumentException |
||
| 496 | * |
||
| 497 | * @todo NotTraversableException |
||
| 498 | */ |
||
| 499 | public static function mustBeTraversable($value) |
||
| 500 | { |
||
| 501 | if (static::isTraversable($value)) |
||
| 502 | return true; |
||
| 503 | |||
| 504 | $exception = new \InvalidArgumentException( |
||
| 505 | "A value must be Traversable instead of: \n" |
||
| 506 | .var_export($value, true) |
||
| 507 | ); |
||
| 508 | |||
| 509 | // The true location of the throw is still available through the backtrace |
||
| 510 | $trace_location = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]; |
||
| 511 | $reflectionClass = new \ReflectionClass( get_class($exception) ); |
||
| 512 | |||
| 513 | // file |
||
| 514 | if (isset($trace_location['file'])) { |
||
| 515 | $reflectionProperty = $reflectionClass->getProperty('file'); |
||
| 516 | $reflectionProperty->setAccessible(true); |
||
| 517 | $reflectionProperty->setValue($exception, $trace_location['file']); |
||
| 518 | } |
||
| 519 | |||
| 520 | // line |
||
| 521 | if (isset($trace_location['line'])) { |
||
| 522 | $reflectionProperty = $reflectionClass->getProperty('line'); |
||
| 523 | $reflectionProperty->setAccessible(true); |
||
| 524 | $reflectionProperty->setValue($exception, $trace_location['line']); |
||
| 525 | } |
||
| 526 | |||
| 527 | throw $exception; |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Generates an id usable in hashes to identify a single grouped row. |
||
| 532 | * |
||
| 533 | * @param array $row The row of the array to group by. |
||
| 534 | * @param array $groups A list of the different groups. Groups can be |
||
| 535 | * strings describing a column name or a callable |
||
| 536 | * function, an array representing a callable, |
||
| 537 | * a function or an integer representing a column. |
||
| 538 | * If the index of the group is a string, it will |
||
| 539 | * be used as a prefix for the group name. |
||
| 540 | * Example: |
||
| 541 | * [ |
||
| 542 | * 'column_name', |
||
| 543 | * 'function_to_call', |
||
| 544 | * 4, //column_number |
||
| 545 | * 'group_prefix' => function($row){}, |
||
| 546 | * 'group_prefix2' => [$object, 'method'], |
||
| 547 | * ] |
||
| 548 | * |
||
| 549 | * @return string The unique identifier of the group |
||
| 550 | */ |
||
| 551 | public static function generateGroupId($row, array $groups_definitions, array $options=[]) |
||
| 632 | } |
||
| 633 | |||
| 634 | /**/ |
||
| 636 |