Completed
Push — master ( 955a0b...b4506e )
by Adelar
02:56
created

Cnpj::isValid()   D

Complexity

Conditions 10
Paths 23

Size

Total Lines 28
Code Lines 18

Duplication

Lines 8
Ratio 28.57 %

Code Coverage

Tests 13
CRAP Score 10.2368

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 8
loc 28
ccs 13
cts 15
cp 0.8667
rs 4.8196
cc 10
eloc 18
nc 23
nop 1
crap 10.2368

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 2
    public function isValid($value)
21
    {
22 2
        if (empty($value)) {
23
            return false;
24 2
        }
25
        $cnpj = preg_replace('/[^0-9]/', '', (string) $value);
26
        // Valida tamanho
27
        if (strlen($cnpj) > 14) {
28 2
            return false;
29 2
        }
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
            $j = ($j == 2) ? 9 : $j - 1;
35
        }
36
        $resto = $soma % 11;
37 2
        if ($cnpj{12} != ($resto < 2 ? 0 : 11 - $resto)) {
38 2
            return false;
39 2
        }
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
            $j = ($j == 2) ? 9 : $j - 1;
44
        }
45
        $resto = $soma % 11;
46
        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
    public function getMessages()
61
    {
62
        return [];
63
    }
64
}
65