Test Setup Failed
Pull Request — master (#31)
by
unknown
07:53
created

Cnpj::getMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types = 1);
3
namespace CpfCnpjValidation;
4
5
use Zend\Validator\AbstractValidator;
6
7
class Cnpj extends AbstractValidator
8
{
9
10 4
    public function isValid($value)
11
    {
12 4
        if (empty($value)) {
13 1
            return false;
14
        }
15 3
        $cnpj = preg_replace('/[^0-9]/', '', (string) $value);
16
        // Valida tamanho
17 3
        if (strlen($cnpj) > 14) {
18 1
            return false;
19
        }
20 2
        $cnpj = str_pad($cnpj, 14, '0', STR_PAD_LEFT);
21
        // Valida primeiro dígito verificador
22 2 View Code Duplication
        for ($i = 0, $j = 5, $soma = 0; $i < 12; $i ++) {
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...
23 2
            $soma += $cnpj{$i} * $j;
24 2
            $j = ($j == 2) ? 9 : $j - 1;
25
        }
26 2
        $resto = $soma % 11;
27 2
        if ($cnpj{12} != ($resto < 2 ? 0 : 11 - $resto)) {
28 1
            return false;
29
        }
30
        // Valida segundo dígito verificador
31 2 View Code Duplication
        for ($i = 0, $j = 6, $soma = 0; $i < 13; $i ++) {
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...
32 2
            $soma += $cnpj{$i} * $j;
33 2
            $j = ($j == 2) ? 9 : $j - 1;
34
        }
35 2
        $resto = $soma % 11;
36 2
        return $cnpj{13} == ($resto < 2 ? 0 : 11 - $resto);
37
    }
38
}
39