B510   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 153
Duplicated Lines 24.84 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 1
dl 38
loc 153
ccs 0
cts 78
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A postValidation() 0 19 4
F validaCPF() 38 71 22

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace NFePHP\EFD\Elements\ICMSIPI;
4
5
use NFePHP\EFD\Common\Element;
6
use NFePHP\EFD\Common\ElementInterface;
7
use \stdClass;
8
9
class B510 extends Element implements ElementInterface
10
{
11
    const REG = 'B510';
12
    const LEVEL = 3;
13
    const PARENT = 'B500';
14
15
    protected $parameters = [
16
        'IND_PROF' => [
17
            'type'     => 'string',
18
            'regex'    => '^[0|1]$',
19
            'required' => true,
20
            'info'     => 'Indicador de habilitação: '
21
            .'0- Profissional habilitado '
22
            .'1- Profissional não habilitado',
23
            'format'   => ''
24
        ],
25
        'IND_ESC' => [
26
            'type'     => 'string',
27
            'regex'    => '^[0|1]$',
28
            'required' => true,
29
            'info'     => 'Indicador de escolaridade: '
30
            .'0- Nível superior '
31
            .'1- Nível médio',
32
            'format'   => ''
33
        ],
34
        'IND_SOC' => [
35
            'type'     => 'string',
36
            'regex'    => '^[0|1]$',
37
            'required' => true,
38
            'info'     => 'Indicador de participação societária: '
39
            .'0- Sócio '
40
            .'1- Não sócio',
41
            'format'   => ''
42
        ],
43
        'CPF' => [
44
            'type'     => 'string',
45
            'regex'    => '^\d{11}$',
46
            'required' => true,
47
            'info'     => 'Número de inscrição do profissional no CPF.',
48
            'format'   => ''
49
        ],
50
        'NOME' => [
51
            'type'     => 'string',
52
            'regex'    => '^.{1,100}$',
53
            'required' => true,
54
            'info'     => 'Nome do profissional',
55
            'format'   => ''
56
        ]
57
    ];
58
59
    /**
60
     * Constructor
61
     * @param \stdClass $std
62
     */
63
    public function __construct(\stdClass $std)
64
    {
65
        parent::__construct(self::REG);
66
        $this->std = $this->standarize($std);
67
        $this->postValidation();
68
    }
69
70
    public function postValidation()
71
    {
72
        /*
73
         * Campo 04 (IND_SOC) Validação: O profissional sócio necessariamente tem de ser
74
         * habilitado (campo IND_PROF preenchido com “0”)
75
         */
76
        if ($this->std->ind_soc == '1' && $this->std->ind_prof != '0') {
77
            throw new \InvalidArgumentException("[" . self::REG . "] O profissional sócio necessariamente tem de "
78
            ."ser habilitado (campo IND_PROF preenchido com “0”)");
79
        }
80
81
        /*
82
         * Campo 05 (CPF) Validação: será conferido o dígito verificador (DV) do
83
         * CPF informado.
84
         */
85
        if (!$this->validaCPF($this->std->cpf)) {
86
            throw new \InvalidArgumentException("[" . self::REG . "] O CPF informado não é válido.");
87
        }
88
    }
89
90
    private function validaCPF($cpf)
91
    {
92
        //Cria um array com apenas os digitos numéricos
93
        $j=0;
94
        $num=[];
95
        for ($i=0; $i<(strlen($cpf)); $i++) {
96
            if (is_numeric($cpf[$i])) {
97
                $num[$j]=(int)$cpf[$i];
98
                $j++;
99
            }
100
        }
101
102
        //Verifica a quantidade de digitos
103
        if (count($num)!=11) {
104
            $validaCPF=false;
105
        } else { //Filtra combinações como 00000000000 e 22222222222
106
            for ($i=0; $i<10; $i++) {
107
                if ($num[0]==$i && $num[1]==$i && $num[2]==$i && $num[3]==$i
108
                && $num[4]==$i && $num[5]==$i && $num[6]==$i && $num[7]==$i
109
                && $num[8]==$i) {
110
                    $validaCPF=false;
111
                    break;
112
                }
113
            }
114
        }
115
116
        //Calcula e compara o primeiro dígito verificador
117 View Code Duplication
        if (!isset($validaCPF)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
            $j=10;
119
            $multiplica=[];
120
            for ($i=0; $i<9; $i++) {
121
                $multiplica[$i]=$num[$i]*$j;
122
                $j--;
123
            }
124
            $soma = array_sum($multiplica);
125
            $resto = $soma%11;
126
            if ($resto<2) {
127
                $dg=0;
128
            } else {
129
                $dg=11-$resto;
130
            }
131
            if ($dg!=$num[9]) {
132
                $validaCPF=false;
133
            }
134
        }
135
136
        //Calcula e compara o segundo dígito verificador.
137 View Code Duplication
        if (!isset($validaCPF)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
            $j=11;
139
            $multiplica=[];
140
            for ($i=0; $i<10; $i++) {
141
                $multiplica[$i]=$num[$i]*$j;
142
                $j--;
143
            }
144
            $soma = array_sum($multiplica);
145
            $resto = $soma%11;
146
            if ($resto<2) {
147
                $dg=0;
148
            } else {
149
                $dg=11-$resto;
150
            }
151
            if ($dg!=$num[10]) {
152
                $validaCPF=false;
153
            } else {
154
                $validaCPF=true;
155
            }
156
        }
157
        
158
        //Retorna o resutado (booleano)
159
        return $validaCPF;
160
    }
161
}
162