IdentityNumberValidator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 20
dl 0
loc 34
rs 10
c 2
b 1
f 1
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 32 10
1
<?php
2
3
namespace Eres\SyliusIyzicoPlugin\Validator\Constraints;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\ConstraintValidator;
7
8
9
class IdentityNumberValidator extends ConstraintValidator
10
{
11
    public function validate($value, Constraint $constraint)
12
    {
13
14
        if (!$constraint instanceof IdentityNumber) {
15
            throw new UnexpectedTypeException($constraint, IdentityNumber::class);
0 ignored issues
show
Bug introduced by
The type Eres\SyliusIyzicoPlugin\...UnexpectedTypeException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
        }
17
18
        if (null === $value || '' === $value) {
19
            return;
20
        }
21
22
        $identityNumberArr = str_split($value);
23
        $identityNumberCount = count($identityNumberArr);
24
        $identityNumberSum = null;
25
        $identityNumberCharacterType = false;
26
27
        for ($i = 0; $i < $identityNumberCount; $i++) {
28
            $identityNumber = $identityNumberArr[$i];
29
            if (!is_numeric($identityNumber)) {
30
                $identityNumberCharacterType = true;
31
                break;
32
            }
33
            if ($i !== $identityNumberCount - 1) {
34
                $identityNumberSum += $identityNumber;
35
            }
36
        }
37
38
39
        if ($identityNumberCount !== 11 || $identityNumberCharacterType || str_split($identityNumberSum)[1] !== $identityNumberArr[$identityNumberCount - 1]) {
40
            $this->context->buildViolation($constraint->message)
41
                ->setParameter('{{ value }}', $value)
42
                ->addViolation();
43
        }
44
    }
45
}
46