@@ -24,7 +24,7 @@ final class Calculator
2424 *
2525 * @return string The result.
2626 */
27- public static function add ($ first_number , $ second_number , $ scale = 6 )
27+ public static function add (string $ first_number , string $ second_number , int $ scale = 6 ): string
2828 {
2929 self ::assertNumberFormat ($ first_number );
3030 self ::assertNumberFormat ($ second_number );
@@ -44,7 +44,7 @@ public static function add($first_number, $second_number, $scale = 6)
4444 *
4545 * @return string The result.
4646 */
47- public static function subtract ($ first_number , $ second_number , $ scale = 6 )
47+ public static function subtract (string $ first_number , string $ second_number , int $ scale = 6 ): string
4848 {
4949 self ::assertNumberFormat ($ first_number );
5050 self ::assertNumberFormat ($ second_number );
@@ -64,7 +64,7 @@ public static function subtract($first_number, $second_number, $scale = 6)
6464 *
6565 * @return string The result.
6666 */
67- public static function multiply ($ first_number , $ second_number , $ scale = 6 )
67+ public static function multiply (string $ first_number , string $ second_number , int $ scale = 6 ): string
6868 {
6969 self ::assertNumberFormat ($ first_number );
7070 self ::assertNumberFormat ($ second_number );
@@ -84,7 +84,7 @@ public static function multiply($first_number, $second_number, $scale = 6)
8484 *
8585 * @return string The result.
8686 */
87- public static function divide ($ first_number , $ second_number , $ scale = 6 )
87+ public static function divide (string $ first_number , string $ second_number , int $ scale = 6 ): string
8888 {
8989 self ::assertNumberFormat ($ first_number );
9090 self ::assertNumberFormat ($ second_number );
@@ -100,7 +100,7 @@ public static function divide($first_number, $second_number, $scale = 6)
100100 *
101101 * @return string The result.
102102 */
103- public static function ceil ($ number )
103+ public static function ceil (string $ number ): string
104104 {
105105 if (self ::compare ($ number , 0 ) == 1 ) {
106106 $ result = bcadd ($ number , '1 ' , 0 );
@@ -118,7 +118,7 @@ public static function ceil($number)
118118 *
119119 * @return string The result.
120120 */
121- public static function floor ($ number )
121+ public static function floor (string $ number ): string
122122 {
123123 if (self ::compare ($ number , 0 ) == 1 ) {
124124 $ result = bcadd ($ number , '0 ' , 0 );
@@ -145,7 +145,7 @@ public static function floor($number)
145145 *
146146 * @throws \InvalidArgumentException
147147 */
148- public static function round ($ number , $ precision = 0 , $ mode = PHP_ROUND_HALF_UP )
148+ public static function round (string $ number , int $ precision = 0 , int $ mode = PHP_ROUND_HALF_UP ): string
149149 {
150150 self ::assertNumberFormat ($ number );
151151 if (!is_numeric ($ precision ) || $ precision < 0 ) {
@@ -163,7 +163,7 @@ public static function round($number, $precision = 0, $mode = PHP_ROUND_HALF_UP)
163163 // The rounding direction is based on the first decimal after $precision.
164164 $ number_parts = explode ('. ' , $ number );
165165 $ decimals = !empty ($ number_parts [1 ]) ? $ number_parts [1 ] : '0 ' ;
166- $ relevant_decimal = isset ( $ decimals [$ precision ]) ? $ decimals [ $ precision ] : 0 ;
166+ $ relevant_decimal = $ decimals [$ precision ] ?? 0 ;
167167 if ($ relevant_decimal < 5 ) {
168168 $ number = $ rounded_down ;
169169 } elseif ($ relevant_decimal == 5 ) {
@@ -197,11 +197,10 @@ public static function round($number, $precision = 0, $mode = PHP_ROUND_HALF_UP)
197197 * @return int 0 if both numbers are equal, 1 if the first one is greater,
198198 * -1 otherwise.
199199 */
200- public static function compare ($ first_number , $ second_number , $ scale = 6 )
200+ public static function compare (string $ first_number , string $ second_number , int $ scale = 6 ): int
201201 {
202202 self ::assertNumberFormat ($ first_number );
203203 self ::assertNumberFormat ($ second_number );
204-
205204 return bccomp ($ first_number , $ second_number , $ scale );
206205 }
207206
@@ -216,7 +215,7 @@ public static function compare($first_number, $second_number, $scale = 6)
216215 *
217216 * @return string The trimmed number.
218217 */
219- public static function trim ($ number )
218+ public static function trim (string $ number ): string
220219 {
221220 if (strpos ($ number , '. ' ) != false ) {
222221 // The number is decimal, strip trailing zeroes.
@@ -228,20 +227,18 @@ public static function trim($number)
228227 return $ number ;
229228 }
230229
231- /**
232- * Assert that the given number is a numeric string value.
233- *
234- * @param string $number The number to check.
235- *
236- * @throws \InvalidArgumentException
237- */
238- public static function assertNumberFormat ($ number )
239- {
240- if (is_float ($ number )) {
241- throw new \InvalidArgumentException (sprintf ('The provided value "%s" must be a string, not a float. ' , $ number ));
242- }
243- if (!is_numeric ($ number )) {
244- throw new \InvalidArgumentException (sprintf ('The provided value "%s" is not a numeric value. ' , $ number ));
245- }
246- }
230+ /**
231+ * Assert that the given number is a numeric string value.
232+ *
233+ * @param string $number The number to check.
234+ *
235+ * @throws \InvalidArgumentException
236+ */
237+ public static function assertNumberFormat (string $ number )
238+ {
239+ if (!is_numeric ($ number )) {
240+ throw new \InvalidArgumentException (sprintf ('The provided value "%s" is not a numeric value. ' , $ number ));
241+ }
242+ }
243+
247244}
0 commit comments