We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 8 |
Paths | 6 |
Total Lines | 23 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Tests | 14 |
CRAP Score | 8 |
Changes | 0 |
1 | <?php |
||
25 | 8 | public function validate($input): bool |
|
26 | { |
||
27 | 8 | if (!preg_match('/^[A-Z0-9]{9}$/', $input)) { |
|
28 | 1 | return false; |
|
29 | } |
||
30 | |||
31 | 7 | $weights = [7, 3, 1, 0, 7, 3, 1, 7, 3]; |
|
32 | 7 | $weightedSum = 0; |
|
33 | 7 | for ($i = 0; $i < 9; ++$i) { |
|
34 | 7 | $code = ord($input[$i]); |
|
35 | 7 | if ($i < 3 && $code <= 57) { // 57 is "9" |
|
36 | 1 | return false; |
|
37 | } |
||
38 | |||
39 | 7 | if ($i > 2 && $code >= 65) { // 65 is "A" |
|
40 | 1 | return false; |
|
41 | } |
||
42 | |||
43 | 7 | $difference = $code <= 57 ? 48 : 55; // 48 is "0" |
|
44 | 7 | $weightedSum += ($code - $difference) * $weights[$i]; |
|
45 | } |
||
46 | |||
47 | 5 | return $weightedSum % 10 == $input[3]; |
|
48 | } |
||
50 |