GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

DateAfterValidator   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 42
c 2
b 0
f 0
dl 0
loc 75
rs 10
wmc 15

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldValue() 0 15 4
B validate() 0 51 11
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\Form\Validator\Constraints;
13
14
use DateTime;
15
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
16
use Symfony\Component\PropertyAccess\PropertyAccess;
17
use Symfony\Component\Validator\Constraint;
18
use Symfony\Component\Validator\ConstraintValidator;
19
use Symfony\Component\Validator\Exception\InvalidArgumentException;
20
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
21
22
final class DateAfterValidator extends ConstraintValidator
23
{
24
    public function validate($value, Constraint $constraint): void
25
    {
26
        if (!$constraint instanceof DateAfter) {
27
            throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateAfter');
28
        }
29
30
        if (!\is_object($value)) {
31
            throw new InvalidArgumentException(sprintf('Could not validate "%s"', \gettype($value)));
32
        }
33
34
        $firstFieldName  = $constraint->firstField;
35
        $secondFieldName = $constraint->secondField;
36
37
        $firstDate  = $this->getFieldValue($value, $firstFieldName);
38
        $secondDate = $this->getFieldValue($value, $secondFieldName);
39
40
        if (!$constraint->required && null === $firstDate && null === $secondDate) {
41
            return;
42
        }
43
44
        if (null === $firstDate && null !== $secondDate) {
45
            $this->context
46
                ->buildViolation($constraint->emptyMessage)
47
                ->setParameter('%emptyField%', $firstFieldName)
48
                ->setParameter('%field%', $secondFieldName)
49
                ->atPath($firstFieldName)
50
                ->addViolation()
51
            ;
52
53
            return;
54
        }
55
56
        if (null !== $firstDate && null === $secondDate) {
57
            $this->context
58
                ->buildViolation($constraint->emptyMessage)
59
                ->setParameter('%emptyField%', $secondFieldName)
60
                ->setParameter('%field%', $firstFieldName)
61
                ->atPath($secondFieldName)
62
                ->addViolation()
63
            ;
64
65
            return;
66
        }
67
68
        if ($firstDate > $secondDate) {
69
            $this->context
70
                ->buildViolation($constraint->message)
71
                ->setParameter('%firstField%', $firstFieldName)
72
                ->setParameter('%secondField%', $secondFieldName)
73
                ->atPath($secondFieldName)
74
                ->addViolation()
75
            ;
76
        }
77
    }
78
79
    /**
80
     * @param array|object $object
81
     */
82
    private function getFieldValue($object, string $field): ?DateTime
83
    {
84
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
85
86
        try {
87
            $value = $propertyAccessor->getValue($object, $field);
88
        } catch (NoSuchPropertyException $e) {
89
            throw new InvalidArgumentException($e->getMessage());
90
        }
91
92
        if (null !== $value && !$value instanceof DateTime) {
93
            throw new UnexpectedTypeException($value, DateTime::class);
94
        }
95
96
        return $value;
97
    }
98
}
99