@@ -96,8 +96,7 @@ |
||
| 96 | 96 | { |
| 97 | 97 | if ($this->isConstant) { |
| 98 | 98 | return new static($out); |
| 99 | - } |
|
| 100 | - else { |
|
| 99 | + } else { |
|
| 101 | 100 | $this->data = $out; |
| 102 | 101 | return $this; |
| 103 | 102 | } |
@@ -26,8 +26,9 @@ discard block |
||
| 26 | 26 | $out = []; |
| 27 | 27 | foreach ($this->data as $key => &$row) { |
| 28 | 28 | |
| 29 | - if ($index_key) |
|
| 30 | - $key = $row[$index_key]; |
|
| 29 | + if ($index_key) { |
|
| 30 | + $key = $row[$index_key]; |
|
| 31 | + } |
|
| 31 | 32 | |
| 32 | 33 | if (is_array($column_names)) { |
| 33 | 34 | $out[$key] = []; |
@@ -42,20 +43,16 @@ discard block |
||
| 42 | 43 | |
| 43 | 44 | $out[$key][$column_name] = $row[$column_name]; |
| 44 | 45 | } |
| 45 | - } |
|
| 46 | - else { |
|
| 46 | + } else { |
|
| 47 | 47 | // This avoids issues with isset, array_key_exists and |
| 48 | 48 | // null entries on objects and arrays. |
| 49 | 49 | if ($row instanceof static) { |
| 50 | 50 | $keys = $row->copy()->keys(); |
| 51 | - } |
|
| 52 | - elseif (is_array($row)) { |
|
| 51 | + } elseif (is_array($row)) { |
|
| 53 | 52 | $keys = array_keys($row); |
| 54 | - } |
|
| 55 | - elseif (is_scalar($row)) { |
|
| 53 | + } elseif (is_scalar($row)) { |
|
| 56 | 54 | $keys = []; |
| 57 | - } |
|
| 58 | - else { |
|
| 55 | + } else { |
|
| 59 | 56 | // todo : handle other classes supporting ArrayAccess? |
| 60 | 57 | $keys = []; |
| 61 | 58 | } |
@@ -101,16 +98,17 @@ discard block |
||
| 101 | 98 | */ |
| 102 | 99 | public function min($default_value=null) |
| 103 | 100 | { |
| 104 | - if ($this->isEmpty() && $default_value !== null) |
|
| 105 | - return $default_value; |
|
| 101 | + if ($this->isEmpty() && $default_value !== null) { |
|
| 102 | + return $default_value; |
|
| 103 | + } |
|
| 106 | 104 | |
| 107 | 105 | try { |
| 108 | 106 | return min($this->data); |
| 109 | - } |
|
| 110 | - catch (\Exception $e) { |
|
| 107 | + } catch (\Exception $e) { |
|
| 111 | 108 | $message = $e->getMessage(); |
| 112 | - if ($e->getMessage() == 'min(): Array must contain at least one element') |
|
| 113 | - $message .= ' or you can set a default value as parameter'; |
|
| 109 | + if ($e->getMessage() == 'min(): Array must contain at least one element') { |
|
| 110 | + $message .= ' or you can set a default value as parameter'; |
|
| 111 | + } |
|
| 114 | 112 | |
| 115 | 113 | $this->throwUsageException($message); |
| 116 | 114 | } |
@@ -124,16 +122,17 @@ discard block |
||
| 124 | 122 | */ |
| 125 | 123 | public function max($default_value=null) |
| 126 | 124 | { |
| 127 | - if ($this->isEmpty() && $default_value !== null) |
|
| 128 | - return $default_value; |
|
| 125 | + if ($this->isEmpty() && $default_value !== null) { |
|
| 126 | + return $default_value; |
|
| 127 | + } |
|
| 129 | 128 | |
| 130 | 129 | try { |
| 131 | 130 | return max($this->data); |
| 132 | - } |
|
| 133 | - catch (\Exception $e) { |
|
| 131 | + } catch (\Exception $e) { |
|
| 134 | 132 | $message = $e->getMessage(); |
| 135 | - if ($e->getMessage() == 'max(): Array must contain at least one element') |
|
| 136 | - $message .= ' or you can set a default value as parameter'; |
|
| 133 | + if ($e->getMessage() == 'max(): Array must contain at least one element') { |
|
| 134 | + $message .= ' or you can set a default value as parameter'; |
|
| 135 | + } |
|
| 137 | 136 | |
| 138 | 137 | $this->throwUsageException($message); |
| 139 | 138 | } |
@@ -200,16 +199,15 @@ discard block |
||
| 200 | 199 | if (PHP_VERSION_ID >= 50600) { |
| 201 | 200 | $out = array_filter($this->data, $callback, |
| 202 | 201 | ARRAY_FILTER_USE_BOTH); |
| 203 | - } |
|
| 204 | - else { |
|
| 202 | + } else { |
|
| 205 | 203 | $out = $this->data; |
| 206 | 204 | foreach ($out as $key => $value) { |
| 207 | - if (!$callback($value, $key)) |
|
| 208 | - unset( $out[$key] ); |
|
| 205 | + if (!$callback($value, $key)) { |
|
| 206 | + unset( $out[$key] ); |
|
| 207 | + } |
|
| 209 | 208 | } |
| 210 | 209 | } |
| 211 | - } |
|
| 212 | - else { |
|
| 210 | + } else { |
|
| 213 | 211 | $out = array_filter($this->data); |
| 214 | 212 | } |
| 215 | 213 | |
@@ -242,8 +240,9 @@ discard block |
||
| 242 | 240 | $out = []; |
| 243 | 241 | foreach ($this->data as $i => $data_item) { |
| 244 | 242 | |
| 245 | - if ($data_item == $item_to_remove) |
|
| 246 | - continue; |
|
| 243 | + if ($data_item == $item_to_remove) { |
|
| 244 | + continue; |
|
| 245 | + } |
|
| 247 | 246 | |
| 248 | 247 | $out[$i] = $data_item; |
| 249 | 248 | } |
@@ -258,11 +257,13 @@ discard block |
||
| 258 | 257 | */ |
| 259 | 258 | public function intersectKey($intersect_with) |
| 260 | 259 | { |
| 261 | - if (!$this->argumentIsArrayOrArrayObject($intersect_with)) |
|
| 262 | - self::throwUsageException("First argument must be an array or a ".static::class."."); |
|
| 260 | + if (!$this->argumentIsArrayOrArrayObject($intersect_with)) { |
|
| 261 | + self::throwUsageException("First argument must be an array or a ".static::class."."); |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | - if ($intersect_with instanceof static) |
|
| 265 | - $intersect_with = $intersect_with->getArray(); |
|
| 264 | + if ($intersect_with instanceof static) { |
|
| 265 | + $intersect_with = $intersect_with->getArray(); |
|
| 266 | + } |
|
| 266 | 267 | |
| 267 | 268 | $out = array_intersect_key($this->data, $intersect_with); |
| 268 | 269 | |
@@ -351,8 +352,9 @@ discard block |
||
| 351 | 352 | */ |
| 352 | 353 | public function diff($compare_with, $check_keys=false, $strict_comparison=false) |
| 353 | 354 | { |
| 354 | - if (!$this->argumentIsArrayOrArrayObject($compare_with)) |
|
| 355 | - self::throwUsageException("First argument must be an iterable"); |
|
| 355 | + if (!$this->argumentIsArrayOrArrayObject($compare_with)) { |
|
| 356 | + self::throwUsageException("First argument must be an iterable"); |
|
| 357 | + } |
|
| 356 | 358 | |
| 357 | 359 | $kept_values = $this->data; |
| 358 | 360 | |
@@ -368,21 +370,18 @@ discard block |
||
| 368 | 370 | && $kept_value === $compared_value ) { |
| 369 | 371 | $is_equal = true; |
| 370 | 372 | } |
| 371 | - } |
|
| 372 | - else { |
|
| 373 | + } else { |
|
| 373 | 374 | if ( $kept_key == $compared_key |
| 374 | 375 | && $kept_value == $compared_value ) { |
| 375 | 376 | $is_equal = true; |
| 376 | 377 | } |
| 377 | 378 | } |
| 378 | - } |
|
| 379 | - else { |
|
| 379 | + } else { |
|
| 380 | 380 | if ($strict_comparison) { |
| 381 | 381 | if ($kept_value === $compared_value) { |
| 382 | 382 | $is_equal = true; |
| 383 | 383 | } |
| 384 | - } |
|
| 385 | - else { |
|
| 384 | + } else { |
|
| 386 | 385 | if ($kept_value == $compared_value) { |
| 387 | 386 | $is_equal = true; |
| 388 | 387 | } |
@@ -421,11 +420,13 @@ discard block |
||
| 421 | 420 | */ |
| 422 | 421 | public function fillKeys($keys, $value) |
| 423 | 422 | { |
| 424 | - if (!$this->argumentIsArrayOrArrayObject($keys)) |
|
| 425 | - self::throwUsageException("First argument must be an array or a ".static::class."."); |
|
| 423 | + if (!$this->argumentIsArrayOrArrayObject($keys)) { |
|
| 424 | + self::throwUsageException("First argument must be an array or a ".static::class."."); |
|
| 425 | + } |
|
| 426 | 426 | |
| 427 | - if ($keys instanceof static) |
|
| 428 | - $keys = $keys->getArray(); |
|
| 427 | + if ($keys instanceof static) { |
|
| 428 | + $keys = $keys->getArray(); |
|
| 429 | + } |
|
| 429 | 430 | |
| 430 | 431 | $out = array_fill_keys($keys, $value); |
| 431 | 432 | return $this->returnConstant($out); |
@@ -470,8 +471,9 @@ discard block |
||
| 470 | 471 | callable $step_generator, |
| 471 | 472 | $max_interations_count=1000 |
| 472 | 473 | ){ |
| 473 | - if ($max_interations_count < 0) |
|
| 474 | - throw new \InvalidArgumentException("$maximum_interations_count must be positive"); |
|
| 474 | + if ($max_interations_count < 0) { |
|
| 475 | + throw new \InvalidArgumentException("$maximum_interations_count must be positive"); |
|
| 476 | + } |
|
| 475 | 477 | |
| 476 | 478 | $out = []; |
| 477 | 479 | $previous_step_value = null; |
@@ -488,16 +490,14 @@ discard block |
||
| 488 | 490 | |
| 489 | 491 | if ($current_step_key === null) { |
| 490 | 492 | $out[] = $current_step_value; |
| 491 | - } |
|
| 492 | - elseif (!is_int($current_step_key)) { |
|
| 493 | + } elseif (!is_int($current_step_key)) { |
|
| 493 | 494 | // Set the local as en_US tu have floats formatted with |
| 494 | 495 | // "." as separator |
| 495 | 496 | // TODO : could it be useful for dates to? |
| 496 | 497 | $current_locale = setlocale(LC_NUMERIC, 'en_US'); |
| 497 | 498 | $out[(string) $current_step_key] = $current_step_value; |
| 498 | 499 | setlocale(LC_NUMERIC, $current_locale); |
| 499 | - } |
|
| 500 | - else { |
|
| 500 | + } else { |
|
| 501 | 501 | $out[$current_step_key] = $current_step_value; |
| 502 | 502 | } |
| 503 | 503 | |
@@ -508,8 +508,7 @@ discard block |
||
| 508 | 508 | if ((string) $current_step_key == (string) $end_key) { |
| 509 | 509 | break; |
| 510 | 510 | } |
| 511 | - } |
|
| 512 | - elseif ($current_step_key == $end_key) { |
|
| 511 | + } elseif ($current_step_key == $end_key) { |
|
| 513 | 512 | break; |
| 514 | 513 | } |
| 515 | 514 | |
@@ -529,10 +528,11 @@ discard block |
||
| 529 | 528 | */ |
| 530 | 529 | public function keys($search_value=null, $strict=false) |
| 531 | 530 | { |
| 532 | - if ($search_value) |
|
| 533 | - return array_keys($this->data, $search_value, $strict); |
|
| 534 | - else |
|
| 535 | - return array_keys($this->data); |
|
| 531 | + if ($search_value) { |
|
| 532 | + return array_keys($this->data, $search_value, $strict); |
|
| 533 | + } else { |
|
| 534 | + return array_keys($this->data); |
|
| 535 | + } |
|
| 536 | 536 | } |
| 537 | 537 | |
| 538 | 538 | /** |
@@ -640,8 +640,9 @@ discard block |
||
| 640 | 640 | |
| 641 | 641 | if ($callback === null) { |
| 642 | 642 | $callback = function($a, $b) { |
| 643 | - if ($a == $b) |
|
| 644 | - return 0; |
|
| 643 | + if ($a == $b) { |
|
| 644 | + return 0; |
|
| 645 | + } |
|
| 645 | 646 | |
| 646 | 647 | return $a > $b ? -1 : 1; |
| 647 | 648 | }; |
@@ -649,8 +650,9 @@ discard block |
||
| 649 | 650 | |
| 650 | 651 | $arguments = Arrays::merge( [&$data], [$callback] ); |
| 651 | 652 | |
| 652 | - if ( ! call_user_func_array('uasort', $arguments) ) |
|
| 653 | - throw new \ErrorException('Unable to apply usort'); |
|
| 653 | + if ( ! call_user_func_array('uasort', $arguments) ) { |
|
| 654 | + throw new \ErrorException('Unable to apply usort'); |
|
| 655 | + } |
|
| 654 | 656 | |
| 655 | 657 | return $this->returnConstant($data); |
| 656 | 658 | } |
@@ -672,8 +674,9 @@ discard block |
||
| 672 | 674 | |
| 673 | 675 | $arguments = Arrays::merge( [&$data], [$callback] ); |
| 674 | 676 | |
| 675 | - if ( ! call_user_func_array('uksort', $arguments) ) |
|
| 676 | - throw new \ErrorException('Unable to apply uksort'); |
|
| 677 | + if ( ! call_user_func_array('uksort', $arguments) ) { |
|
| 678 | + throw new \ErrorException('Unable to apply uksort'); |
|
| 679 | + } |
|
| 677 | 680 | |
| 678 | 681 | return $this->returnConstant($data); |
| 679 | 682 | } |
@@ -91,8 +91,9 @@ discard block |
||
| 91 | 91 | { |
| 92 | 92 | $column_name = $key_column_names->shift(); |
| 93 | 93 | |
| 94 | - if (!$column_name) |
|
| 95 | - return $rows; |
|
| 94 | + if (!$column_name) { |
|
| 95 | + return $rows; |
|
| 96 | + } |
|
| 96 | 97 | |
| 97 | 98 | // $out = new static; |
| 98 | 99 | $out = []; |
@@ -107,10 +108,10 @@ discard block |
||
| 107 | 108 | |
| 108 | 109 | foreach ($sub_rows as $row) { |
| 109 | 110 | try { |
| 110 | - if (is_array($row)) |
|
| 111 | - $row[$column_name] = $key; |
|
| 112 | - } |
|
| 113 | - catch (\Exception $e) { |
|
| 111 | + if (is_array($row)) { |
|
| 112 | + $row[$column_name] = $key; |
|
| 113 | + } |
|
| 114 | + } catch (\Exception $e) { |
|
| 114 | 115 | echo json_encode($column_name); |
| 115 | 116 | echo json_encode($key); |
| 116 | 117 | echo json_encode($e); |
@@ -118,13 +119,12 @@ discard block |
||
| 118 | 119 | } |
| 119 | 120 | $out[] = $row; |
| 120 | 121 | } |
| 121 | - } |
|
| 122 | - else { |
|
| 122 | + } else { |
|
| 123 | 123 | try { |
| 124 | - if (is_array($sub_rows)) |
|
| 125 | - $sub_rows[$column_name] = $key; |
|
| 126 | - } |
|
| 127 | - catch (\Exception $e) { |
|
| 124 | + if (is_array($sub_rows)) { |
|
| 125 | + $sub_rows[$column_name] = $key; |
|
| 126 | + } |
|
| 127 | + } catch (\Exception $e) { |
|
| 128 | 128 | echo json_encode($sub_rows); |
| 129 | 129 | echo json_encode($column_name); |
| 130 | 130 | echo json_encode($key); |
@@ -299,8 +299,9 @@ discard block |
||
| 299 | 299 | ), |
| 300 | 300 | ); |
| 301 | 301 | |
| 302 | - if ($expected != $tableized->getArray()) |
|
| 303 | - throw new \Exception("test failed"); |
|
| 302 | + if ($expected != $tableized->getArray()) { |
|
| 303 | + throw new \Exception("test failed"); |
|
| 304 | + } |
|
| 304 | 305 | |
| 305 | 306 | echo 'Helper_Tabe->tableize tested successfully'; |
| 306 | 307 | } |
@@ -327,13 +328,15 @@ discard block |
||
| 327 | 328 | */ |
| 328 | 329 | private function piramidize_aux($key_column_names, $rows) |
| 329 | 330 | { |
| 330 | - if (!is_array($rows)) |
|
| 331 | - return $rows; |
|
| 331 | + if (!is_array($rows)) { |
|
| 332 | + return $rows; |
|
| 333 | + } |
|
| 332 | 334 | |
| 333 | 335 | $column_name = $key_column_names->shift(); |
| 334 | 336 | |
| 335 | - if (!$column_name) |
|
| 336 | - return $rows; |
|
| 337 | + if (!$column_name) { |
|
| 338 | + return $rows; |
|
| 339 | + } |
|
| 337 | 340 | |
| 338 | 341 | // $out = new static; |
| 339 | 342 | $out = []; |
@@ -348,12 +351,12 @@ discard block |
||
| 348 | 351 | unset($row[$column_name]); |
| 349 | 352 | |
| 350 | 353 | if ($key_column_names->count()) { |
| 351 | - if (!isset($out[$key])) |
|
| 352 | - $out[$key] = []; |
|
| 354 | + if (!isset($out[$key])) { |
|
| 355 | + $out[$key] = []; |
|
| 356 | + } |
|
| 353 | 357 | |
| 354 | 358 | $out[$key][] = $row; |
| 355 | - } |
|
| 356 | - else { |
|
| 359 | + } else { |
|
| 357 | 360 | $out[$key] = $row; |
| 358 | 361 | } |
| 359 | 362 | } |
@@ -529,8 +532,9 @@ discard block |
||
| 529 | 532 | // echo var_export($values->getArray()); |
| 530 | 533 | // $values->dumpJson(true); |
| 531 | 534 | |
| 532 | - if ($expected != $values->getArray()) |
|
| 533 | - throw new \Exception("test failed"); |
|
| 535 | + if ($expected != $values->getArray()) { |
|
| 536 | + throw new \Exception("test failed"); |
|
| 537 | + } |
|
| 534 | 538 | |
| 535 | 539 | echo 'Helper_Tabe->piramidize tested successfully'; |
| 536 | 540 | } |
@@ -18,8 +18,7 @@ discard block |
||
| 18 | 18 | { |
| 19 | 19 | if (is_null($offset)) { |
| 20 | 20 | $this->data[] = $value; |
| 21 | - } |
|
| 22 | - else { |
|
| 21 | + } else { |
|
| 23 | 22 | $this->data[$offset] = $value; |
| 24 | 23 | } |
| 25 | 24 | } |
@@ -31,8 +30,7 @@ discard block |
||
| 31 | 30 | { |
| 32 | 31 | try { |
| 33 | 32 | return isset($this->data[$offset]); |
| 34 | - } |
|
| 35 | - catch (\Exception $e) { |
|
| 33 | + } catch (\Exception $e) { |
|
| 36 | 34 | if (property_exists($this, 'defaultRowGenerator')) { |
| 37 | 35 | return true; |
| 38 | 36 | } |
@@ -77,8 +75,7 @@ discard block |
||
| 77 | 75 | // support but wouldn't throw exception if it doesn't exist |
| 78 | 76 | $returnValue = &$this->data[$offset]; |
| 79 | 77 | return $returnValue; |
| 80 | - } |
|
| 81 | - catch (\Exception $e) { |
|
| 78 | + } catch (\Exception $e) { |
|
| 82 | 79 | // here we simply move the Exception location at the one |
| 83 | 80 | // of the caller as the isset() method is called at its |
| 84 | 81 | // location. |
@@ -158,10 +155,11 @@ discard block |
||
| 158 | 155 | */ |
| 159 | 156 | protected function generateDefaultRow($offset) |
| 160 | 157 | { |
| 161 | - if (property_exists($this, 'defaultRow')) |
|
| 162 | - return $this->defaultRow; |
|
| 163 | - elseif (property_exists($this, 'defaultRowGenerator')) |
|
| 164 | - return call_user_func($this->defaultRowGenerator, $offset); |
|
| 158 | + if (property_exists($this, 'defaultRow')) { |
|
| 159 | + return $this->defaultRow; |
|
| 160 | + } elseif (property_exists($this, 'defaultRowGenerator')) { |
|
| 161 | + return call_user_func($this->defaultRowGenerator, $offset); |
|
| 162 | + } |
|
| 165 | 163 | } |
| 166 | 164 | |
| 167 | 165 | /**/ |
@@ -26,18 +26,19 @@ discard block |
||
| 26 | 26 | $out = []; |
| 27 | 27 | foreach ($this->data as $key => $row) { |
| 28 | 28 | |
| 29 | - if (!$row) |
|
| 30 | - continue; |
|
| 29 | + if (!$row) { |
|
| 30 | + continue; |
|
| 31 | + } |
|
| 31 | 32 | |
| 32 | 33 | $newIndexes = call_user_func($indexGenerator, $key, $row); |
| 33 | - if (!is_array($newIndexes)) |
|
| 34 | - $newIndexes = [$newIndexes]; |
|
| 34 | + if (!is_array($newIndexes)) { |
|
| 35 | + $newIndexes = [$newIndexes]; |
|
| 36 | + } |
|
| 35 | 37 | |
| 36 | 38 | foreach ($newIndexes as $newIndex) { |
| 37 | 39 | if (!isset($out[$newIndex])) { |
| 38 | 40 | $out[$newIndex] = $row; |
| 39 | - } |
|
| 40 | - else { |
|
| 41 | + } else { |
|
| 41 | 42 | if ($conflictResolver === null) { |
| 42 | 43 | self::throwUsageException( |
| 43 | 44 | "A 'group by' provoking a conflict" |
@@ -74,20 +75,21 @@ discard block |
||
| 74 | 75 | $out = []; |
| 75 | 76 | foreach ($this->data as $key => $row) { |
| 76 | 77 | |
| 77 | - if (!$row) |
|
| 78 | - continue; |
|
| 78 | + if (!$row) { |
|
| 79 | + continue; |
|
| 80 | + } |
|
| 79 | 81 | |
| 80 | 82 | $new_keys = call_user_func($indexGenerator, $row, $key); |
| 81 | - if (!is_array($new_keys)) |
|
| 82 | - $new_keys = [$new_keys]; |
|
| 83 | + if (!is_array($new_keys)) { |
|
| 84 | + $new_keys = [$new_keys]; |
|
| 85 | + } |
|
| 83 | 86 | |
| 84 | 87 | foreach ($new_keys as $new_key) { |
| 85 | 88 | if (!isset($out[ $new_key ])) { |
| 86 | 89 | $out[ $new_key ] = [ |
| 87 | 90 | $key => $row |
| 88 | 91 | ]; |
| 89 | - } |
|
| 90 | - else { |
|
| 92 | + } else { |
|
| 91 | 93 | $out[ $new_key ][ $key ] = $row; |
| 92 | 94 | } |
| 93 | 95 | } |
@@ -160,8 +162,9 @@ discard block |
||
| 160 | 162 | $out = []; |
| 161 | 163 | foreach ($this->data as $key => $row) { |
| 162 | 164 | |
| 163 | - if (!$row) |
|
| 164 | - continue; |
|
| 165 | + if (!$row) { |
|
| 166 | + continue; |
|
| 167 | + } |
|
| 165 | 168 | |
| 166 | 169 | $newIndex = call_user_func($indexGenerator, $key, $row); |
| 167 | 170 | |
@@ -171,8 +174,7 @@ discard block |
||
| 171 | 174 | |
| 172 | 175 | if (!isset($out[$newIndex])) { |
| 173 | 176 | $out[$newIndex] = $transformedRow; |
| 174 | - } |
|
| 175 | - else { |
|
| 177 | + } else { |
|
| 176 | 178 | $out[$newIndex] = call_user_func( |
| 177 | 179 | $conflictResolver, |
| 178 | 180 | $newIndex, |
@@ -196,8 +198,9 @@ discard block |
||
| 196 | 198 | */ |
| 197 | 199 | public function mergeWith( $otherTable, callable $conflictResolver=null ) |
| 198 | 200 | { |
| 199 | - if (is_array($otherTable)) |
|
| 200 | - $otherTable = new static($otherTable); |
|
| 201 | + if (is_array($otherTable)) { |
|
| 202 | + $otherTable = new static($otherTable); |
|
| 203 | + } |
|
| 201 | 204 | |
| 202 | 205 | if (!$otherTable instanceof static) { |
| 203 | 206 | self::throwUsageException( |
@@ -211,10 +214,10 @@ discard block |
||
| 211 | 214 | |
| 212 | 215 | if (!isset($out[$key])) { |
| 213 | 216 | $out[$key] = $row; |
| 214 | - } |
|
| 215 | - else { |
|
| 216 | - if ($conflictResolver === null) |
|
| 217 | - self::throwUsageException('No conflict resolver for a merge provoking one'); |
|
| 217 | + } else { |
|
| 218 | + if ($conflictResolver === null) { |
|
| 219 | + self::throwUsageException('No conflict resolver for a merge provoking one'); |
|
| 220 | + } |
|
| 218 | 221 | |
| 219 | 222 | $arguments = [ |
| 220 | 223 | &$key, |
@@ -300,8 +303,7 @@ discard block |
||
| 300 | 303 | $row[$new_name] = $row[$old_name]; |
| 301 | 304 | unset($row[$old_name]); |
| 302 | 305 | } |
| 303 | - } |
|
| 304 | - catch (\Exception $e) { |
|
| 306 | + } catch (\Exception $e) { |
|
| 305 | 307 | self::throwUsageException( $e->getMessage() ); |
| 306 | 308 | } |
| 307 | 309 | |
@@ -322,17 +324,19 @@ discard block |
||
| 322 | 324 | public function limit() |
| 323 | 325 | { |
| 324 | 326 | $arguments = func_get_args(); |
| 325 | - if (count($arguments) == 1 && is_numeric($arguments[0])) |
|
| 326 | - $max = $arguments[0]; |
|
| 327 | - else |
|
| 328 | - self::throwUsageException("Bad arguments type and count for limit()"); |
|
| 327 | + if (count($arguments) == 1 && is_numeric($arguments[0])) { |
|
| 328 | + $max = $arguments[0]; |
|
| 329 | + } else { |
|
| 330 | + self::throwUsageException("Bad arguments type and count for limit()"); |
|
| 331 | + } |
|
| 329 | 332 | |
| 330 | 333 | $out = []; |
| 331 | 334 | $count = 0; |
| 332 | 335 | foreach ($this->data as $key => $row) { |
| 333 | 336 | |
| 334 | - if ($max <= $count) |
|
| 335 | - break; |
|
| 337 | + if ($max <= $count) { |
|
| 338 | + break; |
|
| 339 | + } |
|
| 336 | 340 | |
| 337 | 341 | $out[$key] = $row; |
| 338 | 342 | |
@@ -356,8 +360,9 @@ discard block |
||
| 356 | 360 | */ |
| 357 | 361 | public function append($new_rows, callable $conflict_resolver=null) |
| 358 | 362 | { |
| 359 | - if ($new_rows instanceof static) |
|
| 360 | - $new_rows = $new_rows->getArray(); |
|
| 363 | + if ($new_rows instanceof static) { |
|
| 364 | + $new_rows = $new_rows->getArray(); |
|
| 365 | + } |
|
| 361 | 366 | |
| 362 | 367 | if (!is_array($new_rows)) { |
| 363 | 368 | $this->throwUsageException( |
@@ -382,8 +387,7 @@ discard block |
||
| 382 | 387 | ]; |
| 383 | 388 | |
| 384 | 389 | call_user_func_array($conflict_resolver, $arguments); |
| 385 | - } |
|
| 386 | - else { |
|
| 390 | + } else { |
|
| 387 | 391 | $this->data[$key] = $new_row; |
| 388 | 392 | } |
| 389 | 393 | } |
@@ -493,8 +497,7 @@ discard block |
||
| 493 | 497 | foreach ($rows as $row_id => $joined_row) { |
| 494 | 498 | $out[$row_id] = $joined_row; |
| 495 | 499 | } |
| 496 | - } |
|
| 497 | - else { |
|
| 500 | + } else { |
|
| 498 | 501 | |
| 499 | 502 | if (!isset($rows)) { |
| 500 | 503 | echo json_encode([ |
@@ -504,8 +507,9 @@ discard block |
||
| 504 | 507 | exit; |
| 505 | 508 | } |
| 506 | 509 | |
| 507 | - foreach ($rowIdParts as $rowIdPartName => $rowIdPartValue) |
|
| 508 | - $row[$rowIdPartName] = $rowIdPartValue; |
|
| 510 | + foreach ($rowIdParts as $rowIdPartName => $rowIdPartValue) { |
|
| 511 | + $row[$rowIdPartName] = $rowIdPartValue; |
|
| 512 | + } |
|
| 509 | 513 | |
| 510 | 514 | $indexParts = []; |
| 511 | 515 | foreach ($rowIdParts as $name => $value) { |
@@ -556,8 +560,7 @@ discard block |
||
| 556 | 560 | if (is_string($value) && array_key_exists($value, $row)) { |
| 557 | 561 | $part_name .= $value; |
| 558 | 562 | $group_value = $row[ $value ]; |
| 559 | - } |
|
| 560 | - elseif (is_callable($value)) { |
|
| 563 | + } elseif (is_callable($value)) { |
|
| 561 | 564 | |
| 562 | 565 | if (is_string($value)) { |
| 563 | 566 | $part_name .= $value; |
@@ -566,20 +569,17 @@ discard block |
||
| 566 | 569 | elseif (is_object($value) && ($value instanceof Closure)) { |
| 567 | 570 | $part_name .= 'unnamed-closure-' |
| 568 | 571 | . hash('crc32b', var_export($value, true)); |
| 569 | - } |
|
| 570 | - elseif (is_array($value)) { |
|
| 572 | + } elseif (is_array($value)) { |
|
| 571 | 573 | $part_name .= implode('::', $value); |
| 572 | 574 | } |
| 573 | 575 | |
| 574 | 576 | $group_value = call_user_func_array($value, [ |
| 575 | 577 | $row, &$part_name |
| 576 | 578 | ]); |
| 577 | - } |
|
| 578 | - elseif (is_int($value)) { |
|
| 579 | + } elseif (is_int($value)) { |
|
| 579 | 580 | $part_name .= $value ? : '0'; |
| 580 | 581 | $group_value = $row[ $value ]; |
| 581 | - } |
|
| 582 | - else { |
|
| 582 | + } else { |
|
| 583 | 583 | self::throwUsageException( |
| 584 | 584 | 'Bad value provided for groupBy id generation: ' |
| 585 | 585 | .var_export($value, true) |
@@ -587,8 +587,9 @@ discard block |
||
| 587 | 587 | ); |
| 588 | 588 | } |
| 589 | 589 | |
| 590 | - if (!is_null($part_name)) |
|
| 591 | - $group_parts[ $part_name ] = $group_value; |
|
| 590 | + if (!is_null($part_name)) { |
|
| 591 | + $group_parts[ $part_name ] = $group_value; |
|
| 592 | + } |
|
| 592 | 593 | } |
| 593 | 594 | |
| 594 | 595 | // sort the groups by names (without it the same group could have multiple ids) |
@@ -609,12 +610,12 @@ discard block |
||
| 609 | 610 | public function first($strict=false) |
| 610 | 611 | { |
| 611 | 612 | if (!$this->count()) { |
| 612 | - if ($strict) |
|
| 613 | - throw new \ErrorException("No first element found in this array"); |
|
| 614 | - else |
|
| 615 | - $first = null; |
|
| 616 | - } |
|
| 617 | - else { |
|
| 613 | + if ($strict) { |
|
| 614 | + throw new \ErrorException("No first element found in this array"); |
|
| 615 | + } else { |
|
| 616 | + $first = null; |
|
| 617 | + } |
|
| 618 | + } else { |
|
| 618 | 619 | $key = key($this->data); |
| 619 | 620 | $first = reset($this->data); |
| 620 | 621 | $this->move($key); |
@@ -631,12 +632,12 @@ discard block |
||
| 631 | 632 | public function last($strict=false) |
| 632 | 633 | { |
| 633 | 634 | if (!$this->count()) { |
| 634 | - if ($strict) |
|
| 635 | - throw new \ErrorException("No last element found in this array"); |
|
| 636 | - else |
|
| 637 | - $last = null; |
|
| 638 | - } |
|
| 639 | - else { |
|
| 635 | + if ($strict) { |
|
| 636 | + throw new \ErrorException("No last element found in this array"); |
|
| 637 | + } else { |
|
| 638 | + $last = null; |
|
| 639 | + } |
|
| 640 | + } else { |
|
| 640 | 641 | $key = key($this->data); |
| 641 | 642 | $last = end($this->data); |
| 642 | 643 | $this->move($key); |
@@ -651,12 +652,12 @@ discard block |
||
| 651 | 652 | public function firstKey($strict=false) |
| 652 | 653 | { |
| 653 | 654 | if (!$this->count()) { |
| 654 | - if ($strict) |
|
| 655 | - throw new \ErrorException("No last element found in this array"); |
|
| 656 | - else |
|
| 657 | - $firstKey = null; |
|
| 658 | - } |
|
| 659 | - else { |
|
| 655 | + if ($strict) { |
|
| 656 | + throw new \ErrorException("No last element found in this array"); |
|
| 657 | + } else { |
|
| 658 | + $firstKey = null; |
|
| 659 | + } |
|
| 660 | + } else { |
|
| 660 | 661 | $key = key($this->data); |
| 661 | 662 | reset($this->data); |
| 662 | 663 | $firstKey = key($this->data); |
@@ -672,12 +673,12 @@ discard block |
||
| 672 | 673 | public function lastKey($strict=false) |
| 673 | 674 | { |
| 674 | 675 | if (!$this->count()) { |
| 675 | - if ($strict) |
|
| 676 | - throw new \ErrorException("No last element found in this array"); |
|
| 677 | - else |
|
| 678 | - $lastKey = null; |
|
| 679 | - } |
|
| 680 | - else { |
|
| 676 | + if ($strict) { |
|
| 677 | + throw new \ErrorException("No last element found in this array"); |
|
| 678 | + } else { |
|
| 679 | + $lastKey = null; |
|
| 680 | + } |
|
| 681 | + } else { |
|
| 681 | 682 | $key = key($this->data); |
| 682 | 683 | end($this->data); |
| 683 | 684 | $lastKey = key($this->data); |
@@ -699,8 +700,7 @@ discard block |
||
| 699 | 700 | break; |
| 700 | 701 | } |
| 701 | 702 | } |
| 702 | - } |
|
| 703 | - elseif ($strict) { |
|
| 703 | + } elseif ($strict) { |
|
| 704 | 704 | throw new \ErrorException("Unable to move the internal pointer to a key that doesn't exist."); |
| 705 | 705 | } |
| 706 | 706 | |
@@ -767,8 +767,9 @@ discard block |
||
| 767 | 767 | 'data' => $this->data, |
| 768 | 768 | ]); |
| 769 | 769 | |
| 770 | - if ($exit) |
|
| 771 | - exit; |
|
| 770 | + if ($exit) { |
|
| 771 | + exit; |
|
| 772 | + } |
|
| 772 | 773 | |
| 773 | 774 | return $this; |
| 774 | 775 | } |
@@ -64,14 +64,12 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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]; |
@@ -244,15 +239,18 @@ discard block |
||
| 244 | 239 | ; |
| 245 | 240 | |
| 246 | 241 | foreach ($row as $column => &$values) { |
| 247 | - if ( ! $values instanceof MergeBucket) |
|
| 248 | - continue; |
|
| 242 | + if ( ! $values instanceof MergeBucket) { |
|
| 243 | + continue; |
|
| 244 | + } |
|
| 249 | 245 | |
| 250 | - if (in_array($column, $excluded_columns)) |
|
| 251 | - continue; |
|
| 246 | + if (in_array($column, $excluded_columns)) { |
|
| 247 | + continue; |
|
| 248 | + } |
|
| 252 | 249 | |
| 253 | 250 | $values = Arrays::unique($values); |
| 254 | - if (count($values) == 1) |
|
| 255 | - $values = $values[0]; |
|
| 251 | + if (count($values) == 1) { |
|
| 252 | + $values = $values[0]; |
|
| 253 | + } |
|
| 256 | 254 | } |
| 257 | 255 | |
| 258 | 256 | return $row; |
@@ -281,11 +279,13 @@ discard block |
||
| 281 | 279 | ; |
| 282 | 280 | |
| 283 | 281 | foreach ($row as $column => &$values) { |
| 284 | - if (in_array($column, $excluded_columns)) |
|
| 285 | - continue; |
|
| 282 | + if (in_array($column, $excluded_columns)) { |
|
| 283 | + continue; |
|
| 284 | + } |
|
| 286 | 285 | |
| 287 | - if ($values instanceof MergeBucket) |
|
| 288 | - $values = $values->toArray(); |
|
| 286 | + if ($values instanceof MergeBucket) { |
|
| 287 | + $values = $values->toArray(); |
|
| 288 | + } |
|
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | return $row; |
@@ -312,8 +312,7 @@ discard block |
||
| 312 | 312 | foreach ($array as $key => $value) { |
| 313 | 313 | if (is_scalar($value)) { |
| 314 | 314 | $id = $value; |
| 315 | - } |
|
| 316 | - else { |
|
| 315 | + } else { |
|
| 317 | 316 | $id = serialize($value); |
| 318 | 317 | } |
| 319 | 318 | |
@@ -351,16 +350,13 @@ discard block |
||
| 351 | 350 | foreach ($array as $key => &$value) { // &for optimization |
| 352 | 351 | if (is_scalar($value)) { |
| 353 | 352 | $sum += $value; |
| 354 | - } |
|
| 355 | - elseif (is_null($value)) { |
|
| 353 | + } elseif (is_null($value)) { |
|
| 356 | 354 | continue; |
| 357 | - } |
|
| 358 | - elseif (is_array($value)) { |
|
| 355 | + } elseif (is_array($value)) { |
|
| 359 | 356 | throw new \InvalidArgumentException( |
| 360 | 357 | "Trying to sum an array with '$sum': ".var_export($value, true) |
| 361 | 358 | ); |
| 362 | - } |
|
| 363 | - elseif (is_object($value)) { |
|
| 359 | + } elseif (is_object($value)) { |
|
| 364 | 360 | if ( ! method_exists($value, 'toNumber')) { |
| 365 | 361 | throw new \InvalidArgumentEXception( |
| 366 | 362 | "Trying to sum a ".get_class($value)." object which cannot be casted as a number. " |
@@ -387,17 +383,21 @@ discard block |
||
| 387 | 383 | */ |
| 388 | 384 | public static function weightedMean($values, $weights) |
| 389 | 385 | { |
| 390 | - if ($values instanceof ChainableArray) |
|
| 391 | - $values = $values->toArray(); |
|
| 386 | + if ($values instanceof ChainableArray) { |
|
| 387 | + $values = $values->toArray(); |
|
| 388 | + } |
|
| 392 | 389 | |
| 393 | - if ($weights instanceof ChainableArray) |
|
| 394 | - $weights = $weights->toArray(); |
|
| 390 | + if ($weights instanceof ChainableArray) { |
|
| 391 | + $weights = $weights->toArray(); |
|
| 392 | + } |
|
| 395 | 393 | |
| 396 | - if ( ! is_array($values)) |
|
| 397 | - $values = [$values]; |
|
| 394 | + if ( ! is_array($values)) { |
|
| 395 | + $values = [$values]; |
|
| 396 | + } |
|
| 398 | 397 | |
| 399 | - if ( ! is_array($weights)) |
|
| 400 | - $weights = [$weights]; |
|
| 398 | + if ( ! is_array($weights)) { |
|
| 399 | + $weights = [$weights]; |
|
| 400 | + } |
|
| 401 | 401 | |
| 402 | 402 | if (count($values) != count($weights)) { |
| 403 | 403 | throw new \InvalidArgumentException( |
@@ -408,12 +408,14 @@ discard block |
||
| 408 | 408 | ); |
| 409 | 409 | } |
| 410 | 410 | |
| 411 | - if (!$values) |
|
| 412 | - return null; |
|
| 411 | + if (!$values) { |
|
| 412 | + return null; |
|
| 413 | + } |
|
| 413 | 414 | |
| 414 | 415 | $weights_sum = array_sum($weights); |
| 415 | - if (!$weights_sum) |
|
| 416 | - return 0; |
|
| 416 | + if (!$weights_sum) { |
|
| 417 | + return 0; |
|
| 418 | + } |
|
| 417 | 419 | |
| 418 | 420 | $weighted_sum = 0; |
| 419 | 421 | foreach ($values as $i => $value) { |