Passed
Push — master ( 49293b...25c2e9 )
by Petr
04:03
created

EntityDoesNotExistsValidator   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 2
c 1
b 0
f 1
lcom 1
cbo 3
dl 23
loc 23
ccs 0
cts 13
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 15 15 2

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 AppBundle\Form\Validation;
4
5
use AppBundle\Form\Validation\Infrastructure\EntityValidator;
6
use Symfony\Component\Validator\Constraint;
7
8
/**
9
 * @author Vehsamrak
10
 */
11 View Code Duplication
class EntityDoesNotExistsValidator extends EntityValidator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
12
{
13
14
    /**
15
     * @param mixed $entityFieldValue
16
     * @param EntityDoesNotExists $constraint
17
     */
18
    public function validate($entityFieldValue, Constraint $constraint)
19
    {
20
        $this->checkAnnotationParameters($constraint);
0 ignored issues
show
Compatibility introduced by
$constraint of type object<Symfony\Component\Validator\Constraint> is not a sub-type of object<AppBundle\Form\Va...cture\EntityConstraint>. It seems like you assume a child class of the class Symfony\Component\Validator\Constraint to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
21
22
        $entity = $this->findEntity($entityFieldValue, $constraint);
23
        $entityName = (new \ReflectionClass($constraint->entityClass))->getShortName();
24
25
        if ($entity) {
26
            $this->context->buildViolation($constraint->message)
27
                          ->setParameter('%entityName%', $entityName)
28
                          ->setParameter('%field%', $constraint->entityField)
29
                          ->setParameter('%fieldValue%', $entityFieldValue)
30
                          ->addViolation();
31
        }
32
    }
33
}
34