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

CNPJValidator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 59
Duplicated Lines 59.32 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 13
lcom 1
cbo 4
dl 35
loc 59
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C validate() 35 53 13

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