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

Cnpj   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 13.56 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 76.47%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 11
c 4
b 0
f 0
lcom 0
cbo 0
dl 8
loc 59
ccs 13
cts 17
cp 0.7647
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
D isValid() 8 28 10
A getMessages() 0 4 1

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
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