| Conditions | 6 |
| Paths | 11 |
| Total Lines | 25 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 14 |
| CRAP Score | 6 |
| Changes | 0 | ||
| 1 | <?php |
||
| 19 | 97 | public static function calculateCheckDigit(string $number): string |
|
| 20 | { |
||
| 21 | 97 | if (!ctype_digit($number)) { |
|
| 22 | 3 | throw new LogicException( |
|
| 23 | 3 | "Number can only contain numerical characters, found '$number'" |
|
| 24 | ); |
||
| 25 | } |
||
| 26 | |||
| 27 | 94 | $weight = 2; |
|
| 28 | 94 | $sum = 0; |
|
| 29 | |||
| 30 | 94 | for ($pos=strlen($number)-1; $pos>=0; $pos--) { |
|
| 31 | 94 | $tmp = $number[$pos] * $weight; |
|
| 32 | 94 | $sum += ($tmp > 9) ? (1 + ($tmp % 10)) : $tmp; |
|
| 33 | 94 | $weight = ($weight == 2) ? 1 : 2; |
|
| 34 | } |
||
| 35 | |||
| 36 | 94 | $ceil = $sum; |
|
| 37 | |||
| 38 | 94 | while ($ceil % 10 != 0) { |
|
| 39 | 94 | $ceil++; |
|
| 40 | } |
||
| 41 | |||
| 42 | 94 | return (string)($ceil-$sum); |
|
| 43 | } |
||
| 44 | } |
||
| 45 |