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.
Completed
Pull Request — master (#51)
by joseph
102:38 queued 99:24
created

ValidatedEntityTrait::injectValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Traits;
4
5
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
6
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\Validation\EntityValidatorInterface;
7
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
8
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData;
9
10
/**
11
 * Trait ValidatedEntityTrait
12
 *
13
 * @package EdmondsCommerce\DoctrineStaticMeta\Entity\Traits
14
 *
15
 * Implements ValidatedEntityInterface
16
 */
17
trait ValidatedEntityTrait
18
{
19
    /**
20
     * @var EntityValidatorInterface
21
     */
22
    protected $validator;
23
24
    /**
25
     * Called in the Entity Constructor
26
     *
27
     * @param EntityValidatorInterface $validator
28
     */
29 2
    public function injectValidator(EntityValidatorInterface $validator): void
30
    {
31 2
        $this->validator = $validator;
32 2
        $this->validator->setEntity($this);
0 ignored issues
show
Bug introduced by
$this of type EdmondsCommerce\Doctrine...ts\ValidatedEntityTrait is incompatible with the type EdmondsCommerce\Doctrine...erfaces\EntityInterface expected by parameter $entity of EdmondsCommerce\Doctrine...rInterface::setEntity(). ( Ignorable by Annotation )

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

32
        $this->validator->setEntity(/** @scrutinizer ignore-type */ $this);
Loading history...
33 2
    }
34
35
    /**
36
     * @param ValidatorClassMetaData $metadata
37
     *
38
     * @throws DoctrineStaticMetaException
39
     */
40 2
    public static function loadValidatorMetaData(ValidatorClassMetaData $metadata): void
41
    {
42 2
        static::$reflectionClass = $metadata->getReflectionClass();
0 ignored issues
show
Bug Best Practice introduced by
The property reflectionClass does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43 2
        static::loadPropertyValidatorMetaData($metadata);
44 2
    }
45
46
    /**
47
     * @param ValidatorClassMetaData $metadata
48
     *
49
     * @throws DoctrineStaticMetaException
50
     */
51 2
    protected static function loadPropertyValidatorMetaData(ValidatorClassMetaData $metadata): void
52
    {
53 2
        $methodName = '__no_method__';
54
        try {
55 2
            $staticMethods = static::getStaticMethods();
56
            //now loop through and call them
57 2
            foreach ($staticMethods as $method) {
58 2
                $methodName = $method->getName();
59 2
                if ($methodName === EntityInterface::METHOD_PREFIX_GET_PROPERTY_VALIDATOR_META) {
60
                    continue;
61
                }
62 2
                if (0 === stripos($methodName, EntityInterface::METHOD_PREFIX_GET_PROPERTY_VALIDATOR_META)) {
63 2
                    static::$methodName($metadata);
64
                }
65
            }
66
        } catch (\Exception $e) {
67
            throw new DoctrineStaticMetaException(
68
                'Exception in ' . __METHOD__ . 'for '
69
                . self::$reflectionClass->getName() . "::$methodName\n\n"
70
                . $e->getMessage()
71
            );
72
        }
73 2
    }
74
75
    /**
76
     * Is the current Entity valid
77
     *
78
     * @return bool
79
     */
80
    public function isValid(): bool
81
    {
82
        $validator = $this->getValidator();
83
        if ($validator === false) {
84
            return true;
85
        }
86
87
        return $validator->isValid();
88
    }
89
90
    /**
91
     * Validate the current Entity
92
     *
93
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException
94
     */
95
    public function validate(): void
96
    {
97
        $validator = $this->getValidator();
98
        if ($validator === false) {
99
            return;
100
        }
101
102
        $validator->validate();
103
    }
104
105
    /**
106
     * Validate a named property in the current Entity
107
     *
108
     * @param string $propertyName
109
     *
110
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException
111
     */
112 15
    public function validateProperty(string $propertyName): void
113
    {
114 15
        $validator = $this->getValidator();
115 15
        if ($validator === false) {
116 13
            return;
117
        }
118 2
        $validator->validateProperty($propertyName);
119 1
    }
120
121 15
    private function getValidator()
122
    {
123 15
        if (null === $this->validator) {
124 13
            return false;
125
        }
126
127 2
        return $this->validator;
128
    }
129
}
130