IdentityNumberValidator::validate()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 10
eloc 19
nc 10
nop 2
dl 0
loc 32
rs 7.6666
c 2
b 1
f 1

How to fix   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 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