Cnpj::getMessages()   A
last analyzed

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
namespace CpfCnpjValidation;
3
4
use Zend\Validator\ValidatorInterface;
5
6
class Cnpj implements ValidatorInterface
7
{
8
9
    /**
10
     * Returns true if and only if $value meets the validation requirements
11
     *
12
     * If $value fails validation, then this method returns false, and
13
     * getMessages() will return an array of messages that explain why the
14
     * validation failed.
15
     *
16
     * @param mixed $value            
17
     * @return bool
18
     * @throws Exception\RuntimeException If validation of $value is impossible
19
     */
20 4
    public function isValid($value)
21
    {
22 4
        if (empty($value)) {
23 1
            return false;
24
        }
25 3
        $cnpj = preg_replace('/[^0-9]/', '', (string) $value);
26
        // Valida tamanho
27 3
        if (strlen($cnpj) > 14) {
28 1
            return false;
29
        }
30 2
        $cnpj = str_pad($cnpj, 14, '0', STR_PAD_LEFT);
31
        // Valida primeiro dígito verificador
32 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...
33 2
            $soma += $cnpj{$i} * $j;
34 2
            $j = ($j == 2) ? 9 : $j - 1;
35
        }
36 2
        $resto = $soma % 11;
37 2
        if ($cnpj{12} != ($resto < 2 ? 0 : 11 - $resto)) {
38 1
            return false;
39
        }
40
        // Valida segundo dígito verificador
41 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...
42 2
            $soma += $cnpj{$i} * $j;
43 2
            $j = ($j == 2) ? 9 : $j - 1;
44
        }
45 2
        $resto = $soma % 11;
46 2
        return $cnpj{13} == ($resto < 2 ? 0 : 11 - $resto);
47
    }
48
49
    /**
50
     * Returns an array of messages that explain why the most recent isValid()
51
     * call returned false.
52
     * The array keys are validation failure message identifiers,
53
     * and the array values are the corresponding human-readable message strings.
54
     *
55
     * If isValid() was never called or if the most recent isValid() call
56
     * returned true, then this method returns an empty array.
57
     *
58
     * @return array
59
     */
60 1
    public function getMessages()
61
    {
62 1
        return [];
63
    }
64
}
65