| Total Complexity | 13 |
| Total Lines | 75 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 10 | class NumberUtil |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @param integer $number |
||
| 14 | * @return integer |
||
| 15 | */ |
||
| 16 | public static function sumDigit(int $number) |
||
| 17 | { |
||
| 18 | if ($number < 0) { |
||
| 19 | throw new NegativeNumberException("Parameter has to be positive."); |
||
| 20 | } |
||
| 21 | $s = (string) $number; |
||
| 22 | $sum = 0; |
||
| 23 | for ($i = 0; $i < strlen($s); $i++) { |
||
| 24 | $sum += (int) $s[$i]; |
||
| 25 | } |
||
| 26 | return $sum; |
||
| 27 | } |
||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * @param integer $number |
||
| 33 | * @return integer |
||
| 34 | */ |
||
| 35 | public static function getNextTens($number) |
||
| 36 | { |
||
| 37 | if ($number < 0) { |
||
| 38 | throw new NegativeNumberException("Parameter has to be positive."); |
||
| 39 | } |
||
| 40 | $s = (string) $number; |
||
| 41 | if (strlen($s) == 1) { |
||
| 42 | return 10; |
||
| 43 | } |
||
| 44 | $tens = (int) $s[0]; |
||
| 45 | return ($tens + 1) * 10; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param integer $number |
||
| 50 | * @return integer |
||
| 51 | */ |
||
| 52 | public static function getUnit($number) |
||
| 53 | { |
||
| 54 | if ($number < 0) { |
||
| 55 | throw new NegativeNumberException("Parameter has to be positive."); |
||
| 56 | } |
||
| 57 | $s = (string) $number; |
||
| 58 | return (int) $s[strlen($s) - 1]; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param integer $value |
||
| 63 | * @param integer $minValue |
||
| 64 | * @param integer $maxValue |
||
| 65 | * @return boolean |
||
| 66 | */ |
||
| 67 | public static function isInRange(int $value, int $minValue, int $maxValue) |
||
| 68 | { |
||
| 69 | return $value > $minValue && $value < $maxValue; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param array $numbers |
||
| 74 | * @return integer |
||
| 75 | */ |
||
| 76 | public function getMinValue($numbers) |
||
| 85 | } |
||
| 86 | } |
||
| 87 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: