Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
CPF
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
4 / 4
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 cpfValido
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
1 / 1
7
 formatado
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getNumero
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Core\Models;
4
5use Core\Exceptions\ValidationException;
6
7class CPF
8{
9    private string $numero;
10
11    public function __construct($numero)
12    {
13        if(!$this->cpfValido($numero))
14            throw new ValidationException("CPF inválido", $numero);
15
16        $this->numero = $numero;
17    }
18
19    private function cpfValido($cpf)
20    {
21        if(strlen($cpf) !== 11){
22            return false;
23        }
24
25        $ehUmDosCpfInvalidos = in_array(
26            $cpf,
27            [
28                "00000000000",
29                "11111111111",
30                "22222222222",
31                "33333333333",
32                "44444444444",
33                "55555555555",
34                "66666666666",
35                "77777777777",
36                "88888888888",
37                "99999999999"
38            ]
39        );
40        if($ehUmDosCpfInvalidos){
41            return false;
42        }
43
44        $c = str_split($cpf);
45        $c = array_map(fn($c) => intval($c), $c);
46
47        $somaPrimeiroDigito =
48                $c[0] * 10 +
49                $c[1] * 9 +
50                $c[2] * 8 +
51                $c[3] * 7 +
52                $c[4] * 6 +
53                $c[5] * 5 +
54                $c[6] * 4 +
55                $c[7] * 3 +
56                $c[8] * 2;
57        $restoSomaPrimeiroDigito = $somaPrimeiroDigito % 11;
58
59        $primeiroDigito = $restoSomaPrimeiroDigito < 2
60            ? 0 : 11 - $restoSomaPrimeiroDigito;
61
62        $somaSegundoDigito =
63                $c[0] * 11 +
64                $c[1] * 10 +
65                $c[2] * 9 +
66                $c[3] * 8 +
67                $c[4] * 7 +
68                $c[5] * 6 +
69                $c[6] * 5 +
70                $c[7] * 4 +
71                $c[8] * 3 +
72                $c[9] * 2;
73
74        $restoSomaSegundoDigito = $somaSegundoDigito % 11;
75        $segundoDigito = $restoSomaSegundoDigito < 2
76            ? 0 : 11 - $restoSomaSegundoDigito;
77
78        if(
79            $c[9] === $primeiroDigito
80            && $c[10] === $segundoDigito
81        ){
82            return true;
83        }
84
85        return false;
86    }
87
88    public function formatado()
89    {
90        $cpf =
91            substr($this->numero, 0, 3).".".
92            substr($this->numero, 3, 3).".".
93            substr($this->numero, 6, 3)."-".
94            substr($this->numero, 9, 2);
95
96        return $cpf;
97    }
98
99    public function getNumero()
100    {
101        return $this->numero;
102    }
103}