Passed
Push — main ( 5bb7eb...e681d2 )
by Breno
01:41
created

DigitoVerificador::mod11()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
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 = 'CPF 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