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.
Passed
Pull Request — master (#41)
by joseph
02:48
created

ValidatedEntityTrait::injectValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
    public function injectValidator(EntityValidatorInterface $validator): void
30
    {
31
        $this->validator = $validator;
32
        $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
    }
34
35
    /**
36
     * @param ValidatorClassMetaData $metadata
37
     *
38
     * @throws DoctrineStaticMetaException
39
     */
40
    public static function loadValidatorMetaData(ValidatorClassMetaData $metadata): void
41
    {
42
        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
        static::loadPropertyValidatorMetaData($metadata);
44
    }
45
46
    /**
47
     * @param ValidatorClassMetaData $metadata
48
     *
49
     * @throws DoctrineStaticMetaException
50
     */
51
    protected static function loadPropertyValidatorMetaData(ValidatorClassMetaData $metadata): void
52
    {
53
        $methodName = '__no_method__';
54
        try {
55
            $staticMethods = static::getStaticMethods();
56
            //now loop through and call them
57
            foreach ($staticMethods as $method) {
58
                $methodName = $method->getName();
59
                if ($methodName === EntityInterface::METHOD_PREFIX_GET_PROPERTY_VALIDATOR_META) {
60
                    continue;
61
                }
62
                if (0 === stripos($methodName, EntityInterface::METHOD_PREFIX_GET_PROPERTY_VALIDATOR_META)) {
63
                    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
    }
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
    public function validateProperty(string $propertyName): void
113
    {
114
        $validator = $this->getValidator();
115
        if ($validator === false) {
116
            return;
117
        }
118
        $validator->validateProperty($propertyName);
119
    }
120
121
    private function getValidator()
122
    {
123
        if (null === $this->validator) {
124
            return false;
125
        }
126
127
        return $this->validator;
128
    }
129
}
130