Test Failed
Pull Request — master (#570)
by Alexis
17:07
created

DataCollectingValidator::onKernelRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Liip/FunctionalTestBundle
7
 *
8
 * (c) Lukas Kahwe Smith <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Liip\FunctionalTestBundle\Validator;
15
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
18
use Symfony\Component\HttpKernel\KernelEvents;
19
use Symfony\Component\Validator\ConstraintViolationList;
20
use Symfony\Component\Validator\ConstraintViolationListInterface;
21
use Symfony\Component\Validator\Context\ExecutionContextInterface;
22
use Symfony\Component\Validator\Mapping\MetadataInterface;
23
use Symfony\Component\Validator\Validator\ContextualValidatorInterface;
24
use Symfony\Component\Validator\Validator\ValidatorInterface;
25
26
class DataCollectingValidator implements ValidatorInterface, EventSubscriberInterface
27
{
28
    /**
29
     * @var ValidatorInterface
30
     */
31
    protected $wrappedValidator;
32
33
    /**
34
     * @var ConstraintViolationListInterface
35
     */
36
    protected $lastErrors;
37
38
    public function __construct(ValidatorInterface $wrappedValidator)
39
    {
40
        $this->wrappedValidator = $wrappedValidator;
41
        $this->clearLastErrors();
42
    }
43
44
    public function clearLastErrors(): void
45
    {
46
        $this->lastErrors = new ConstraintViolationList();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Symfony\Component\V...nstraintViolationList() of type object<Symfony\Component...onstraintViolationList> is incompatible with the declared type object<Symfony\Component...ViolationListInterface> of property $lastErrors.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
    }
48
49
    public function getLastErrors(): ConstraintViolationListInterface
50
    {
51
        return $this->lastErrors;
52
    }
53
54
    public function getMetadataFor($value): MetadataInterface
55
    {
56
        return $this->wrappedValidator->getMetadataFor($value);
57
    }
58
59
    public function hasMetadataFor($value): bool
60
    {
61
        return $this->wrappedValidator->hasMetadataFor($value);
62
    }
63
64
    public function validate($value, $constraints = null, $groups = null): ConstraintViolationListInterface
65
    {
66
        return $this->lastErrors = $this->wrappedValidator->validate($value, $constraints, $groups);
67
    }
68
69
    public function validateProperty($object, $propertyName, $groups = null): ConstraintViolationListInterface
70
    {
71
        return $this->wrappedValidator->validateProperty($object, $propertyName, $groups);
72
    }
73
74
    public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null): ConstraintViolationListInterface
75
    {
76
        return $this->wrappedValidator->validatePropertyValue($objectOrClass, $propertyName, $value, $groups);
77
    }
78
79
    public function startContext(): ContextualValidatorInterface
80
    {
81
        return $this->wrappedValidator->startContext();
82
    }
83
84
    public function inContext(ExecutionContextInterface $context): ContextualValidatorInterface
85
    {
86
        return $this->wrappedValidator->inContext($context);
87
    }
88
89
    public function onKernelRequest(GetResponseEvent $event): void
90
    {
91
        if ($event->isMasterRequest()) {
92
            $this->clearLastErrors();
93
        }
94
    }
95
96
    public static function getSubscribedEvents(): array
97
    {
98
        return [
99
            KernelEvents::REQUEST => ['onKernelRequest', 99999],
100
        ];
101
    }
102
}
103