Passed
Push — main ( b6fdcf...be384a )
by Breno
01:39
created

DigitoVerificador::mod10()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\Rules\Brazilian;
5
6
use Attribute;
7
use BrenoRoosevelt\Validation\AbstractValidation;
8
9
#[Attribute(Attribute::TARGET_PROPERTY)]
10
class DigitoVerificador extends AbstractValidation
11
{
12
    public function __construct(?string $message = 'Dígito verificador inválido')
13
    {
14
        parent::__construct($message);
15
    }
16
17
    public function evaluate($input, array $context = []): bool
18
    {
19
        $numbers = preg_replace('/\D/', '', (string) $input);
20
        $number = substr($numbers, 0, -1);
21
        $digit = (int) substr($numbers, -1);
22
        return self::mod11($number) === $digit;
23
    }
24
25
    public static function mod11($input): int
26
    {
27
        $numbers = array_reverse(str_split((string) $input));
0 ignored issues
show
Bug introduced by
It seems like str_split((string)$input) can also be of type true; however, parameter $array of array_reverse() 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
        $numbers = array_reverse(/** @scrutinizer ignore-type */ str_split((string) $input));
Loading history...
28
        $factor = [2, 3, 4, 5, 6, 7, 8, 9];
29
        $size = count($factor);
30
        $i = $sum = 0;
31
        foreach ($numbers as $number) {
32
            $sum += $number * $factor[$i++ % $size];
33
        }
34
35
        return 11 - (($sum * 10 ) % 11);
36
    }
37
38
    public static function mod10($input): int
39
    {
40
        $numbers = array_reverse(str_split((string) $input));
0 ignored issues
show
Bug introduced by
It seems like str_split((string)$input) can also be of type true; however, parameter $array of array_reverse() 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

40
        $numbers = array_reverse(/** @scrutinizer ignore-type */ str_split((string) $input));
Loading history...
41
        $factor = [2, 1];
42
        $size = count($factor);
43
        $i = $sum = 0;
44
        foreach ($numbers as $number) {
45
            $num = $number * $factor[$i++ % $size];
46
            $sum += array_sum(str_split((string) $num));
0 ignored issues
show
Bug introduced by
It seems like str_split((string)$num) 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

46
            $sum += array_sum(/** @scrutinizer ignore-type */ str_split((string) $num));
Loading history...
47
        }
48
49
        return 10 - ($sum % 10);
50
    }
51
}
52