Passed
Pull Request — master (#160)
by Pierre
02:55 queued 54s
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 Composer\InstalledVersions;
8
use Exception;
9
use Symfony\Component\Form\Exception\UnexpectedTypeException;
10
use Symfony\Component\Validator\Constraint;
11
12
switch ($version = substr((string) InstalledVersions::getVersion('symfony/validator'), 0, 3)) {
13
    default:
14
        throw new Exception('knplabs/dictionary-bundle is not compatible with the current version of symfony/validator: '.$version);
15
16
    case '6.0':
17
        trait SymfonyCompatibilityTrait
18
        {
19
            public function validate(mixed $value, Constraint $constraint): void
20
            {
21
                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...
22
                    throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Dictionary');
23
                }
24
25
                if (null === $value || '' === $value) {
26
                    return;
27
                }
28
29
                $dictionary = $this->dictionaries[$constraint->name];
30
                $values     = $dictionary->getKeys();
31
32
                if (!\in_array($value, $values, true)) {
33
                    $this->context->addViolation(
34
                        $constraint->message,
35
                        [
36
                            '{{ 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

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