Passed
Push — main ( 9fd9c1...757bec )
by Breno
01:56
created

DigitoVerificador::evaluate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 4
b 0
f 0
nc 2
nop 2
dl 0
loc 19
rs 9.8333
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\Rules\Brazilian;
5
6
use BrenoRoosevelt\Validation\AbstractRule;
7
use InvalidArgumentException;
8
use Throwable;
9
10
class DigitoVerificador extends AbstractRule
11
{
12
    const MOD11 = 0;
13
    const MOD10 = 1;
14
15
    public function __construct(private int $algorithm = self::MOD11, ?string $message = 'Dígito verificador inválido')
16
    {
17
        parent::__construct($message);
18
    }
19
20
    public function isValid($input, array $context = []): bool
21
    {
22
        try {
23
            $stringInput = (string) $input;
24
        } catch (Throwable $exception) {
0 ignored issues
show
Unused Code introduced by
catch (\Throwable $exception) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
25
            return false;
26
        }
27
28
        if (preg_match('/^\d$/', $stringInput) !== 1) {
29
            return false;
30
        }
31
32
        $number = substr($stringInput, 0, -1);
33
        $digit = (int) substr($stringInput, -1);
34
        return
35
            $digit === match ($this->algorithm) {
36
                self::MOD11 => self::mod11($number),
37
                self::MOD10 => self::mod10($number),
38
                default => throw new InvalidArgumentException('Algoritmo de cálculo de dígito inválido')
39
            };
40
    }
41
42
    public static function mod11($input, $modReturn = []): int
43
    {
44
        $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

44
        $numbers = array_reverse(/** @scrutinizer ignore-type */ str_split((string) $input));
Loading history...
45
        $factor = [2, 3, 4, 5, 6, 7, 8, 9];
46
        $size = count($factor);
47
        $i = $sum = 0;
48
        foreach ($numbers as $number) {
49
            $sum += $number * $factor[$i++ % $size];
50
        }
51
52
        $mod11 = ($sum * 10 ) % 11;
53
        return $modReturn[$mod11] ?? 11 - $mod11;
54
    }
55
56
    public static function mod10($input): int
57
    {
58
        $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

58
        $numbers = array_reverse(/** @scrutinizer ignore-type */ str_split((string) $input));
Loading history...
59
        $factor = [2, 1];
60
        $size = count($factor);
61
        $i = $sum = 0;
62
        foreach ($numbers as $number) {
63
            $num = $number * $factor[$i++ % $size];
64
            $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

64
            $sum += array_sum(/** @scrutinizer ignore-type */ str_split((string) $num));
Loading history...
65
        }
66
67
        return 10 - ($sum % 10);
68
    }
69
70
    public static function checkCpfCnpjDigits(string $document): bool
71
    {
72
        $document = preg_replace('/\D/', '', $document);
73
        $number = substr($document, 0, -2);
74
        $digits = substr($document, -2);
75
76
        $modReturn = [0 => 0, 1 => 0];
77
        $digit1 = DigitoVerificador::mod11($number, $modReturn);
78
        $digit2 = DigitoVerificador::mod11($number . $digit1, $modReturn);
79
80
        return $digits === ($digit1 . $digit2);
81
    }
82
}
83