Passed
Push — master ( 4d96b0...be450e )
by Petr
04:08
created

EntityDoesNotExistsValidator::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 15
loc 15
ccs 11
cts 11
cp 1
rs 9.4285
c 1
b 0
f 1
cc 2
eloc 10
nc 2
nop 2
crap 2
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 7
    public function validate($entityFieldValue, Constraint $constraint)
19
    {
20 7
        $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 7
        $entity = $this->findEntity($entityFieldValue, $constraint);
23 7
        $entityName = (new \ReflectionClass($constraint->entityClass))->getShortName();
24
25 7
        if ($entity) {
26 2
            $this->context->buildViolation($constraint->message)
27 2
                          ->setParameter('%entityName%', $entityName)
28 2
                          ->setParameter('%field%', $constraint->entityField)
29 2
                          ->setParameter('%fieldValue%', $entityFieldValue)
30 2
                          ->addViolation();
31
        }
32 7
    }
33
}
34