Passed
Push — master ( 355586...94527c )
by Vincent
04:50
created

ConstraintValueValidator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 31
c 1
b 0
f 0
dl 0
loc 99
ccs 32
cts 32
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A empty() 0 7 2
A onTransformerException() 0 19 3
A validate() 0 20 3
A constraints() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
namespace Bdf\Form\Validator;
4
5
use Bdf\Form\ElementInterface;
6
use Bdf\Form\Error\FormError;
7
use Exception;
8
use Symfony\Component\Validator\Constraint;
9
10
/**
11
 * Value validator using symfony constraint
12
 * The element will be used as "root" context object on the symfony validator
13
 *
14
 * @template T
15
 * @implements ValueValidatorInterface<T>
16
 */
17
final class ConstraintValueValidator implements ValueValidatorInterface
18
{
19
    /**
20
     * @var self
21
     */
22
    private static $emptyInstance;
23
24
    /**
25
     * @var Constraint[]
26
     */
27
    private $constraints;
28
29
    /**
30
     * @var TransformerExceptionConstraint
31
     */
32
    private $transformerExceptionConstraint;
33
34
35
    /**
36
     * ConstraintValueValidator constructor.
37
     *
38
     * @param Constraint[] $constraints
39
     * @param TransformerExceptionConstraint|null $transformerExceptionConstraint
40
     */
41 390
    public function __construct(array $constraints = [], ?TransformerExceptionConstraint $transformerExceptionConstraint = null)
42
    {
43 390
        $this->constraints = $constraints;
44 390
        $this->transformerExceptionConstraint = $transformerExceptionConstraint ?? new TransformerExceptionConstraint();
45 390
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 372
    public function validate($value, ElementInterface $element): FormError
51
    {
52 372
        if (!$this->constraints) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->constraints of type Symfony\Component\Validator\Constraint[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
53 208
            return FormError::null();
54
        }
55
56 204
        $root = $element->root();
57
58
        /** @psalm-suppress TooManyArguments */
59 204
        $errors = $root->getValidator()
60 204
            ->startContext($element)
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Component\Valida...terface::startContext() has too many arguments starting with $element. ( Ignorable by Annotation )

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

60
            ->/** @scrutinizer ignore-call */ startContext($element)

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
61 204
            ->validate($value, $this->constraints, $root->constraintGroups())
62 204
            ->getViolations()
63
        ;
64
65 204
        if ($errors->has(0)) {
66 172
            return FormError::violation($errors->get(0));
67
        }
68
69 107
        return FormError::null();
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 35
    public function onTransformerException(Exception $exception, $value, ElementInterface $element): FormError
76
    {
77 35
        if ($this->transformerExceptionConstraint->ignoreException) {
78 15
            return FormError::null();
79
        }
80
81
        /** @psalm-suppress TooManyArguments */
82 21
        $errors = $element->root()
83 21
            ->getValidator()
84 21
            ->startContext($element)
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Component\Valida...terface::startContext() has too many arguments starting with $element. ( Ignorable by Annotation )

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

84
            ->/** @scrutinizer ignore-call */ startContext($element)

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
85 21
            ->validate($value, $this->transformerExceptionConstraint->withException($exception))
86 21
            ->getViolations()
87
        ;
88
89 21
        if ($errors->has(0)) {
90 21
            return FormError::violation($errors->get(0));
91
        }
92
93 1
        return FormError::null();
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 41
    public function constraints(): array
100
    {
101 41
        return $this->constraints;
102
    }
103
104
    /**
105
     * Get the empty value validator instance
106
     *
107
     * @return ConstraintValueValidator<mixed>
108
     */
109 311
    public static function empty(): self
110
    {
111 311
        if (self::$emptyInstance) {
112 311
            return self::$emptyInstance;
113
        }
114
115 1
        return self::$emptyInstance = new self();
116
    }
117
}
118