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

CPFValidator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 44
Duplicated Lines 31.82 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 14
loc 44
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D validate() 14 38 9

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\Validator\Constraint;
6
use Symfony\Component\Validator\ConstraintValidator;
7
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
8
9
/**
10
 * @author Rafael Mello <[email protected]>
11
 *
12
 * @api
13
 */
14
class CPFValidator extends ConstraintValidator
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function validate($value, Constraint $constraint)
20
    {
21
        if (!$constraint instanceof CPF) {
22
            throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\CPF');
23
        }
24
        if (null === $value || '' === $value) {
25
            return;
26
        }
27
        $valueNumber = preg_replace('/[^0-9]/', '', $value);
28 View Code Duplication
        if (strlen($valueNumber) != 11) {
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
        if (!preg_match('/(?!(\d)\1{10})\d{11}/', $valueNumber)) {
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
            $this->context->buildViolation($constraint->message)
37
                ->setParameter('{{ value }}', $this->formatValue($value))
38
                ->addViolation();
39
40
            return;
41
        }
42
        for ($t = 9; $t < 11; ++$t) {
43
            for ($d = 0, $c = 0; $c < $t; ++$c) {
44
                $d += $valueNumber{$c}
45
                * (($t + 1) - $c);
46
            }
47
            $d = ((10 * $d) % 11) % 10;
48
            if ($valueNumber{$c} != $d) {
49
                $this->context->buildViolation($constraint->message)
50
                    ->setParameter('{{ value }}', $this->formatValue($value))
51
                    ->addViolation();
52
53
                return;
54
            }
55
        }
56
    }
57
}
58