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 (#96)
by Ross
15:04 queued 12:23
created

ValidatedEntityTrait::validateProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.032
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
     * @param ValidatorClassMetaData $metadata
26
     *
27
     * @throws DoctrineStaticMetaException
28
     */
29 43
    public static function loadValidatorMetaData(ValidatorClassMetaData $metadata): void
30
    {
31 43
        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...
32 43
        static::loadPropertyValidatorMetaData($metadata);
33 43
    }
34
35
    /**
36
     * @param ValidatorClassMetaData $metadata
37
     *
38
     * @throws DoctrineStaticMetaException
39
     */
40 43
    protected static function loadPropertyValidatorMetaData(ValidatorClassMetaData $metadata): void
41
    {
42 43
        $methodName = '__no_method__';
43
        try {
44 43
            $staticMethods = static::getStaticMethods();
45
            //now loop through and call them
46 43
            foreach ($staticMethods as $method) {
47 43
                $methodName = $method->getName();
48 43
                if ($methodName === EntityInterface::METHOD_PREFIX_GET_PROPERTY_VALIDATOR_META) {
49
                    continue;
50
                }
51 43
                if (0 === stripos($methodName, EntityInterface::METHOD_PREFIX_GET_PROPERTY_VALIDATOR_META)) {
52 43
                    static::$methodName($metadata);
53
                }
54
            }
55
        } catch (\Exception $e) {
56
            $reflectionClass = static::getReflectionClass();
57
            throw new DoctrineStaticMetaException(
58
                'Exception in ' . __METHOD__ . 'for '
59
                . $reflectionClass->getName() . "::$methodName\n\n"
60
                . $e->getMessage()
61
            );
62 43
        }
63
    }
64
65
    /**
66
     * Called in the Entity Constructor
67
     *
68
     * @param EntityValidatorInterface $validator
69 75
     */
70
    public function injectValidator(EntityValidatorInterface $validator): void
71 75
    {
72 75
        $this->validator = $validator;
73 75
        $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

73
        $this->validator->setEntity(/** @scrutinizer ignore-type */ $this);
Loading history...
74
    }
75
76
    /**
77
     * Is the current Entity valid
78
     *
79
     * @return bool
80 4
     */
81
    public function isValid(): bool
82 4
    {
83 4
        $validator = $this->getValidator();
84
        if ($validator === false) {
85
            return true;
86
        }
87 4
88
        return $validator->isValid();
89
    }
90 43
91
    private function getValidator()
92 43
    {
93
        if (null === $this->validator) {
94
            return false;
95
        }
96 43
97
        return $this->validator;
98
    }
99
100
    /**
101
     * Validate the current Entity
102
     *
103
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException
104
     */
105
    public function validate(): void
106
    {
107
        $validator = $this->getValidator();
108
        if ($validator === false) {
109
            return;
110
        }
111
112
        $validator->validate();
113
    }
114
115
    /**
116
     * Validate a named property in the current Entity
117
     *
118
     * @param string $propertyName
119
     *
120
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException
121 41
     */
122
    public function validateProperty(string $propertyName): void
123 41
    {
124 41
        $validator = $this->getValidator();
125
        if ($validator === false) {
126
            return;
127 41
        }
128 39
        $validator->validateProperty($propertyName);
129
    }
130
}
131