1 | <?php |
||||
2 | |||||
3 | namespace Milwad\LaravelValidate\Rules; |
||||
4 | |||||
5 | use Illuminate\Contracts\Validation\Rule; |
||||
6 | |||||
7 | class ValidImei implements Rule |
||||
8 | { |
||||
9 | /** |
||||
10 | * Check IMEI is valid. |
||||
11 | */ |
||||
12 | public function passes($attribute, $value): bool |
||||
13 | { |
||||
14 | $imei = $value; |
||||
15 | |||||
16 | if (strlen($imei) != 15 || ! ctype_digit($imei)) { |
||||
17 | return false; |
||||
18 | } |
||||
19 | |||||
20 | $digits = str_split($imei); // Get digits |
||||
21 | $imei_last = array_pop($digits); // Remove last digit, and store it |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
22 | $log = []; |
||||
23 | |||||
24 | foreach ($digits as $key => $n) { |
||||
25 | if ($key & 1) { |
||||
26 | $double = str_split($n * 2); // Get double digits |
||||
27 | $n = array_sum($double); // Sum double digits |
||||
0 ignored issues
–
show
It seems like
$double can also be of type true ; however, parameter $array of array_sum() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
28 | } |
||||
29 | |||||
30 | $log[] = $n; // Append log |
||||
31 | } |
||||
32 | $sum = array_sum($log) * 9; // Sum log & multiply by 9 |
||||
33 | |||||
34 | return substr($sum, -1) === $imei_last; |
||||
35 | } |
||||
36 | |||||
37 | /** |
||||
38 | * Get the validation error message. |
||||
39 | */ |
||||
40 | public function message(): string |
||||
41 | { |
||||
42 | return __('validate.imei'); |
||||
43 | } |
||||
44 | } |
||||
45 |