Passed
Push — dev ( 40f0b5...3a2e98 )
by Fike
02:50
created

ValidParameterNameValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 23.33 %

Importance

Changes 0
Metric Value
wmc 4
dl 7
loc 30
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 7 24 4

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
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Mapping\Validation\Constraint;
6
7
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\Infrastructure\Registry;
8
use Symfony\Component\Validator\Constraint;
9
use Symfony\Component\Validator\ConstraintValidator;
10
11
class ValidParameterNameValidator extends ConstraintValidator
12
{
13
    /**
14
     * @inheritDoc
15
     * @param ValidParameterName $constraint
16
     */
17
    public function validate($value, Constraint $constraint)
18
    {
19
        $context = $this->context;
20
        if (!is_string($value)) {
21
            $context
22
                ->buildViolation($constraint->illegalValueMessage)
23
                ->addViolation();
24
            return;
25
        }
26
        $parameter = Registry::getInstance()->find($value);
27
        if (!$parameter) {
28
            $context
29
                ->buildViolation($constraint->missingParameterMessage)
30
                ->setParameter('{{ name }}', $value)
31
                ->addViolation();
32
            return;
33
        }
34 View Code Duplication
        if ($value !== $parameter->getId()) {
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...
35
            $this
36
                ->context
37
                ->buildViolation($constraint->message)
38
                ->setParameter('{{ id }}', $parameter->getId())
39
                ->setParameter('{{ name }}', $value)
40
                ->addViolation();
41
        }
42
    }
43
}
44