| Total Complexity | 54 |
| Total Lines | 343 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Arrays_Merge_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 Arrays_Merge_Trait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | trait Arrays_Merge_Trait |
||
| 10 | { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Merges two rows by replacing their column values by MergeBuckets |
||
| 14 | * containing their values. |
||
| 15 | * |
||
| 16 | * @param array $existing_row |
||
| 17 | * @param array $conflict_row |
||
| 18 | * @param scalar $key |
||
| 19 | * |
||
| 20 | * @return array |
||
| 21 | */ |
||
| 22 | public static function mergeInColumnBuckets( |
||
| 23 | $existing_row, |
||
| 24 | $conflict_row, |
||
| 25 | $existing_key=null, |
||
| 26 | $conflict_key=null |
||
| 27 | ) { |
||
| 28 | static::mustBeCountable($existing_row); |
||
| 29 | static::mustBeCountable($conflict_row); |
||
| 30 | |||
| 31 | $merged_row = []; |
||
| 32 | foreach ($existing_row as $existing_column => $existing_value) { |
||
| 33 | if ($existing_value instanceof MergeBucket) { |
||
| 34 | $merged_row[ $existing_column ] = $existing_value; |
||
| 35 | } |
||
| 36 | else { |
||
| 37 | if (isset($existing_key)) { |
||
| 38 | $merged_row[ $existing_column ] = MergeBucket::from([ |
||
| 39 | $existing_key => $existing_value |
||
| 40 | ]); |
||
| 41 | } |
||
| 42 | else { |
||
| 43 | $merged_row[ $existing_column ] = MergeBucket::from([ |
||
| 44 | $existing_value |
||
| 45 | ]); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | foreach ($conflict_row as $conflict_column => $conflict_value) { |
||
| 51 | if (! isset($merged_row[ $conflict_column ])) { |
||
| 52 | $merged_row[ $conflict_column ] = new MergeBucket; |
||
| 53 | } |
||
| 54 | |||
| 55 | if ($conflict_value instanceof MergeBucket) { |
||
| 56 | foreach ($conflict_value as $conflict_bucket_value) { |
||
| 57 | if (isset($conflict_key)) { |
||
| 58 | $merged_row[ $conflict_column ][$conflict_key] = $conflict_bucket_value; |
||
| 59 | } |
||
| 60 | else { |
||
| 61 | $merged_row[ $conflict_column ][] = $conflict_bucket_value; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 | else { |
||
| 66 | if (isset($conflict_key)) { |
||
| 67 | $merged_row[ $conflict_column ][$conflict_key] = $conflict_value; |
||
| 68 | } |
||
| 69 | else { |
||
| 70 | $merged_row[ $conflict_column ][] = $conflict_value; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | return $merged_row; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Merges two rows |
||
| 80 | * |
||
| 81 | * @param array $existing_row |
||
| 82 | * @param array $conflict_row |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | * |
||
| 86 | * @deprecated |
||
| 87 | */ |
||
| 88 | public static function mergePreservingDistincts( |
||
| 89 | $existing_row, |
||
| 90 | $conflict_row |
||
| 91 | ) { |
||
| 92 | return self::mergeInColumnBuckets($existing_row, $conflict_row); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * This is the cleaning last part of self::mergePreservingDistincts() |
||
| 97 | * |
||
| 98 | * @param array|Countable $row |
||
|
|
|||
| 99 | * @param array $options : 'excluded_columns' |
||
| 100 | * |
||
| 101 | * @see mergePreservingDistincts() |
||
| 102 | * @see cleanMergeDuplicates() |
||
| 103 | */ |
||
| 104 | public static function cleanMergeBuckets($row, array $options=[]) |
||
| 105 | { |
||
| 106 | static::mustBeCountable($row); |
||
| 107 | |||
| 108 | $excluded_columns = isset($options['excluded_columns']) |
||
| 109 | ? $options['excluded_columns'] |
||
| 110 | : [] |
||
| 111 | ; |
||
| 112 | |||
| 113 | foreach ($row as $column => &$values) { |
||
| 114 | if (in_array($column, $excluded_columns)) { |
||
| 115 | continue; |
||
| 116 | } |
||
| 117 | |||
| 118 | if ($values instanceof MergeBucket) { |
||
| 119 | $values = $values->toArray(); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | return $row; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * This is the cleaning part of self::mergePreservingDistincts() |
||
| 128 | * |
||
| 129 | * @param array|Countable $row |
||
| 130 | * @param array $options : 'excluded_columns' |
||
| 131 | */ |
||
| 132 | public static function cleanMergeDuplicates($row, array $options=[]) |
||
| 133 | { |
||
| 134 | static::mustBeCountable($row); |
||
| 135 | |||
| 136 | $excluded_columns = isset($options['excluded_columns']) |
||
| 137 | ? $options['excluded_columns'] |
||
| 138 | : [] |
||
| 139 | ; |
||
| 140 | |||
| 141 | foreach ($row as $column => &$values) { |
||
| 142 | if ( ! $values instanceof MergeBucket) |
||
| 143 | continue; |
||
| 144 | |||
| 145 | if (in_array($column, $excluded_columns)) |
||
| 146 | continue; |
||
| 147 | |||
| 148 | $values = Arrays::unique($values); |
||
| 149 | if (count($values) == 1) |
||
| 150 | $values = $values[0]; |
||
| 151 | } |
||
| 152 | |||
| 153 | return $row; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Equivalent of array_merge_recursive with more options. |
||
| 158 | * |
||
| 159 | * @param array $existing_row |
||
| 160 | * @param array $conflict_row |
||
| 161 | * @param callable|null $merge_resolver |
||
| 162 | * @param int $max_depth |
||
| 163 | * |
||
| 164 | * + If exist only in conflict row => add |
||
| 165 | * + If same continue |
||
| 166 | * + If different merge as array |
||
| 167 | */ |
||
| 168 | public static function mergeRecursiveCustom( |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Taken from Kohana's Arr class. |
||
| 227 | * |
||
| 228 | * Recursively merge two or more arrays. Values in an associative array |
||
| 229 | * overwrite previous values with the same key. Values in an indexed array |
||
| 230 | * are appended, but only when they do not already exist in the result. |
||
| 231 | * |
||
| 232 | * Note that this does not work the same as [array_merge_recursive](http://php.net/array_merge_recursive)! |
||
| 233 | * |
||
| 234 | * $john = array('name' => 'john', 'children' => array('fred', 'paul', 'sally', 'jane')); |
||
| 235 | * $mary = array('name' => 'mary', 'children' => array('jane')); |
||
| 236 | * |
||
| 237 | * // John and Mary are married, merge them together |
||
| 238 | * $john = Arr::merge($john, $mary); |
||
| 239 | * |
||
| 240 | * // The output of $john will now be: |
||
| 241 | * array('name' => 'mary', 'children' => array('fred', 'paul', 'sally', 'jane')) |
||
| 242 | * |
||
| 243 | * @param array $array1 initial array |
||
| 244 | * @param array $array2,... array to merge |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | public static function merge($array1, $array2) |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * If an array contains merge buckets merge all those buckets with |
||
| 316 | * the other values. |
||
| 317 | * |
||
| 318 | * This is a uni-dimensional flatten implementation |
||
| 319 | * |
||
| 320 | * @param array An array containing MergeBuckets |
||
| 321 | * @return array An array conatining all the values of the MergeBuckets |
||
| 322 | * and those of the initial array. |
||
| 323 | */ |
||
| 324 | public static function flattenMergeBuckets(array $array) |
||
| 356 |