| Total Complexity | 92 |
| Total Lines | 709 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ChainableArray_Utils_Trait 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 ChainableArray_Utils_Trait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | trait ChainableArray_Utils_Trait |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Same as $this->groupByTransformed() without the transformer to |
||
| 11 | * improve the readability in some cases. |
||
| 12 | * |
||
| 13 | * @param callable $indexGenerator Can return a scalar or an array. |
||
| 14 | * Multiple indexes allow to add a row to multiple groups. |
||
| 15 | * @param callable $conflictResolver |
||
| 16 | * |
||
| 17 | * @throws Missing conflict resolver |
||
| 18 | * |
||
| 19 | * @return array The array containing the grouped rows. |
||
| 20 | */ |
||
| 21 | public function groupBy( callable $indexGenerator, callable $conflictResolver=null ) |
||
| 22 | { |
||
| 23 | // todo : this doesn't work |
||
| 24 | // return $this->groupByTransformed($indexGenerator, null, $conflictResolver); |
||
| 25 | |||
| 26 | $out = []; |
||
| 27 | foreach ($this->data as $key => $row) { |
||
| 28 | |||
| 29 | if (!$row) |
||
| 30 | continue; |
||
| 31 | |||
| 32 | $newIndexes = call_user_func($indexGenerator, $key, $row); |
||
| 33 | if (!is_array($newIndexes)) |
||
| 34 | $newIndexes = [$newIndexes]; |
||
| 35 | |||
| 36 | foreach ($newIndexes as $newIndex) { |
||
| 37 | if (!isset($out[$newIndex])) { |
||
| 38 | $out[$newIndex] = $row; |
||
| 39 | } |
||
| 40 | else { |
||
| 41 | if ($conflictResolver === null) { |
||
| 42 | self::throwUsageException( |
||
| 43 | "A 'group by' provoking a conflict" |
||
| 44 | ." has no conflict resolver defined:\n" |
||
| 45 | ." + key: ".$key."\n" |
||
| 46 | ." + existing: ".var_export($out[$newIndex], true)."\n" |
||
| 47 | ." + conflict: ".var_export($row, true)."\n" |
||
| 48 | ); |
||
| 49 | } |
||
| 50 | |||
| 51 | $out[$newIndex] = call_user_func( |
||
| 52 | $conflictResolver, |
||
| 53 | $newIndex, |
||
| 54 | $out[$newIndex], |
||
| 55 | $row |
||
| 56 | ); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | return $this->returnConstant($out); |
||
|
|
|||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Group rows in arrays indexed by the index generated by $indexGenerator |
||
| 66 | * |
||
| 67 | * @param callable $indexGenerator Can return a scalar or an array. |
||
| 68 | * Multiple indexes allow to add a row to multiple groups. |
||
| 69 | * |
||
| 70 | * @return array The array containing the grouped rows. |
||
| 71 | */ |
||
| 72 | public function groupInArrays( callable $indexGenerator ) |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Parses an array and group it rows by index. This index is generated |
||
| 101 | * by the first parameter. |
||
| 102 | * The row corresponding to the new index can be different from the |
||
| 103 | * grouped ones so the second parameter allows us to transform them. |
||
| 104 | * Finally, the third parameter is used to resolve the conflict e.g. |
||
| 105 | * when two rows generate the same index. |
||
| 106 | * |
||
| 107 | * @paramb callable $indexGenerator |
||
| 108 | * @paramb callable $rowTransformer |
||
| 109 | * @paramb callable $conflictResolver |
||
| 110 | * |
||
| 111 | * @return array The array containing the grouped rows. |
||
| 112 | */ |
||
| 113 | public function groupByTransformed( |
||
| 114 | callable $indexGenerator, |
||
| 115 | callable $rowTransformer, // todo check this behavior |
||
| 116 | callable $conflictResolver ) |
||
| 117 | { |
||
| 118 | // The goal here is to remove the second parameter has it makes the |
||
| 119 | // grouping process too complicated |
||
| 120 | // if (!$conflictResolver) { |
||
| 121 | // $conflictResolver = $rowTransformer; |
||
| 122 | // $rowTransformer = null; |
||
| 123 | // } |
||
| 124 | |||
| 125 | $out = []; |
||
| 126 | foreach ($this->data as $key => $row) { |
||
| 127 | |||
| 128 | if (!$row) |
||
| 129 | continue; |
||
| 130 | |||
| 131 | $newIndex = call_user_func($indexGenerator, $key, $row); |
||
| 132 | |||
| 133 | $transformedRow = $rowTransformer |
||
| 134 | ? call_user_func($rowTransformer, $row) |
||
| 135 | : $row; |
||
| 136 | |||
| 137 | if (!isset($out[$newIndex])) { |
||
| 138 | $out[$newIndex] = $transformedRow; |
||
| 139 | } |
||
| 140 | else { |
||
| 141 | $out[$newIndex] = call_user_func( |
||
| 142 | $conflictResolver, |
||
| 143 | $newIndex, |
||
| 144 | $out[$newIndex], |
||
| 145 | $transformedRow, |
||
| 146 | $row |
||
| 147 | ); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | return $this->returnConstant($out); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Merge a table into another one |
||
| 156 | * |
||
| 157 | * @param static $otherTable The table to merge into |
||
| 158 | * @param callable $conflictResolver Defines what to do if two |
||
| 159 | * rows have the same index. |
||
| 160 | * @return static |
||
| 161 | */ |
||
| 162 | public function mergeWith( $otherTable, callable $conflictResolver=null ) |
||
| 163 | { |
||
| 164 | if (is_array($otherTable)) |
||
| 165 | $otherTable = new static($otherTable); |
||
| 166 | |||
| 167 | if (!$otherTable instanceof static) { |
||
| 168 | self::throwUsageException( |
||
| 169 | '$otherTable must be an array or an instance of '.static::class.' instead of: ' |
||
| 170 | .var_export($otherTable, true) |
||
| 171 | ); |
||
| 172 | } |
||
| 173 | |||
| 174 | $out = $this->data; |
||
| 175 | foreach ($otherTable->getArray() as $key => $row) { |
||
| 176 | |||
| 177 | if (!isset($out[$key])) { |
||
| 178 | $out[$key] = $row; |
||
| 179 | } |
||
| 180 | else { |
||
| 181 | if ($conflictResolver === null) |
||
| 182 | self::throwUsageException('No conflict resolver for a merge provoking one'); |
||
| 183 | |||
| 184 | $arguments = [ |
||
| 185 | &$key, |
||
| 186 | $out[$key], |
||
| 187 | $row |
||
| 188 | ]; |
||
| 189 | |||
| 190 | $out[$key] = call_user_func_array( |
||
| 191 | $conflictResolver, |
||
| 192 | $arguments |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | return $this->returnConstant($out); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Merge the table $otherTable into the current table. |
||
| 202 | * (same as self::mergeWith with the other table as $this) |
||
| 203 | * @return static |
||
| 204 | */ |
||
| 205 | public function mergeIn( $otherTable, callable $conflictResolver=null ) |
||
| 206 | { |
||
| 207 | $otherTable->mergeWith($this, $conflictResolver); |
||
| 208 | return $this; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * The same as self::mergeWith with an array of tables. |
||
| 213 | * |
||
| 214 | * @param array $othersTable array of HelperTable |
||
| 215 | * @param func $conflictResolver callback resolver |
||
| 216 | */ |
||
| 217 | public function mergeSeveralWith(array $othersTable, callable $conflictResolver = null) |
||
| 218 | { |
||
| 219 | foreach ($othersTable as $otherTable) { |
||
| 220 | $this->mergeWith($otherTable, $conflictResolver); |
||
| 221 | } |
||
| 222 | |||
| 223 | return $this; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * |
||
| 228 | */ |
||
| 229 | public function each(callable $rowTransformer) |
||
| 230 | { |
||
| 231 | $out = []; |
||
| 232 | foreach ($this->data as $key => $row) { |
||
| 233 | $out[$key] = call_user_func_array( |
||
| 234 | $rowTransformer, |
||
| 235 | [$row, &$key, $this->data] |
||
| 236 | ); |
||
| 237 | } |
||
| 238 | |||
| 239 | return $this->returnConstant($out); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Rename a column on every row. |
||
| 244 | * |
||
| 245 | * @todo remove this method and force the usage of $this->renameColumns()? |
||
| 246 | * @deprecated use $this->renameColumns(Array) instead] |
||
| 247 | * |
||
| 248 | */ |
||
| 249 | public function renameColumn($old_name, $new_name) |
||
| 250 | { |
||
| 251 | return $this->renameColumns([$old_name => $new_name]); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Rename a column on every row. |
||
| 256 | * |
||
| 257 | * @return static |
||
| 258 | */ |
||
| 259 | public function renameColumns(array $old_to_new_names) |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Limits the size of the array. |
||
| 281 | * |
||
| 282 | * @param int $max |
||
| 283 | * @return Heper_Table $this |
||
| 284 | * |
||
| 285 | * @todo implement other parameters for this function like in SQL |
||
| 286 | */ |
||
| 287 | public function limit() |
||
| 288 | { |
||
| 289 | $arguments = func_get_args(); |
||
| 290 | if (count($arguments) == 1 && is_numeric($arguments[0])) |
||
| 291 | $max = $arguments[0]; |
||
| 292 | else |
||
| 293 | self::throwUsageException("Bad arguments type and count for limit()"); |
||
| 294 | |||
| 295 | $out = []; |
||
| 296 | $count = 0; |
||
| 297 | foreach ($this->data as $key => $row) { |
||
| 298 | |||
| 299 | if ($max <= $count) |
||
| 300 | break; |
||
| 301 | |||
| 302 | $out[$key] = $row; |
||
| 303 | |||
| 304 | $count++; |
||
| 305 | } |
||
| 306 | |||
| 307 | return $this->returnConstant($out); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Appends an array to the current one. |
||
| 312 | * |
||
| 313 | * @param array|static $new_rows to append |
||
| 314 | * @param callable $conflict_resolver to use if a new row as the |
||
| 315 | * same key as an existing row. By default, the new |
||
| 316 | * key will be lost and the row appended as natively. |
||
| 317 | * |
||
| 318 | * @throws UsageException If the $new_rows parameter is neither an array |
||
| 319 | * nor a static. |
||
| 320 | * @return static $this |
||
| 321 | */ |
||
| 322 | public function append($new_rows, callable $conflict_resolver=null) |
||
| 323 | { |
||
| 324 | if ($new_rows instanceof static) |
||
| 325 | $new_rows = $new_rows->getArray(); |
||
| 326 | |||
| 327 | if (!is_array($new_rows)) { |
||
| 328 | $this->throwUsageException( |
||
| 329 | "\$new_rows parameter must be an array or an instance of " . __CLASS__ |
||
| 330 | ); |
||
| 331 | } |
||
| 332 | |||
| 333 | if (!$conflict_resolver) { |
||
| 334 | // default conflict resolver: append with numeric key |
||
| 335 | $conflict_resolver = function (&$data, $existing_row, $confliuct_row, $key) { |
||
| 336 | $data[] = $confliuct_row; |
||
| 337 | }; |
||
| 338 | } |
||
| 339 | |||
| 340 | foreach ($new_rows as $key => $new_row) { |
||
| 341 | if (isset($this->data[$key])) { |
||
| 342 | $arguments = [ |
||
| 343 | &$this->data, |
||
| 344 | $existing_row, |
||
| 345 | $confliuct_row, |
||
| 346 | $key |
||
| 347 | ]; |
||
| 348 | |||
| 349 | call_user_func_array($conflict_resolver, $arguments); |
||
| 350 | } |
||
| 351 | else { |
||
| 352 | $this->data[$key] = $new_row; |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | return $this; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param $columnNames scalar[] The names of the newly created columns. |
||
| 361 | * @param $options array Unsed presently |
||
| 362 | * |
||
| 363 | * @see self::dimensionsAsColumns_recurser() |
||
| 364 | * |
||
| 365 | * @return static |
||
| 366 | */ |
||
| 367 | public function dimensionsAsColumns(array $columnNames, array $options=null) |
||
| 368 | { |
||
| 369 | $out = $this->dimensionsAsColumns_recurser($this->data, $columnNames); |
||
| 370 | return $this->returnConstant($out); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * |
||
| 375 | * @todo Fix case of other columns |
||
| 376 | * |
||
| 377 | * Example: |
||
| 378 | * dimensionsAsColumns_recurser([ |
||
| 379 | * [ |
||
| 380 | * 0, |
||
| 381 | * 'me', |
||
| 382 | * ], |
||
| 383 | * [ |
||
| 384 | * 1, |
||
| 385 | * 'me_too', |
||
| 386 | * ], |
||
| 387 | * ], |
||
| 388 | * [ |
||
| 389 | * 'id', |
||
| 390 | * 'name', |
||
| 391 | * ] |
||
| 392 | * |
||
| 393 | * => [ |
||
| 394 | * 'id:0-name:me' => [ |
||
| 395 | * 'id' => 0, |
||
| 396 | * 'name' => 'me', |
||
| 397 | * ], |
||
| 398 | * 'id:1-name:me_too' => [ |
||
| 399 | * 'id' => 1, |
||
| 400 | * 'name' => 'me_too', |
||
| 401 | * ], |
||
| 402 | * ] |
||
| 403 | */ |
||
| 404 | protected function dimensionsAsColumns_recurser(array $data, $columnNames, $rowIdParts=[]) |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Returns the first element of the array |
||
| 491 | */ |
||
| 492 | public function first($strict=false) |
||
| 493 | { |
||
| 494 | if (!$this->count()) { |
||
| 495 | if ($strict) |
||
| 496 | throw new \ErrorException("No first element found in this array"); |
||
| 497 | else |
||
| 498 | $first = null; |
||
| 499 | } |
||
| 500 | else { |
||
| 501 | $key = key($this->data); |
||
| 502 | $first = reset($this->data); |
||
| 503 | $this->move($key); |
||
| 504 | } |
||
| 505 | |||
| 506 | return $first; |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Returns the last element of the array |
||
| 511 | * |
||
| 512 | * @todo Preserve the offset |
||
| 513 | */ |
||
| 514 | public function last($strict=false) |
||
| 515 | { |
||
| 516 | if (!$this->count()) { |
||
| 517 | if ($strict) |
||
| 518 | throw new \ErrorException("No last element found in this array"); |
||
| 519 | else |
||
| 520 | $last = null; |
||
| 521 | } |
||
| 522 | else { |
||
| 523 | $key = key($this->data); |
||
| 524 | $last = end($this->data); |
||
| 525 | $this->move($key); |
||
| 526 | } |
||
| 527 | |||
| 528 | return $last; |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * |
||
| 533 | */ |
||
| 534 | public function firstKey($strict=false) |
||
| 535 | { |
||
| 536 | if (!$this->count()) { |
||
| 537 | if ($strict) |
||
| 538 | throw new \ErrorException("No last element found in this array"); |
||
| 539 | else |
||
| 540 | $firstKey = null; |
||
| 541 | } |
||
| 542 | else { |
||
| 543 | $key = key($this->data); |
||
| 544 | reset($this->data); |
||
| 545 | $firstKey = key($this->data); |
||
| 546 | $this->move($key); |
||
| 547 | } |
||
| 548 | |||
| 549 | return $firstKey; |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * |
||
| 554 | */ |
||
| 555 | public function lastKey($strict=false) |
||
| 556 | { |
||
| 557 | if (!$this->count()) { |
||
| 558 | if ($strict) |
||
| 559 | throw new \ErrorException("No last element found in this array"); |
||
| 560 | else |
||
| 561 | $lastKey = null; |
||
| 562 | } |
||
| 563 | else { |
||
| 564 | $key = key($this->data); |
||
| 565 | end($this->data); |
||
| 566 | $lastKey = key($this->data); |
||
| 567 | $this->move($key); |
||
| 568 | } |
||
| 569 | |||
| 570 | return $lastKey; |
||
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Move the internal pointer of the array to the key given as parameter |
||
| 575 | */ |
||
| 576 | public function move($key, $strict=true) |
||
| 577 | { |
||
| 578 | if (array_key_exists($key, $this->data)) { |
||
| 579 | foreach ($this->data as $i => &$value) { |
||
| 580 | if ($i === $key) { |
||
| 581 | prev($this->data); |
||
| 582 | break; |
||
| 583 | } |
||
| 584 | } |
||
| 585 | } |
||
| 586 | elseif ($strict) { |
||
| 587 | throw new \ErrorException("Unable to move the internal pointer to a key that doesn't exist."); |
||
| 588 | } |
||
| 589 | |||
| 590 | return $this; |
||
| 591 | } |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Chained equivalent of in_array(). |
||
| 595 | * @return bool |
||
| 596 | */ |
||
| 597 | public function contains($value) |
||
| 598 | { |
||
| 599 | return in_array($value, $this->data); |
||
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Checks if the array is associative or not. |
||
| 604 | * @return bool |
||
| 605 | */ |
||
| 606 | public function isAssoc() |
||
| 607 | { |
||
| 608 | return Arrays::isAssoc($this->getArray()); |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Checks if the array is empty or not. |
||
| 613 | * @return bool |
||
| 614 | */ |
||
| 615 | public function isEmpty() |
||
| 618 | } |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Computes the weighted mean of the values of a column weighted |
||
| 622 | * by the values of a second one. |
||
| 623 | * |
||
| 624 | * @param string $valueColumnName |
||
| 625 | * @param string $weightColumnName |
||
| 626 | * |
||
| 627 | * @return float The calculated weighted mean. |
||
| 628 | */ |
||
| 629 | public function weightedMean($valueColumnName, $weightColumnName) |
||
| 630 | { |
||
| 631 | $values = array_column($this->data, $valueColumnName); |
||
| 632 | $weights = array_column($this->data, $weightColumnName); |
||
| 633 | |||
| 634 | return Arrays::weightedMean($values, $weights); |
||
| 635 | } |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Equivalent of var_dump(). |
||
| 639 | * |
||
| 640 | * @see http://php.net/manual/fr/function.var-dump.php |
||
| 641 | * @todo Handle xdebug dump formatting |
||
| 642 | */ |
||
| 643 | public function dump($exit=false) |
||
| 657 | } |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Scans the array recursivelly (until the max depthis reached) and replaces |
||
| 661 | * the entries with the callback; |
||
| 662 | * |
||
| 663 | * @todo move it to an Arrays class storing static methods |
||
| 664 | */ |
||
| 665 | public static function replaceEntries( |
||
| 666 | array $array, callable $replacer, $max_depth=null |
||
| 667 | ) { |
||
| 668 | foreach ($array as $key => &$row) { |
||
| 669 | $arguments = [&$row, $key]; |
||
| 670 | call_user_func_array($replacer, $arguments); |
||
| 671 | |||
| 672 | if (is_array($row) && $max_depth !== 0) { // allowing null to have no depth limit |
||
| 673 | $row = self::replaceEntries( |
||
| 674 | $row, $replacer, $max_depth ? $max_depth-1 : $max_depth |
||
| 675 | ); |
||
| 676 | } |
||
| 677 | } |
||
| 678 | |||
| 679 | return $array; |
||
| 680 | } |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Equivalent of ->filter() but removes the matching values |
||
| 684 | * |
||
| 685 | * @param callable|array $callback The filter logic with $value and $key |
||
| 686 | * as parameters. |
||
| 687 | * |
||
| 688 | * @return static $this or a new static. |
||
| 689 | */ |
||
| 690 | public function extract($callback=null) |
||
| 716 | } |
||
| 717 | |||
| 718 | /**/ |
||
| 719 | } |
||
| 720 |