PluginProbe
TablePress – Tables in WordPress made easy / 3.0
TablePress – Tables in WordPress made easy v3.0
3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 2.4.4 All 44 releases
← All changes | libraries/evalmath.class.php +117 -121 2.1.73.0 View file →
@@ -29,59 +29,56 @@
29 29 *
30 30 * Note, variable and function names are case insensitive.
31 31 *
32 32 * @since 1.0.0
33 - * @var string
34 33 */
35 - protected static $name_pattern = '[a-z][a-z0-9_]*';
34 + protected static string $name_pattern = '[a-z][a-z0-9_]*';
36 35
37 36 /**
38 37 * Whether to suppress errors and warnings.
39 38 *
40 39 * @since 1.0.0
41 - * @var boolean
42 40 */
43 - public $suppress_errors = false;
41 + public bool $suppress_errors = false;
44 42
45 43 /**
46 44 * The last error message that was raised.
47 45 *
48 46 * @since 1.0.0
49 - * @var string
50 47 */
51 - public $last_error = '';
48 + public string $last_error = '';
52 49
53 50 /**
54 51 * Variables (including constants).
55 52 *
56 53 * @since 1.0.0
57 - * @var array
54 + * @var array<string, mixed>
58 55 */
59 - public $variables = array();
56 + public array $variables = array();
60 57
61 58 /**
62 59 * User-defined functions.
63 60 *
64 61 * @since 1.0.0
65 - * @var array
62 + * @var array<string, mixed>
66 63 */
67 - protected $functions = array();
64 + protected array $functions = array();
68 65
69 66 /**
70 67 * Constants.
71 68 *
72 69 * @since 1.0.0
73 - * @var array
70 + * @var array<string, mixed>
74 71 */
75 - protected $constants = array();
72 + protected array $constants = array();
76 73
77 74 /**
78 75 * Built-in functions.
79 76 *
80 77 * @since 1.0.0
81 - * @var array
78 + * @var string[]
82 79 */
83 - protected $builtin_functions = array(
80 + protected array $builtin_functions = array(
84 81 'sin',
85 82 'sinh',
86 83 'arcsin',
87 84 'asin',
@@ -111,11 +108,11 @@
111 108 /**
112 109 * Emulated functions.
113 110 *
114 111 * @since 1.0.0
115 - * @var array
112 + * @var array<string, int[]>
116 113 */
117 - protected $calc_functions = array(
114 + protected array $calc_functions = array(
118 115 'average' => array( -1 ),
119 116 'mean' => array( -1 ),
120 117 'median' => array( -1 ),
121 118 'mode' => array( -1 ),
@@ -160,9 +157,9 @@
160 157 *
161 158 * @param string $expression The expression that shall be evaluated.
162 159 * @return string|false Evaluated expression or false on error.
163 160 */
164 - public function evaluate( $expression ) {
161 + public function evaluate( $expression ) /* : string|false */ {
165 162 return $this->pfx( $this->nfx( $expression ) );
166 163 }
167 164
168 165 /**
@@ -172,9 +169,9 @@
172 169 *
173 170 * @param string $expression The expression that shall be evaluated.
174 171 * @return string|bool Evaluated expression, true on successful function assignment, or false on error.
175 172 */
176 - public function assign_and_evaluate( $expression ) {
173 + public function assign_and_evaluate( $expression ) /* : string|bool */ {
177 174 $this->last_error = '';
178 175 $expression = trim( $expression );
179 176 $expression = rtrim( $expression, ';' );
180 177
@@ -236,11 +233,11 @@
236 233 * Return all user-defined variables and values.
237 234 *
238 235 * @since 1.0.0
239 236 *
240 - * @return array User-defined variables and values.
237 + * @return array<string, mixed> User-defined variables and values.
241 238 */
242 - public function variables() {
239 + public function variables(): array {
243 240 return $this->variables;
244 241 }
245 242
246 243 /**
@@ -247,11 +244,11 @@
247 244 * Return all user-defined functions with their arguments.
248 245 *
249 246 * @since 1.0.0
250 247 *
251 - * @return array User-defined functions.
248 + * @return array<int, string> User-defined functions.
252 249 */
253 - public function functions() {
250 + public function functions(): array {
254 251 $output = array();
255 252 foreach ( $this->functions as $name => $data ) {
256 253 $output[] = $name . '( ' . implode( ', ', $data['args'] ) . ' )';
257 254 }
@@ -267,25 +264,25 @@
267 264 *
268 265 * @since 1.0.0
269 266 *
270 267 * @param string $expression Math expression that shall be converted.
271 - * @return array|false Converted expression or false on error.
268 + * @return mixed[]|false Converted expression or false on error.
272 269 */
273 - protected function nfx( $expression ) {
270 + protected function nfx( $expression ) /* : mixed[]|false */ {
274 271 $index = 0;
275 272 $stack = new EvalMath_Stack();
276 273 $output = array(); // postfix form of expression, to be passed to pfx().
277 274 $expression = trim( strtolower( $expression ) );
278 275
279 - $ops = array( '+', '-', '*', '/', '^', '_', '>', '<', '=' );
280 - $ops_r = array( '+' => 0, '-' => 0, '*' => 0, '/' => 0, '^' => 1, '>' => 0, '<' => 0, '=' => 0 ); // Right-associative operator?
281 - $ops_p = array( '+' => 0, '-' => 0, '*' => 1, '/' => 1, '_' => 1, '^' => 2, '>' => 0, '<' => 0, '=' => 0 ); // Operator precedence.
276 + $ops = array( '+', '-', '*', '/', '^', '_', '>', '<', '=', '%' );
277 + $ops_r = array( '+' => 0, '-' => 0, '*' => 0, '/' => 0, '^' => 1, '>' => 0, '<' => 0, '=' => 0, '%' => 0 ); // Right-associative operator?
278 + $ops_p = array( '+' => 0, '-' => 0, '*' => 1, '/' => 1, '_' => 1, '^' => 2, '>' => 0, '<' => 0, '=' => 0, '%' => 1 ); // Operator precedence.
282 279
283 280 // We use this in syntax-checking the expression and determining when a - (minus) is a negation.
284 281 $expecting_operator = false;
285 282
286 283 // Make sure the characters are all good.
287 - if ( 1 === preg_match( '/[^\w\s+*^\/()\.,-<>=]/', $expression, $matches ) ) {
284 + if ( 1 === preg_match( '/[^\%\w\s+*^\/()\.,-<>=]/', $expression, $matches ) ) {
288 285 return $this->raise_error( 'illegal_character_general', $matches[0] );
289 286 }
290 287
291 288 // Infinite Loop for the conversion.
@@ -346,9 +343,9 @@
346 343 $output[] = array( 'function_name' => $function_name, 'arg_count' => $arg_count );
347 344 // Check the argument count, depending on what type of function we have.
348 345 if ( in_array( $function_name, $this->builtin_functions, true ) ) {
349 346 // Built-in functions.
350 - if ( $arg_count > 1 ) {
347 + if ( $arg_count > 1 ) { // @phpstan-ignore greater.alwaysFalse
351 348 $error_data = array( 'expected' => 1, 'given' => $arg_count );
352 349 return $this->raise_error( 'wrong_number_of_arguments', $error_data );
353 350 }
354 351 } elseif ( array_key_exists( $function_name, $this->calc_functions ) ) {
@@ -353,17 +350,18 @@
353 350 }
354 351 } elseif ( array_key_exists( $function_name, $this->calc_functions ) ) {
355 352 // Calc-emulation functions.
356 353 $counts = $this->calc_functions[ $function_name ];
357 - if ( in_array( -1, $counts, true ) && $arg_count > 0 ) {
354 + // @phpstan-ignore greater.alwaysFalse, booleanAnd.alwaysFalse
355 + if ( in_array( -1, $counts, true ) && $arg_count > 0 ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
358 356 // Everything is fine, we expected an indefinite number arguments and got some.
359 - } elseif ( ! in_array( $arg_count, $counts, true ) ) {
357 + } elseif ( ! in_array( $arg_count, $counts, true ) ) { // @phpstan-ignore function.impossibleType
360 358 $error_data = array( 'expected' => implode( '/', $this->calc_functions[ $function_name ] ), 'given' => $arg_count );
361 359 return $this->raise_error( 'wrong_number_of_arguments', $error_data );
362 360 }
363 361 } elseif ( array_key_exists( $function_name, $this->functions ) ) {
364 362 // User-defined functions.
365 - if ( count( $this->functions[ $function_name ]['args'] ) !== $arg_count ) {
363 + if ( count( $this->functions[ $function_name ]['args'] ) !== $arg_count ) { // @phpstan-ignore notIdentical.alwaysTrue
366 364 $error_data = array( 'expected' => count( $this->functions[ $function_name ]['args'] ), 'given' => $arg_count );
367 365 return $this->raise_error( 'wrong_number_of_arguments', $error_data );
368 366 }
369 367 } else {
@@ -389,9 +387,9 @@
389 387 if ( 0 === preg_match( '/^(' . self::$name_pattern . ')\($/', (string) $stack->last( 2 ), $matches ) ) {
390 388 return $this->raise_error( 'unexpected_comma' );
391 389 }
392 390 // Increment the argument count.
393 - $stack->push( $stack->pop() + 1 );
391 + $stack->push( $stack->pop() + 1 ); // @phpstan-ignore binaryOp.invalid
394 392 // Put the ( back on, we'll need to pop back to it again.
395 393 $stack->push( '(' );
396 394 ++$index;
397 395 $expecting_operator = false;
@@ -495,13 +493,13 @@
495 493 * Evaluate postfix notation.
496 494 *
497 495 * @since 1.0.0
498 496 *
499 - * @param array|false $tokens [description].
500 - * @param array $variables Optional. [description].
497 + * @param array<int, string|mixed[]>|false $tokens [description].
498 + * @param array<string, mixed> $variables Optional. [description].
501 499 * @return mixed [description].
502 500 */
503 - protected function pfx( $tokens, array $variables = array() ) {
501 + protected function pfx( $tokens, array $variables = array() ) /* : mixed */ {
504 502 if ( false === $tokens ) {
505 503 return false;
506 504 }
507 505
@@ -512,10 +510,11 @@
512 510 if ( is_array( $token ) ) { // it's a function!
513 511 $function_name = $token['function_name'];
514 512 $count = $token['arg_count'];
515 513
516 - // Built-in function.
517 514 if ( in_array( $function_name, $this->builtin_functions, true ) ) {
515 + // Built-in function.
516 +
518 517 $op1 = $stack->pop();
519 518 if ( is_null( $op1 ) ) {
520 519 return $this->raise_error( 'internal_error' );
521 520 }
@@ -527,11 +526,11 @@
527 526 }
528 527 // Perfectly safe eval().
529 528 // phpcs:ignore Squiz.PHP.Eval.Discouraged
530 529 eval( '$stack->push( ' . $function_name . '( $op1 ) );' );
530 + } elseif ( array_key_exists( $function_name, $this->calc_functions ) ) {
531 + // Calc-emulation function.
531 532
532 - // Calc-emulation function.
533 - } elseif ( array_key_exists( $function_name, $this->calc_functions ) ) {
534 533 // Get function arguments.
535 534 $args = array();
536 535 for ( $i = $count - 1; $i >= 0; $i-- ) {
537 536 $arg = $stack->pop();
@@ -559,11 +558,11 @@
559 558 if ( false === $result ) {
560 559 return $this->raise_error( 'internal_error' );
561 560 }
562 561 $stack->push( $result );
562 + } elseif ( array_key_exists( $function_name, $this->functions ) ) {
563 + // User-defined function.
563 564
564 - // User-defined function.
565 - } elseif ( array_key_exists( $function_name, $this->functions ) ) {
566 565 // Get function arguments.
567 566 $args = array();
568 567 for ( $i = count( $this->functions[ $function_name ]['args'] ) - 1; $i >= 0; $i-- ) {
569 568 $arg = $stack->pop();
@@ -572,14 +571,13 @@
572 571 } else {
573 572 $args[ $this->functions[ $function_name ]['args'][ $i ] ] = $arg;
574 573 }
575 574 }
576 - // yay... recursion!
577 - $stack->push( $this->pfx( $this->functions[ $function_name ]['func'], $args ) );
575 + // Recursion.
576 + $stack->push( $this->pfx( $this->functions[ $function_name ]['func'], $args ) ); // @phpstan-ignore argument.type
578 577 }
579 -
578 + } elseif ( in_array( $token, array( '+', '-', '*', '/', '^', '>', '<', '=', '%' ), true ) ) {
580 579 // If the token is a binary operator, pop two values off the stack, do the operation, and push the result back on.
581 - } elseif ( in_array( $token, array( '+', '-', '*', '/', '^', '>', '<', '=' ), true ) ) {
582 580 $op2 = $stack->pop();
583 581 if ( is_null( $op2 ) ) {
584 582 return $this->raise_error( 'internal_error' );
585 583 }
@@ -615,25 +613,26 @@
615 613 case '=':
616 614 // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison,Universal.Operators.StrictComparisons.LooseEqual
617 615 $stack->push( (int) ( $op1 == $op2 ) ); // Don't use === as the variable type can differ (int/double/bool).
618 616 break;
617 + case '%':
618 + $stack->push( $op1 % $op2 );
619 + break;
619 620 }
620 -
621 + } elseif ( '_' === $token ) {
621 622 // If the token is a unary operator, pop one value off the stack, do the operation, and push it back on.
622 - } elseif ( '_' === $token ) {
623 623 $stack->push( -1 * $stack->pop() );
624 -
625 - // If the token is a number or variable, push it on the stack.
624 + } elseif ( is_numeric( $token ) ) {
625 + // If the token is a number, push it on the stack.
626 + $stack->push( $token );
627 + } elseif ( array_key_exists( $token, $this->variables ) ) {
628 + // If the token is a variable, push it on the stack.
629 + $stack->push( $this->variables[ $token ] );
630 + } elseif ( array_key_exists( $token, $variables ) ) {
631 + // If the token is a variable, push it on the stack.
632 + $stack->push( $variables[ $token ] );
626 633 } else {
627 - if ( is_numeric( $token ) ) {
628 - $stack->push( $token );
629 - } elseif ( array_key_exists( $token, $this->variables ) ) {
630 - $stack->push( $this->variables[ $token ] );
631 - } elseif ( array_key_exists( $token, $variables ) ) {
632 - $stack->push( $variables[ $token ] );
633 - } else {
634 - return $this->raise_error( 'undefined_variable', $token );
635 - }
634 + return $this->raise_error( 'undefined_variable', $token );
636 635 }
637 636 }
638 637 // When we're out of tokens, the stack should have a single element, the final result.
639 638 if ( 1 !== $stack->count ) {
@@ -646,13 +645,13 @@
646 645 * Raise an error.
647 646 *
648 647 * @since 1.0.0
649 648 *
650 - * @param string $message Error message.
651 - * @param array|string $error_data Optional. Additional error data.
652 - * @return bool False, to stop evaluation.
649 + * @param string $message Error message.
650 + * @param mixed[]|string $error_data Optional. Additional error data.
651 + * @return false False, to stop evaluation.
653 652 */
654 - protected function raise_error( $message, $error_data = null ) {
653 + protected function raise_error( $message, $error_data = null ): bool {
655 654 $this->last_error = $this->get_error_string( $message, $error_data );
656 655 return false;
657 656 }
658 657
@@ -663,13 +662,13 @@
663 662 *
664 663 * @link https://github.com/moodle/moodle/blob/13264f35057d2f37374ec3e0e8ad4070f4676bd7/lang/en/mathslib.php
665 664 * @link https://github.com/moodle/moodle/blob/8e54ce9717c19f768b95f4332f70e3180ffafc46/lib/moodlelib.php#L6323
666 665 *
667 - * @param string $identifier Identifier of the string.
668 - * @param array|string $error_data Optional. Additional error data.
666 + * @param string $identifier Identifier of the string.
667 + * @param mixed[]|string $error_data Optional. Additional error data.
669 668 * @return string Translated string.
670 669 */
671 - protected function get_error_string( $identifier, $error_data = null ) {
670 + protected function get_error_string( $identifier, $error_data = null ): string {
672 671 $strings = array();
673 672 $strings['an_unexpected_error_occurred'] = 'an unexpected error occurred';
674 673 $strings['cannot_assign_to_constant'] = 'cannot assign to constant \'{$error_data}\'';
675 674 $strings['cannot_redefine_builtin_function'] = 'cannot redefine built-in function \'{$error_data}()\'';
@@ -685,9 +684,9 @@
685 684 $strings['unexpected_comma'] = 'unexpected comma';
686 685 $strings['unexpected_operator'] = 'unexpected operator \'{$error_data}\'';
687 686 $strings['wrong_number_of_arguments'] = 'wrong number of arguments ({$error_data->given} given, {$error_data->expected} expected)';
688 687
689 - $string = $strings[ $identifier ];
688 + $a_string = $strings[ $identifier ];
690 689
691 690 if ( null !== $error_data ) {
692 691 if ( is_array( $error_data ) ) {
693 692 $search = array();
@@ -711,16 +710,16 @@
711 710 $search[] = '{$error_data->' . $key . '}';
712 711 $replace[] = (string) $value;
713 712 }
714 713 if ( $search ) {
715 - $string = str_replace( $search, $replace, $string );
714 + $a_string = str_replace( $search, $replace, $a_string );
716 715 }
717 716 } else {
718 - $string = str_replace( '{$error_data}', (string) $error_data, $string );
717 + $a_string = str_replace( '{$error_data}', (string) $error_data, $a_string );
719 718 }
720 719 }
721 720
722 - return $string;
721 + return $a_string;
723 722 }
724 723
725 724 } // class EvalMath
726 725
@@ -736,19 +735,18 @@
736 735 /**
737 736 * The stack.
738 737 *
739 738 * @since 1.0.0
740 - * @var array
739 + * @var mixed[]
741 740 */
742 - protected $stack = array();
741 + protected array $stack = array();
743 742
744 743 /**
745 744 * Number of items on the stack.
746 745 *
747 746 * @since 1.0.0
748 - * @var int
749 747 */
750 - public $count = 0;
748 + public int $count = 0;
751 749
752 750 /**
753 751 * Push an item onto the stack.
754 752 *
@@ -755,9 +753,9 @@
755 753 * @since 1.0.0
756 754 *
757 755 * @param mixed $value The item that is pushed onto the stack.
758 756 */
759 - public function push( $value ) {
757 + public function push( $value ): void {
760 758 $this->stack[ $this->count ] = $value;
761 759 ++$this->count;
762 760 }
763 761
@@ -765,11 +763,11 @@
765 763 * Pop an item from the top of the stack.
766 764 *
767 765 * @since 1.0.0
768 766 *
769 - * @return mixed The item that is popped from the stack.
767 + * @return mixed|null The item that is popped from the stack.
770 768 */
771 - public function pop() {
769 + public function pop() /* : mixed|null */ {
772 770 if ( $this->count > 0 ) {
773 771 --$this->count;
774 772 return $this->stack[ $this->count ];
775 773 }
@@ -781,11 +779,11 @@
781 779 *
782 780 * @since 1.0.0
783 781 *
784 782 * @param int $n Count from the end of the stack.
785 - * @return mixed The item that is popped from the stack.
783 + * @return mixed|null The item that is popped from the stack.
786 784 */
787 - public function last( $n = 1 ) {
785 + public function last( $n = 1 ) /* : mixed|null */ {
788 786 if ( ( $this->count - $n ) >= 0 ) {
789 787 return $this->stack[ $this->count - $n ];
790 788 }
791 789 return null;
@@ -805,11 +803,10 @@
805 803 /**
806 804 * Seed for the generation of random numbers.
807 805 *
808 806 * @since 1.0.0
809 - * @var string
810 807 */
811 - protected static $random_seed = null;
808 + protected static ?string $random_seed = null;
812 809
813 810 /**
814 811 * Choose from two values based on an if-condition.
815 812 *
@@ -821,9 +818,9 @@
821 818 * @param double|int $statement Return value if the condition is true.
822 819 * @param double|int $alternative Return value if the condition is false.
823 820 * @return double|int Result of the if check.
824 821 */
825 - public static function func_if( $condition, $statement, $alternative ) {
822 + public static function func_if( $condition, $statement, $alternative ) /* : float|int */ {
826 823 return ( (bool) $condition ? $statement : $alternative );
827 824 }
828 825
829 826 /**
@@ -835,9 +832,9 @@
835 832 *
836 833 * @param double|int $value Value to be negated.
837 834 * @return int Negated value (0 for false, 1 for true).
838 835 */
839 - public static function func_not( $value ) {
836 + public static function func_not( $value ): int {
840 837 return (int) ! (bool) $value;
841 838 }
842 839
843 840 /**
@@ -849,9 +846,9 @@
849 846 *
850 847 * @param double|int ...$args Values for which the conjunction shall be calculated.
851 848 * @return int Conjunction of the passed arguments.
852 849 */
853 - public static function func_and( ...$args ) {
850 + public static function func_and( ...$args ): int {
854 851 foreach ( $args as $value ) {
855 852 if ( ! $value ) {
856 853 return 0;
857 854 }
@@ -868,9 +865,9 @@
868 865 *
869 866 * @param double|int ...$args Values for which the disjunction shall be calculated.
870 867 * @return int Disjunction of the passed arguments.
871 868 */
872 - public static function func_or( ...$args ) {
869 + public static function func_or( ...$args ): int {
873 870 foreach ( $args as $value ) {
874 871 if ( $value ) {
875 872 return 1;
876 873 }
@@ -884,9 +881,9 @@
884 881 * @since 1.0.0
885 882 *
886 883 * @return double Rounded value of Pi.
887 884 */
888 - public static function pi() {
885 + public static function pi(): float {
889 886 return pi();
890 887 }
891 888
892 889 /**
@@ -896,9 +893,9 @@
896 893 *
897 894 * @param double|int ...$args Values for which the sum shall be calculated.
898 895 * @return double|int Sum of the passed arguments.
899 896 */
900 - public static function sum( ...$args ) {
897 + public static function sum( ...$args ) /* : float|int */ {
901 898 return array_sum( $args );
902 899 }
903 900
904 901 /**
@@ -906,11 +903,11 @@
906 903 *
907 904 * @since 1.10.0
908 905 *
909 906 * @param double|int ...$args Values for which the number of non-empty elements shall be counted.
910 - * @return double|int Counted number of non-empty elements in the passed values.
907 + * @return int Counted number of non-empty elements in the passed values.
911 908 */
912 - public static function counta( ...$args ) {
909 + public static function counta( ...$args ): int {
913 910 return count( array_filter( $args ) );
914 911 }
915 912
916 913 /**
@@ -920,9 +917,9 @@
920 917 *
921 918 * @param double|int ...$args Values for which the product shall be calculated.
922 919 * @return double|int Product of the passed arguments.
923 920 */
924 - public static function product( ...$args ) {
921 + public static function product( ...$args ) /* : float|int */ {
925 922 return array_product( $args );
926 923 }
927 924
928 925 /**
@@ -932,9 +929,9 @@
932 929 *
933 930 * @param double|int ...$args Values for which the average shall be calculated.
934 931 * @return double|int Average value of the passed arguments.
935 932 */
936 - public static function average( ...$args ) {
933 + public static function average( ...$args ) /* : float|int */ {
937 934 // Catch division by zero.
938 935 if ( 0 === count( $args ) ) {
939 936 return 0;
940 937 }
@@ -947,15 +944,15 @@
947 944 * For even counts of arguments, the upper median is returned.
948 945 *
949 946 * @since 1.0.0
950 947 *
951 - * @param double|int ...$args Values for which the median shall be calculated.
948 + * @param array<int, double|int> ...$args Values for which the median shall be calculated.
952 949 * @return double|int Median of the passed arguments.
953 950 */
954 - public static function median( ...$args ) {
951 + public static function median( array ...$args ) /* : float|int */ {
955 952 sort( $args );
956 - $middle = floor( count( $args ) / 2 ); // Upper median for even counts.
957 - return $args[ $middle ];
953 + $middle = intdiv( count( $args ), 2 ); // Upper median for even counts.
954 + return $args[ $middle ]; // @phpstan-ignore return.type
958 955 }
959 956
960 957 /**
961 958 * Calculate the mode of the arguments.
@@ -961,16 +958,15 @@
961 958 * Calculate the mode of the arguments.
962 959 *
963 960 * @since 1.0.0
964 961 *
965 - * @param double|int ...$args Values for which the mode shall be calculated.
962 + * @param array<int, double|int> ...$args Values for which the mode shall be calculated.
966 963 * @return double|int Mode of the passed arguments.
967 964 */
968 - public static function mode( ...$args ) {
969 - $values = array_count_values( $args );
965 + public static function mode( ...$args ) /* : float|int */ {
966 + $values = array_count_values( $args ); // @phpstan-ignore argument.type
970 967 asort( $values );
971 - end( $values );
972 - return key( $values );
968 + return array_key_last( $values ); // @phpstan-ignore return.type
973 969 }
974 970
975 971 /**
976 972 * Calculate the range of the arguments.
@@ -979,9 +975,9 @@
979 975 *
980 976 * @param double|int ...$args Values for which the range shall be calculated.
981 977 * @return double|int Range of the passed arguments.
982 978 */
983 - public static function range( ...$args ) {
979 + public static function range( ...$args ) /* : float|int */ {
984 980 sort( $args );
985 981 return end( $args ) - reset( $args );
986 982 }
987 983
@@ -992,10 +988,10 @@
992 988 *
993 989 * @param double|int ...$args Values for which the maximum value shall be found.
994 990 * @return double|int Maximum value of the passed arguments.
995 991 */
996 - public static function max( ...$args ) {
997 - return max( $args );
992 + public static function max( ...$args ) /* : float|int */ {
993 + return max( $args ); // @phpstan-ignore argument.type
998 994 }
999 995
1000 996 /**
1001 997 * Find the minimum value of the arguments.
@@ -1004,10 +1000,10 @@
1004 1000 *
1005 1001 * @param double|int ...$args Values for which the minimum value shall be found.
1006 1002 * @return double|int Minimum value of the passed arguments.
1007 1003 */
1008 - public static function min( ...$args ) {
1009 - return min( $args );
1004 + public static function min( ...$args ) /* : float|int */ {
1005 + return min( $args ); // @phpstan-ignore argument.type
1010 1006 }
1011 1007
1012 1008 /**
1013 1009 * Calculate the remainder of a division of two numbers.
@@ -1015,11 +1011,11 @@
1015 1011 * @since 1.0.0
1016 1012 *
1017 1013 * @param double|int $op1 First number (dividend).
1018 1014 * @param double|int $op2 Second number (divisor).
1019 - * @return double|int Remainer of the division (dividend / divisor).
1015 + * @return int Remainder of the division (dividend / divisor).
1020 1016 */
1021 - public static function mod( $op1, $op2 ) {
1017 + public static function mod( $op1, $op2 ): int {
1022 1018 return $op1 % $op2;
1023 1019 }
1024 1020
1025 1021 /**
@@ -1030,9 +1026,9 @@
1030 1026 * @param double|int $base Base.
1031 1027 * @param double|int $exponent Exponent.
1032 1028 * @return double|int Power base^exponent.
1033 1029 */
1034 - public static function power( $base, $exponent ) {
1030 + public static function power( $base, $exponent ) /* : float|int */ {
1035 1031 return pow( $base, $exponent );
1036 1032 }
1037 1033
1038 1034 /**
@@ -1043,9 +1039,9 @@
1043 1039 * @param double|int $number Number.
1044 1040 * @param double|int $base Optional. Base for the logarithm. Default e (for the natural logarithm).
1045 1041 * @return double Logarithm of the number to the base.
1046 1042 */
1047 - public static function log( $number, $base = M_E ) {
1043 + public static function log( $number, $base = M_E ): float {
1048 1044 return log( $number, $base );
1049 1045 }
1050 1046
1051 1047 /**
@@ -1058,9 +1054,9 @@
1058 1054 * @param double|int $op1 First number.
1059 1055 * @param double|int $op2 Second number.
1060 1056 * @return double Arc tangent of two numbers, similar to arc tangent of $op1/op$ except for the sign.
1061 1057 */
1062 - public static function atan2( $op1, $op2 ) {
1058 + public static function atan2( $op1, $op2 ): float {
1063 1059 return atan2( $op1, $op2 );
1064 1060 }
1065 1061
1066 1062 /**
@@ -1071,9 +1067,9 @@
1071 1067 * @param double|int $value Number to be rounded.
1072 1068 * @param int $decimals Optional. Number of decimals after the comma after the rounding.
1073 1069 * @return double Rounded number.
1074 1070 */
1075 - public static function round( $value, $decimals = 0 ) {
1071 + public static function round( $value, $decimals = 0 ): float {
1076 1072 return round( $value, $decimals );
1077 1073 }
1078 1074
1079 1075 /**
@@ -1086,9 +1082,9 @@
1086 1082 * @param double|int $value Number to be rounded and formatted.
1087 1083 * @param int $decimals Optional. Number of decimals after the decimal separator after the rounding.
1088 1084 * @return string Formatted number.
1089 1085 */
1090 - public static function number_format( $value, $decimals = 0 ) {
1086 + public static function number_format( $value, $decimals = 0 ): string {
1091 1087 return number_format( $value, $decimals, '.', ',' );
1092 1088 }
1093 1089
1094 1090 /**
@@ -1101,9 +1097,9 @@
1101 1097 * @param double|int $value Number to be rounded and formatted.
1102 1098 * @param int $decimals Optional. Number of decimals after the decimal separator after the rounding.
1103 1099 * @return string Formatted number.
1104 1100 */
1105 - public static function number_format_eu( $value, $decimals = 0 ) {
1101 + public static function number_format_eu( $value, $decimals = 0 ): string {
1106 1102 return number_format( $value, $decimals, ',', ' ' );
1107 1103 }
1108 1104
1109 1105 /**
@@ -1112,9 +1108,9 @@
1112 1108 * @since 1.0.0
1113 1109 *
1114 1110 * @param string $random_seed The seed.
1115 1111 */
1116 - protected static function _set_random_seed( $random_seed ) {
1112 + protected static function _set_random_seed( $random_seed ): void {
1117 1113 self::$random_seed = $random_seed;
1118 1114 }
1119 1115
1120 1116 /**
@@ -1123,9 +1119,9 @@
1123 1119 * @since 1.0.0
1124 1120 *
1125 1121 * @return string The seed.
1126 1122 */
1127 - protected static function _get_random_seed() {
1123 + protected static function _get_random_seed(): string {
1128 1124 if ( is_null( self::$random_seed ) ) {
1129 1125 return microtime();
1130 1126 }
1131 1127 return self::$random_seed;
@@ -1139,9 +1135,9 @@
1139 1135 * @param int $min Minimum value for the range.
1140 1136 * @param int $max Maximum value for the range.
1141 1137 * @return int Random integer from the range [$min, $max].
1142 1138 */
1143 - public static function rand_int( $min, $max ) {
1139 + public static function rand_int( $min, $max ): int {
1144 1140 // Swap min and max value if min is bigger than max.
1145 1141 if ( $min > $max ) {
1146 1142 $tmp = $max;
1147 1143 $max = $min;
@@ -1151,15 +1147,15 @@
1151 1147 $number_characters = (int) ceil( log( $max + 1 - $min, 16 ) );
1152 1148 $md5string = md5( self::_get_random_seed() );
1153 1149 $offset = 0;
1154 1150 do {
1155 - while ( ( $offset + $number_characters ) > strlen( $md5string ) ) {
1151 + while ( ( $offset + $number_characters ) > strlen( $md5string ) ) { // phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found
1156 1152 $md5string .= md5( $md5string );
1157 1153 }
1158 - $randomno = hexdec( substr( $md5string, $offset, $number_characters ) );
1154 + $random_number = (int) hexdec( substr( $md5string, $offset, $number_characters ) );
1159 1155 $offset += $number_characters;
1160 - } while ( ( $min + $randomno ) > $max );
1161 - return $min + $randomno;
1156 + } while ( ( $min + $random_number ) > $max );
1157 + return $min + $random_number;
1162 1158 }
1163 1159
1164 1160 /**
1165 1161 * Get a random double value from a range [0, 1].
@@ -1167,10 +1163,10 @@
1167 1163 * @since 1.0.0
1168 1164 *
1169 1165 * @return double Random number from the range [0, 1].
1170 1166 */
1171 - public static function rand_float() {
1167 + public static function rand_float(): float {
1172 1168 $random_values = unpack( 'v', md5( self::_get_random_seed(), true ) );
1173 - return array_shift( $random_values ) / 65536;
1169 + return array_shift( $random_values ) / 65536; // @phpstan-ignore argument.type
1174 1170 }
1175 1171
1176 1172 } // class EvalMath_Functions