Passed
Push — master ( 2a9c77...ac68e5 )
by Jean
02:19
created
src/ChainableArray_Utils_Trait.php 1 patch
Spacing   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -18,7 +18,7 @@  discard block
 block discarded – undo
18 18
      *
19 19
      * @return array The array containing the grouped rows.
20 20
      */
21
-    public function groupBy( callable $indexGenerator, callable $conflictResolver=null )
21
+    public function groupBy(callable $indexGenerator, callable $conflictResolver = null)
22 22
     {
23 23
         // todo : this doesn't work
24 24
         // return $this->groupByTransformed($indexGenerator, null, $conflictResolver);
@@ -26,15 +26,15 @@  discard block
 block discarded – undo
26 26
         $out = [];
27 27
         foreach ($this->data as $key => $row) {
28 28
 
29
-            if (!$row)
29
+            if ( ! $row)
30 30
                 continue;
31 31
 
32 32
             $newIndexes     = call_user_func($indexGenerator, $key, $row);
33
-            if (!is_array($newIndexes))
33
+            if ( ! is_array($newIndexes))
34 34
                 $newIndexes = [$newIndexes];
35 35
 
36 36
             foreach ($newIndexes as $newIndex) {
37
-                if (!isset($out[$newIndex])) {
37
+                if ( ! isset($out[$newIndex])) {
38 38
                     $out[$newIndex] = $row;
39 39
                 }
40 40
                 else {
@@ -69,26 +69,26 @@  discard block
 block discarded – undo
69 69
      *
70 70
      * @return array The array containing the grouped rows.
71 71
      */
72
-    public function groupInArrays( callable $indexGenerator )
72
+    public function groupInArrays(callable $indexGenerator)
73 73
     {
74 74
         $out = [];
75 75
         foreach ($this->data as $key => $row) {
76 76
 
77
-            if (!$row)
77
+            if ( ! $row)
78 78
                 continue;
79 79
 
80 80
             $new_keys = call_user_func($indexGenerator, $row, $key);
81
-            if (!is_array($new_keys))
81
+            if ( ! is_array($new_keys))
82 82
                 $new_keys = [$new_keys];
83 83
 
84 84
             foreach ($new_keys as $new_key) {
85
-                if (!isset($out[ $new_key ])) {
86
-                    $out[ $new_key ] = [
85
+                if ( ! isset($out[$new_key])) {
86
+                    $out[$new_key] = [
87 87
                         $key => $row
88 88
                     ];
89 89
                 }
90 90
                 else {
91
-                    $out[ $new_key ][ $key ] = $row;
91
+                    $out[$new_key][$key] = $row;
92 92
                 }
93 93
             }
94 94
         }
@@ -112,7 +112,7 @@  discard block
 block discarded – undo
112 112
      */
113 113
     public function groupByTransformed(
114 114
         callable $indexGenerator,
115
-        callable $rowTransformer,      // todo check this behavior
115
+        callable $rowTransformer, // todo check this behavior
116 116
         callable $conflictResolver )
117 117
     {
118 118
         // The goal here is to remove the second parameter has it makes the
@@ -125,7 +125,7 @@  discard block
 block discarded – undo
125 125
         $out = [];
126 126
         foreach ($this->data as $key => $row) {
127 127
 
128
-            if (!$row)
128
+            if ( ! $row)
129 129
                 continue;
130 130
 
131 131
             $newIndex       = call_user_func($indexGenerator, $key, $row);
@@ -134,7 +134,7 @@  discard block
 block discarded – undo
134 134
                             ? call_user_func($rowTransformer, $row)
135 135
                             : $row;
136 136
 
137
-            if (!isset($out[$newIndex])) {
137
+            if ( ! isset($out[$newIndex])) {
138 138
                 $out[$newIndex] = $transformedRow;
139 139
             }
140 140
             else {
@@ -159,12 +159,12 @@  discard block
 block discarded – undo
159 159
      *                                       rows have the same index.
160 160
      * @return static
161 161
      */
162
-    public function mergeWith( $otherTable, callable $conflictResolver=null )
162
+    public function mergeWith($otherTable, callable $conflictResolver = null)
163 163
     {
164 164
         if (is_array($otherTable))
165 165
             $otherTable = new static($otherTable);
166 166
 
167
-        if (!$otherTable instanceof static) {
167
+        if ( ! $otherTable instanceof static) {
168 168
             self::throwUsageException(
169 169
                 '$otherTable must be an array or an instance of '.static::class.' instead of: '
170 170
                 .var_export($otherTable, true)
@@ -174,7 +174,7 @@  discard block
 block discarded – undo
174 174
         $out = $this->data;
175 175
         foreach ($otherTable->getArray() as $key => $row) {
176 176
 
177
-            if (!isset($out[$key])) {
177
+            if ( ! isset($out[$key])) {
178 178
                 $out[$key] = $row;
179 179
             }
180 180
             else {
@@ -202,7 +202,7 @@  discard block
 block discarded – undo
202 202
      * (same as self::mergeWith with the other table as $this)
203 203
      * @return static
204 204
      */
205
-    public function mergeIn( $otherTable, callable $conflictResolver=null )
205
+    public function mergeIn($otherTable, callable $conflictResolver = null)
206 206
     {
207 207
         $otherTable->mergeWith($this, $conflictResolver);
208 208
         return $this;
@@ -228,7 +228,7 @@  discard block
 block discarded – undo
228 228
      */
229 229
     public function each(callable $rowTransformer)
230 230
     {
231
-        $out  = [];
231
+        $out = [];
232 232
         foreach ($this->data as $key => $row) {
233 233
             $out[$key] = call_user_func_array(
234 234
                 $rowTransformer,
@@ -258,7 +258,7 @@  discard block
 block discarded – undo
258 258
      */
259 259
     public function renameColumns(array $old_to_new_names)
260 260
     {
261
-        $out  = [];
261
+        $out = [];
262 262
         foreach ($this->data as $key => $row) {
263 263
             try {
264 264
                 foreach ($old_to_new_names as $old_name => $new_name) {
@@ -267,7 +267,7 @@  discard block
 block discarded – undo
267 267
                 }
268 268
             }
269 269
             catch (\Exception $e) {
270
-                self::throwUsageException( $e->getMessage() );
270
+                self::throwUsageException($e->getMessage());
271 271
             }
272 272
 
273 273
             $out[$key] = $row;
@@ -319,20 +319,20 @@  discard block
 block discarded – undo
319 319
      *                            nor a static.
320 320
      * @return static       $this
321 321
      */
322
-    public function append($new_rows, callable $conflict_resolver=null)
322
+    public function append($new_rows, callable $conflict_resolver = null)
323 323
     {
324 324
         if ($new_rows instanceof static)
325 325
             $new_rows = $new_rows->getArray();
326 326
 
327
-        if (!is_array($new_rows)) {
327
+        if ( ! is_array($new_rows)) {
328 328
             $this->throwUsageException(
329
-                "\$new_rows parameter must be an array or an instance of " . __CLASS__
329
+                "\$new_rows parameter must be an array or an instance of ".__CLASS__
330 330
             );
331 331
         }
332 332
 
333
-        if (!$conflict_resolver) {
333
+        if ( ! $conflict_resolver) {
334 334
             // default conflict resolver: append with numeric key
335
-            $conflict_resolver = function (&$data, $existing_row, $confliuct_row, $key) {
335
+            $conflict_resolver = function(&$data, $existing_row, $confliuct_row, $key) {
336 336
                 $data[] = $confliuct_row;
337 337
             };
338 338
         }
@@ -364,7 +364,7 @@  discard block
 block discarded – undo
364 364
      *
365 365
      * @return static
366 366
      */
367
-    public function dimensionsAsColumns(array $columnNames, array $options=null)
367
+    public function dimensionsAsColumns(array $columnNames, array $options = null)
368 368
     {
369 369
         $out = $this->dimensionsAsColumns_recurser($this->data, $columnNames);
370 370
         return $this->returnConstant($out);
@@ -401,12 +401,12 @@  discard block
 block discarded – undo
401 401
      *      ],
402 402
      * ]
403 403
      */
404
-    protected function dimensionsAsColumns_recurser(array $data, $columnNames, $rowIdParts=[])
404
+    protected function dimensionsAsColumns_recurser(array $data, $columnNames, $rowIdParts = [])
405 405
     {
406 406
         $out = [];
407 407
         // if (!$columnNames)
408 408
             // return $data;
409
-        $no_more_column = !(bool) $columnNames;
409
+        $no_more_column = ! (bool) $columnNames;
410 410
 
411 411
         // If all the names have been given to the dimensions
412 412
         // we compile the index key of the row at the current level
@@ -430,7 +430,7 @@  discard block
 block discarded – undo
430 430
                     self::throwUsageException(
431 431
                          "Trying to populate a column '$name' that "
432 432
                         ."already exists with a different value "
433
-                        .var_export($data[$name], true). " => '$value'"
433
+                        .var_export($data[$name], true)." => '$value'"
434 434
                     );
435 435
                 }
436 436
                 $data[$name] = $value;
@@ -461,7 +461,7 @@  discard block
 block discarded – undo
461 461
             }
462 462
             else {
463 463
 
464
-                if (!isset($rows)) {
464
+                if ( ! isset($rows)) {
465 465
                     echo json_encode([
466 466
                         '$rowIdParts' => $rowIdParts,
467 467
                         '$row' => $row,
@@ -520,12 +520,12 @@  discard block
 block discarded – undo
520 520
 
521 521
             if (is_string($value) && array_key_exists($value, $row)) {
522 522
                 $part_name  .= $value;
523
-                $group_value = $row[ $value ];
523
+                $group_value = $row[$value];
524 524
             }
525 525
             elseif (is_callable($value)) {
526 526
 
527 527
                 if (is_string($value)) {
528
-                    $part_name  .= $value;
528
+                    $part_name .= $value;
529 529
                 }
530 530
                 // elseif (is_function($value)) {
531 531
                 elseif (is_object($value) && ($value instanceof Closure)) {
@@ -541,19 +541,19 @@  discard block
 block discarded – undo
541 541
                 ]);
542 542
             }
543 543
             elseif (is_int($value)) {
544
-                $part_name  .= $value ? : '0';
545
-                $group_value = $row[ $value ];
544
+                $part_name  .= $value ?: '0';
545
+                $group_value = $row[$value];
546 546
             }
547 547
             else {
548 548
                 self::throwUsageException(
549 549
                     'Bad value provided for groupBy id generation: '
550 550
                     .var_export($value, true)
551
-                    ."\n" . var_export($row, true)
551
+                    ."\n".var_export($row, true)
552 552
                 );
553 553
             }
554 554
 
555
-            if (!is_null($part_name))
556
-                $group_parts[ $part_name ] = $group_value;
555
+            if ( ! is_null($part_name))
556
+                $group_parts[$part_name] = $group_value;
557 557
         }
558 558
 
559 559
         // sort the groups by names (without it the same group could have multiple ids)
@@ -571,9 +571,9 @@  discard block
 block discarded – undo
571 571
     /**
572 572
      * Returns the first element of the array
573 573
      */
574
-    public function first($strict=false)
574
+    public function first($strict = false)
575 575
     {
576
-        if (!$this->count()) {
576
+        if ( ! $this->count()) {
577 577
             if ($strict)
578 578
                 throw new \ErrorException("No first element found in this array");
579 579
             else
@@ -593,9 +593,9 @@  discard block
 block discarded – undo
593 593
      *
594 594
      * @todo Preserve the offset
595 595
      */
596
-    public function last($strict=false)
596
+    public function last($strict = false)
597 597
     {
598
-        if (!$this->count()) {
598
+        if ( ! $this->count()) {
599 599
             if ($strict)
600 600
                 throw new \ErrorException("No last element found in this array");
601 601
             else
@@ -613,9 +613,9 @@  discard block
 block discarded – undo
613 613
     /**
614 614
      *
615 615
      */
616
-    public function firstKey($strict=false)
616
+    public function firstKey($strict = false)
617 617
     {
618
-        if (!$this->count()) {
618
+        if ( ! $this->count()) {
619 619
             if ($strict)
620 620
                 throw new \ErrorException("No last element found in this array");
621 621
             else
@@ -634,16 +634,16 @@  discard block
 block discarded – undo
634 634
     /**
635 635
      *
636 636
      */
637
-    public function lastKey($strict=false)
637
+    public function lastKey($strict = false)
638 638
     {
639
-        if (!$this->count()) {
639
+        if ( ! $this->count()) {
640 640
             if ($strict)
641 641
                 throw new \ErrorException("No last element found in this array");
642 642
             else
643 643
                 $lastKey = null;
644 644
         }
645 645
         else {
646
-            $key  = key($this->data);
646
+            $key = key($this->data);
647 647
             end($this->data);
648 648
             $lastKey = key($this->data);
649 649
             $this->move($key);
@@ -655,7 +655,7 @@  discard block
 block discarded – undo
655 655
     /**
656 656
      * Move the internal pointer of the array to the key given as parameter
657 657
      */
658
-    public function move($key, $strict=true)
658
+    public function move($key, $strict = true)
659 659
     {
660 660
         if (array_key_exists($key, $this->data)) {
661 661
             foreach ($this->data as $i => &$value) {
@@ -722,13 +722,13 @@  discard block
 block discarded – undo
722 722
      * @see http://php.net/manual/fr/function.var-dump.php
723 723
      * @todo Handle xdebug dump formatting
724 724
      */
725
-    public function dump($exit=false)
725
+    public function dump($exit = false)
726 726
     {
727 727
         $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
728 728
         $caller = $bt[0];
729 729
 
730 730
         var_export([
731
-            'location' => $caller['file'] . ':' . $caller['line'],
731
+            'location' => $caller['file'].':'.$caller['line'],
732 732
             'data'     => $this->data,
733 733
         ]);
734 734
 
@@ -745,7 +745,7 @@  discard block
 block discarded – undo
745 745
      * @todo move it to an Arrays class storing static methods
746 746
      */
747 747
     public static function replaceEntries(
748
-        array $array, callable $replacer, $max_depth=null
748
+        array $array, callable $replacer, $max_depth = null
749 749
     ) {
750 750
         foreach ($array as $key => &$row) {
751 751
             $arguments = [&$row, $key];
@@ -753,7 +753,7 @@  discard block
 block discarded – undo
753 753
 
754 754
             if (is_array($row) && $max_depth !== 0) { // allowing null to have no depth limit
755 755
                 $row = self::replaceEntries(
756
-                    $row, $replacer, $max_depth ? $max_depth-1 : $max_depth
756
+                    $row, $replacer, $max_depth ? $max_depth - 1 : $max_depth
757 757
                 );
758 758
             }
759 759
         }
@@ -769,7 +769,7 @@  discard block
 block discarded – undo
769 769
      *
770 770
      * @return static $this or a new static.
771 771
      */
772
-    public function extract($callback=null)
772
+    public function extract($callback = null)
773 773
     {
774 774
         if ($callback) {
775 775
 
@@ -777,7 +777,7 @@  discard block
 block discarded – undo
777 777
                 $callback = new \JClaveau\LogicalFilter\LogicalFilter($callback);
778 778
             }
779 779
 
780
-            if (!is_callable($callback)) {
780
+            if ( ! is_callable($callback)) {
781 781
                 $this->throwUsageException(
782 782
                     "\$callback must be a logical filter description array or a callable"
783 783
                     ." instead of "
@@ -789,7 +789,7 @@  discard block
 block discarded – undo
789 789
             foreach ($this->data as $key => $value) {
790 790
                 if ($callback($value, $key)) {
791 791
                     $out[$key] = $value;
792
-                    unset( $this->data[$key] );
792
+                    unset($this->data[$key]);
793 793
                 }
794 794
             }
795 795
         }
Please login to merge, or discard this patch.
src/Arrays.php 1 patch
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,14 +196,14 @@  discard block
 block discarded – undo
196 196
     public static function mergePreservingDistincts(
197 197
         $existing_row,
198 198
         $conflict_row
199
-    ){
199
+    ) {
200 200
         static::mustBeCountable($existing_row);
201 201
         static::mustBeCountable($conflict_row);
202 202
 
203 203
         $merge = static::mergeRecursiveCustom(
204 204
             $existing_row,
205 205
             $conflict_row,
206
-            function ($existing_value, $conflict_value, $column) {
206
+            function($existing_value, $conflict_value, $column) {
207 207
 
208 208
                 if ( ! $existing_value instanceof MergeBucket) {
209 209
                     $existing_value = MergeBucket::from()->push($existing_value);
@@ -232,7 +232,7 @@  discard block
 block discarded – undo
232 232
      * @param  array|Countable   $row
233 233
      * @param  array             $options : 'excluded_columns'
234 234
      */
235
-    public static function cleanMergeDuplicates($row, array $options=[])
235
+    public static function cleanMergeDuplicates($row, array $options = [])
236 236
     {
237 237
         static::mustBeCountable($row);
238 238
 
@@ -265,7 +265,7 @@  discard block
 block discarded – undo
265 265
      * @see mergePreservingDistincts()
266 266
      * @see cleanMergeDuplicates()
267 267
      */
268
-    public static function cleanMergeBuckets($row, array $options=[])
268
+    public static function cleanMergeBuckets($row, array $options = [])
269 269
     {
270 270
         static::mustBeCountable($row);
271 271
 
@@ -306,13 +306,13 @@  discard block
 block discarded – undo
306 306
                 $id = serialize($value);
307 307
             }
308 308
 
309
-            if (isset($ids[ $id ])) {
310
-                unset($array[ $key ]);
311
-                $ids[ $id ][] = $key;
309
+            if (isset($ids[$id])) {
310
+                unset($array[$key]);
311
+                $ids[$id][] = $key;
312 312
                 continue;
313 313
             }
314 314
 
315
-            $ids[ $id ] = [$key];
315
+            $ids[$id] = [$key];
316 316
         }
317 317
 
318 318
         return $array;
@@ -387,16 +387,16 @@  discard block
 block discarded – undo
387 387
             throw new \InvalidArgumentException(
388 388
                 "Different number of "
389 389
                 ." values and weights for weight mean calculation: \n"
390
-                .var_export($values,  true)."\n\n"
390
+                .var_export($values, true)."\n\n"
391 391
                 .var_export($weights, true)
392 392
             );
393 393
         }
394 394
 
395
-        if (!$values)
395
+        if ( ! $values)
396 396
             return null;
397 397
 
398
-        $weights_sum  = array_sum($weights);
399
-        if (!$weights_sum)
398
+        $weights_sum = array_sum($weights);
399
+        if ( ! $weights_sum)
400 400
             return 0;
401 401
 
402 402
         $weighted_sum = 0;
@@ -431,15 +431,15 @@  discard block
 block discarded – undo
431 431
      */
432 432
     public static function mustBeCountable($value)
433 433
     {
434
-        if ( ! static::isCountable($value) ) {
434
+        if ( ! static::isCountable($value)) {
435 435
             $exception = new \InvalidArgumentException(
436 436
                 "A value must be Countable instead of: \n"
437 437
                 .var_export($value, true)
438 438
             );
439 439
 
440 440
             // The true location of the throw is still available through the backtrace
441
-            $trace_location  = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
442
-            $reflectionClass = new \ReflectionClass( get_class($exception) );
441
+            $trace_location  = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
442
+            $reflectionClass = new \ReflectionClass(get_class($exception));
443 443
 
444 444
             //file
445 445
             if (isset($trace_location['file'])) {
Please login to merge, or discard this patch.