Passed
Push — master ( 13d022...2fdb27 )
by Dmitry
04:41
created

OrderValidator::validate()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 16.1792

Importance

Changes 0
Metric Value
dl 0
loc 24
c 0
b 0
f 0
ccs 4
cts 17
cp 0.2353
rs 9.2248
cc 5
nc 6
nop 2
crap 16.1792
1
<?php declare(strict_types=1);
2
3
namespace SymfonyBundles\ServiceLayer\Validator\Pagination;
4
5
use Symfony\Component\Validator\Exception;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
9
final class OrderValidator extends ConstraintValidator
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14 2
    public function validate($orders, Constraint $constraint)
15
    {
16 2
        if (!$constraint instanceof Order) {
0 ignored issues
show
Bug introduced by
The class SymfonyBundles\ServiceLa...idator\Pagination\Order does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
17
            throw new Exception\UnexpectedTypeException($constraint, Order::class);
18
        }
19
20 2
        foreach ($orders as $field => $order) {
21
            if (!\in_array($order, $constraint->orders, true)) {
22
                $this->context
23
                    ->buildViolation($constraint->orderMessage)
24
                    ->setParameter('{{ field }}', $field)
25
                    ->setParameter('{{ value }}', $order)
26
                    ->addViolation();
27
            }
28
29
            if (!\preg_match($constraint->fieldPattern, $field)) {
30
                $this->context
31
                    ->buildViolation($constraint->fieldMessage)
32
                    ->setParameter('{{ field }}', $field)
33
                    ->setParameter('{{ value }}', $order)
34
                    ->addViolation();
35
            }
36
        }
37 2
    }
38
}
39