Completed
Push — master ( 810e98...a03780 )
by Rafael
16:20
created

CNPJValidator::validate()   C

Complexity

Conditions 13
Paths 81

Size

Total Lines 53
Code Lines 41

Duplication

Lines 35
Ratio 66.04 %
Metric Value
dl 35
loc 53
rs 6.3327
cc 13
eloc 41
nc 81
nop 2

How to fix   Long Method    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
3
namespace Mero\BaseBundle\Validator\Constraints;
4
5
use Symfony\Component\Form\Exception\UnexpectedTypeException;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
9
/**
10
 * @author Rafael Mello <[email protected]>
11
 *
12
 * @api
13
 */
14
class CNPJValidator extends ConstraintValidator
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function validate($value, Constraint $constraint)
20
    {
21
        if (!$constraint instanceof CNPJ) {
22
            throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\CNPJ');
23
        }
24
        if (null === $value || '' === $value) {
25
            return;
26
        }
27
        $valueNumber = preg_replace('/[^0-9]/', '', $value);
28 View Code Duplication
        if (strlen($valueNumber) != 14) {
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...
29
            $this->context->buildViolation($constraint->message)
30
                ->setParameter('{{ value }}', $this->formatValue($value))
31
                ->addViolation();
32
33
            return;
34
        }
35 View Code Duplication
        for ($i = 0, $aux = 5, $count = 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...
36
            $count += $valueNumber{$i}
37
            * $aux;
38
            $aux = ($aux == 2)
39
                ? 9
40
                : $aux - 1;
41
        }
42
        $d1 = $count % 11;
43
        $d1 = $d1 < 2
44
            ? 0
45
            : 11 - $d1;
46 View Code Duplication
        if ($valueNumber{12} != $d1) {
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...
47
            $this->context->buildViolation($constraint->message)
48
                ->setParameter('{{ value }}', $this->formatValue($value))
49
                ->addViolation();
50
51
            return;
52
        }
53 View Code Duplication
        for ($i = 0, $aux = 6, $count = 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...
54
            $count += $valueNumber{$i}
55
            * $aux;
56
            $aux = ($aux == 2)
57
                ? 9
58
                : $aux - 1;
59
        }
60
        $d2 = $count % 11;
61
        $d2 = $d2 < 2
62
            ? 0
63
            : 11 - $d2;
64 View Code Duplication
        if ($valueNumber{13} != $d2) {
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...
65
            $this->context->buildViolation($constraint->message)
66
                ->setParameter('{{ value }}', $this->formatValue($value))
67
                ->addViolation();
68
69
            return;
70
        }
71
    }
72
}
73