Completed
Push — master ( d20ccf...046303 )
by Jean
02:40
created
src/Arrays.php 2 patches
Spacing   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -134,13 +134,13 @@  discard block
 block discarded – undo
134 134
     public static function mergeRecursiveCustom(
135 135
         array $existing_row,
136 136
         array $conflict_row,
137
-        callable $merge_resolver=null,
138
-        $max_depth=null
139
-    ){
137
+        callable $merge_resolver = null,
138
+        $max_depth = null
139
+    ) {
140 140
         foreach ($conflict_row as $column => $conflict_value) {
141 141
 
142 142
             // not existing in first array
143
-            if (!isset($existing_row[$column])) {
143
+            if ( ! isset($existing_row[$column])) {
144 144
                 $existing_row[$column] = $conflict_value;
145 145
                 continue;
146 146
             }
@@ -173,7 +173,7 @@  discard block
 block discarded – undo
173 173
             }
174 174
             else {
175 175
                 // same resolution as array_merge_recursive
176
-                if (!is_array($existing_value)) {
176
+                if ( ! is_array($existing_value)) {
177 177
                     $existing_row[$column] = [$existing_value];
178 178
                 }
179 179
 
@@ -196,11 +196,11 @@  discard block
 block discarded – undo
196 196
     public static function mergePreservingDistincts(
197 197
         array $existing_row,
198 198
         array $conflict_row
199
-    ){
199
+    ) {
200 200
         $merge = static::mergeRecursiveCustom(
201 201
             $existing_row,
202 202
             $conflict_row,
203
-            function ($existing_value, $conflict_value, $column) {
203
+            function($existing_value, $conflict_value, $column) {
204 204
 
205 205
                 if ( ! $existing_value instanceof MergeBucket) {
206 206
                     $existing_value = MergeBucket::from()->push($existing_value);
@@ -230,7 +230,7 @@  discard block
 block discarded – undo
230 230
     {
231 231
         foreach ($merged_row as $entry => $values) {
232 232
             if ($values instanceof MergeBucket) {
233
-                $merged_row[ $entry ] = $values->toArray();
233
+                $merged_row[$entry] = $values->toArray();
234 234
             }
235 235
         }
236 236
 
@@ -242,10 +242,10 @@  discard block
 block discarded – undo
242 242
      *
243 243
      * @see mergePreservingDistincts()
244 244
      */
245
-    public static function keepUniqueColumnValues(array $row, array $excluded_columns=[])
245
+    public static function keepUniqueColumnValues(array $row, array $excluded_columns = [])
246 246
     {
247 247
         foreach ($row as $column => &$values) {
248
-            if (!is_array($values))
248
+            if ( ! is_array($values))
249 249
                 continue;
250 250
 
251 251
             if (in_array($column, $excluded_columns))
@@ -269,7 +269,7 @@  discard block
 block discarded – undo
269 269
      */
270 270
     public static function unique($array)
271 271
     {
272
-        if (! is_array($array) && ! $array instanceof \Traversable) {
272
+        if ( ! is_array($array) && ! $array instanceof \Traversable) {
273 273
             throw new \InvalidArgumentException(
274 274
                 "\$array must be an array or a \Traversable instead of: \n"
275 275
                 .var_export($array, true)
@@ -285,13 +285,13 @@  discard block
 block discarded – undo
285 285
                 $id = serialize($value);
286 286
             }
287 287
 
288
-            if (isset($ids[ $id ])) {
289
-                unset($array[ $key ]);
290
-                $ids[ $id ][] = $key;
288
+            if (isset($ids[$id])) {
289
+                unset($array[$key]);
290
+                $ids[$id][] = $key;
291 291
                 continue;
292 292
             }
293 293
 
294
-            $ids[ $id ] = [$key];
294
+            $ids[$id] = [$key];
295 295
         }
296 296
 
297 297
         return $array;
@@ -308,7 +308,7 @@  discard block
 block discarded – undo
308 308
      */
309 309
     public static function sum($array)
310 310
     {
311
-        if (! is_array($array) && ! $array instanceof \Traversable) {
311
+        if ( ! is_array($array) && ! $array instanceof \Traversable) {
312 312
             throw new \InvalidArgumentException(
313 313
                 "\$array must be an array or a \Traversable instead of: \n"
314 314
                 .var_export($array, true)
@@ -384,16 +384,16 @@  discard block
 block discarded – undo
384 384
             throw new \InvalidArgumentException(
385 385
                 "Different number of "
386 386
                 ." values and weights for weight mean calculation: \n"
387
-                .var_export($values,  true)."\n\n"
387
+                .var_export($values, true)."\n\n"
388 388
                 .var_export($weights, true)
389 389
             );
390 390
         }
391 391
 
392
-        if (!$values)
392
+        if ( ! $values)
393 393
             return null;
394 394
 
395
-        $weights_sum  = array_sum($weights);
396
-        if (!$weights_sum)
395
+        $weights_sum = array_sum($weights);
396
+        if ( ! $weights_sum)
397 397
             return 0;
398 398
 
399 399
         $weighted_sum = 0;
Please login to merge, or discard this patch.
Braces   +38 added lines, -40 removed lines patch added patch discarded remove patch
@@ -64,14 +64,12 @@  discard block
 block discarded – undo
64 64
                 )
65 65
                 {
66 66
                     $array1[$key] = self::merge($array1[$key], $value);
67
-                }
68
-                else
67
+                } else
69 68
                 {
70 69
                     $array1[$key] = $value;
71 70
                 }
72 71
             }
73
-        }
74
-        else
72
+        } else
75 73
         {
76 74
             foreach ($array2 as $value)
77 75
             {
@@ -96,14 +94,12 @@  discard block
 block discarded – undo
96 94
                         )
97 95
                         {
98 96
                             $array1[$key] = self::merge($array1[$key], $value);
99
-                        }
100
-                        else
97
+                        } else
101 98
                         {
102 99
                             $array1[$key] = $value;
103 100
                         }
104 101
                     }
105
-                }
106
-                else
102
+                } else
107 103
                 {
108 104
                     foreach ($array2 as $value)
109 105
                     {
@@ -170,8 +166,7 @@  discard block
 block discarded – undo
170 166
                         $column,
171 167
                     ]
172 168
                 );
173
-            }
174
-            else {
169
+            } else {
175 170
                 // same resolution as array_merge_recursive
176 171
                 if (!is_array($existing_value)) {
177 172
                     $existing_row[$column] = [$existing_value];
@@ -245,15 +240,18 @@  discard block
 block discarded – undo
245 240
     public static function keepUniqueColumnValues(array $row, array $excluded_columns=[])
246 241
     {
247 242
         foreach ($row as $column => &$values) {
248
-            if (!is_array($values))
249
-                continue;
243
+            if (!is_array($values)) {
244
+                            continue;
245
+            }
250 246
 
251
-            if (in_array($column, $excluded_columns))
252
-                continue;
247
+            if (in_array($column, $excluded_columns)) {
248
+                            continue;
249
+            }
253 250
 
254 251
             $values = array_unique($values);
255
-            if (count($values) == 1)
256
-                $values = $values[0];
252
+            if (count($values) == 1) {
253
+                            $values = $values[0];
254
+            }
257 255
         }
258 256
 
259 257
         return $row;
@@ -280,8 +278,7 @@  discard block
 block discarded – undo
280 278
         foreach ($array as $key => $value) {
281 279
             if (is_scalar($value)) {
282 280
                 $id = $value;
283
-            }
284
-            else {
281
+            } else {
285 282
                 $id = serialize($value);
286 283
             }
287 284
 
@@ -319,28 +316,23 @@  discard block
 block discarded – undo
319 316
         foreach ($array as $key => &$value) { // &for optimization
320 317
             if (is_scalar($value)) {
321 318
                 $sum += $value;
322
-            }
323
-            elseif (is_null($value)) {
319
+            } elseif (is_null($value)) {
324 320
                 continue;
325
-            }
326
-            elseif (is_array($value)) {
321
+            } elseif (is_array($value)) {
327 322
                 throw new \InvalidArgumentException(
328 323
                     "Trying to sum an array with '$sum': ".var_export($value, true)
329 324
                 );
330
-            }
331
-            elseif (is_object($value)) {
325
+            } elseif (is_object($value)) {
332 326
                 if (version_compare(phpversion(), '7.0.0', '>=')) {
333 327
                     try {
334 328
                         $sum += $value->toNumber();
335
-                    }
336
-                    catch (\Exception $e) {
329
+                    } catch (\Exception $e) {
337 330
                         throw new \InvalidArgumentEXception(
338 331
                              "Trying to sum a ".get_class($value)." object which cannot be casted as a number. "
339 332
                             ."Please add a toNumber() method."
340 333
                         );
341 334
                     }
342
-                }
343
-                else {
335
+                } else {
344 336
                     if ( ! method_exists($value, 'toNumber')) {
345 337
                         throw new \InvalidArgumentEXception(
346 338
                              "Trying to sum a ".get_class($value)." object which cannot be casted as a number. "
@@ -368,17 +360,21 @@  discard block
 block discarded – undo
368 360
      */
369 361
     public static function weightedMean($values, $weights)
370 362
     {
371
-        if ($values instanceof ChainableArray)
372
-            $values = $values->toArray();
363
+        if ($values instanceof ChainableArray) {
364
+                    $values = $values->toArray();
365
+        }
373 366
 
374
-        if ($weights instanceof ChainableArray)
375
-            $weights = $weights->toArray();
367
+        if ($weights instanceof ChainableArray) {
368
+                    $weights = $weights->toArray();
369
+        }
376 370
 
377
-        if ( ! is_array($values))
378
-            $values = [$values];
371
+        if ( ! is_array($values)) {
372
+                    $values = [$values];
373
+        }
379 374
 
380
-        if ( ! is_array($weights))
381
-            $weights = [$weights];
375
+        if ( ! is_array($weights)) {
376
+                    $weights = [$weights];
377
+        }
382 378
 
383 379
         if (count($values) != count($weights)) {
384 380
             throw new \InvalidArgumentException(
@@ -389,12 +385,14 @@  discard block
 block discarded – undo
389 385
             );
390 386
         }
391 387
 
392
-        if (!$values)
393
-            return null;
388
+        if (!$values) {
389
+                    return null;
390
+        }
394 391
 
395 392
         $weights_sum  = array_sum($weights);
396
-        if (!$weights_sum)
397
-            return 0;
393
+        if (!$weights_sum) {
394
+                    return 0;
395
+        }
398 396
 
399 397
         $weighted_sum = 0;
400 398
         foreach ($values as $i => $value) {
Please login to merge, or discard this patch.