ValidImei::message()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
It seems like $digits can also be of type true; however, parameter $array of array_pop() 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 ignore-type  annotation

21
        $imei_last = array_pop(/** @scrutinizer ignore-type */ $digits); // Remove last digit, and store it
Loading history...
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
Bug introduced by
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 ignore-type  annotation

27
                $n = array_sum(/** @scrutinizer ignore-type */ $double); // Sum double digits
Loading history...
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