Passed
Pull Request — master (#160)
by Pierre
02:22
created

SymfonyCompatibilityTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 21
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 19 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Knp\DictionaryBundle\Validator\Constraints\DictionaryValidator;
6
7
use Symfony\Component\Form\Exception\UnexpectedTypeException;
8
use Symfony\Component\HttpKernel\Kernel;
9
use Symfony\Component\Validator\Constraint;
10
11
switch (true) {
12
    case Kernel::VERSION_ID >= 60000:
13
        trait SymfonyCompatibilityTrait
14
        {
15
            public function validate(mixed $value, Constraint $constraint): void
16
            {
17
                if (!$constraint instanceof Dictionary) {
0 ignored issues
show
Bug introduced by
The type Knp\DictionaryBundle\Val...aryValidator\Dictionary 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...
18
                    throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Dictionary');
19
                }
20
21
                if (null === $value || '' === $value) {
22
                    return;
23
                }
24
25
                $dictionary = $this->dictionaries[$constraint->name];
26
                $values     = $dictionary->getKeys();
27
28
                if (!\in_array($value, $values, true)) {
29
                    $this->context->addViolation(
30
                        $constraint->message,
31
                        [
32
                            '{{ key }}'  => $this->varToString($value),
0 ignored issues
show
Bug introduced by
It seems like varToString() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
                            '{{ key }}'  => $this->/** @scrutinizer ignore-call */ varToString($value),
Loading history...
33
                            '{{ keys }}' => implode(', ', array_map([$this, 'varToString'], $values)),
34
                        ]
35
                    );
36
                }
37
            }
38
        }
39
40
        break;
41
42
    case Kernel::VERSION_ID < 60000 && Kernel::VERSION_ID >= 50400:
43
        trait SymfonyCompatibilityTrait
44
        {
45
            public function validate($value, Constraint $constraint): void
46
            {
47
                if (!$constraint instanceof Dictionary) {
48
                    throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Dictionary');
49
                }
50
51
                if (null === $value || '' === $value) {
52
                    return;
53
                }
54
55
                $dictionary = $this->dictionaries[$constraint->name];
56
                $values     = $dictionary->getKeys();
57
58
                if (!\in_array($value, $values, true)) {
59
                    $this->context->addViolation(
60
                        $constraint->message,
61
                        [
62
                            '{{ key }}'  => $this->varToString($value),
63
                            '{{ keys }}' => implode(', ', array_map([$this, 'varToString'], $values)),
64
                        ]
65
                    );
66
                }
67
            }
68
        }
69
70
        break;
71
}
72