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

SymfonyCompatibilityTrait::validate()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 19
rs 9.6111
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\HttpKernel\Kernel;
11
use Symfony\Component\Validator\Constraint;
12
13
switch (substr(InstalledVersions::getVersion('symfony/validator'), 3)) {
0 ignored issues
show
Bug introduced by
It seems like Composer\InstalledVersio...on('symfony/validator') can also be of type null; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

13
switch (substr(/** @scrutinizer ignore-type */ InstalledVersions::getVersion('symfony/validator'), 3)) {
Loading history...
14
    default:
15
        throw new Exception('knplabs/dictionary-bundle is not compatible with the current version of symfony/validator');
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