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

CPFValidator::validate()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 38
Code Lines 26

Duplication

Lines 14
Ratio 36.84 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 14
loc 38
rs 4.9091
c 1
b 0
f 0
cc 9
eloc 26
nc 9
nop 2
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