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
Branch develop (0737e3)
by Maximilian
11:49
created

ClassMetadata   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 0
dl 0
loc 172
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A getPropertyValue() 0 16 4
A getPropertyName() 0 13 4
A modifyProperty() 0 19 3
A checkProperty() 0 15 3
A resolveCache() 0 12 3
1
<?php
2
3
namespace Ma27\ApiKeyAuthenticationBundle\Service\Mapping;
4
5
use ReflectionProperty;
6
7
/**
8
 * Metadata object that contains the necessary data of the user model.
9
 *
10
 * @internal This code is part of the internal API to gather the appropriate model information and shouldn't be used for else use-cases
11
 */
12
class ClassMetadata
13
{
14
    const LOGIN_PROPERTY = 'login';
15
    const PASSWORD_PROPERTY = 'password';
16
    const API_KEY_PROPERTY = 'apiKey';
17
    const LAST_ACTION_PROPERTY = 'lastAction';
18
19
    /**
20
     * @var int[]
21
     */
22
    private static $requiredProperties = array(
23
        self::LOGIN_PROPERTY,
24
        self::PASSWORD_PROPERTY,
25
        self::API_KEY_PROPERTY,
26
    );
27
28
    /**
29
     * @var ReflectionProperty[]
30
     */
31
    private $properties = array();
32
33
    /**
34
     * @var string[]
35
     */
36
    private $lazyPropertyNameCache = array();
37
38
    /**
39
     * @var string[]
40
     */
41
    private $lazyValueCache = array();
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param ReflectionProperty|string[] $properties
47
     *
48
     * @throws \InvalidArgumentException If one necessary property is missing
49
     */
50
    public function __construct(array $properties)
51
    {
52
        $this->properties = $properties;
0 ignored issues
show
Documentation Bug introduced by
It seems like $properties of type array<integer,string> is incompatible with the declared type array<integer,object<ReflectionProperty>> of property $properties.

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...
53
54
        foreach (self::$requiredProperties as $property) {
55
            if (!isset($this->properties[$property])) {
56
                throw new \InvalidArgumentException(sprintf(
57
                    'Missing required property "%s"!',
58
                    $property
59
                ));
60
            }
61
        }
62
    }
63
64
    /**
65
     * Gets the value of the given property.
66
     *
67
     * @param object $user
68
     * @param string $property
69
     * @param bool   $strict
70
     *
71
     * @return mixed
72
     */
73
    public function getPropertyValue($user, $property = self::LOGIN_PROPERTY, $strict = false)
74
    {
75
        if ($this->checkProperty($property, $strict)) {
76
            $oid = spl_object_hash($user);
77
            if (null !== $cacheHit = $this->resolveCache($oid, $property)) {
78
                return $cacheHit;
79
            }
80
81
            if (is_string($this->properties[$property])) {
82
                $this->properties[$property] = new ReflectionProperty($user, $this->properties[$property]);
83
            }
84
            $this->properties[$property]->setAccessible(true);
85
86
            return $this->lazyValueCache[$oid][$property] = $this->properties[$property]->getValue($user);
87
        }
88
    }
89
90
    /**
91
     * Gets the name of a specific property by its metadata constant.
92
     *
93
     * @param string $property
94
     * @param bool   $strict
95
     *
96
     * @return null|string
97
     */
98
    public function getPropertyName($property = self::LOGIN_PROPERTY, $strict = false)
99
    {
100
        if ($this->checkProperty($property, $strict)) {
101
            if (is_string($this->properties[$property])) {
102
                return $this->properties[$property];
103
            }
104
            if (isset($this->lazyPropertyNameCache[$property])) {
105
                return $this->lazyPropertyNameCache[$property];
106
            }
107
108
            return $this->lazyPropertyNameCache[$property] = $this->properties[$property]->getName();
109
        }
110
    }
111
112
    /**
113
     * Modifies a property and clears the cache.
114
     *
115
     * @param object $user
116
     * @param mixed  $newValue
117
     * @param string $property
118
     */
119
    public function modifyProperty($user, $newValue, $property = self::LOGIN_PROPERTY)
120
    {
121
        $this->checkProperty($property, true);
122
123
        $propertyObject = $this->properties[$property];
124
        if (is_string($propertyObject)) {
125
            $this->properties[$property] = $propertyObject = new ReflectionProperty($user, $propertyObject);
126
        }
127
128
        $propertyObject->setAccessible(true);
129
        $propertyObject->setValue($user, $newValue);
130
131
        $oid = spl_object_hash($user);
132
        if (!array_key_exists($oid, $this->lazyValueCache)) {
133
            $this->lazyValueCache[$oid] = array();
134
        }
135
136
        $this->lazyValueCache[$oid][$property] = $newValue;
137
    }
138
139
    /**
140
     * Validates a property.
141
     *
142
     * @param string $property
143
     * @param bool   $strict
144
     *
145
     * @return bool
146
     */
147
    private function checkProperty($property = self::LOGIN_PROPERTY, $strict = false)
148
    {
149
        if (!isset($this->properties[$property])) {
150
            if ($strict) {
151
                throw new \LogicException(sprintf(
152
                    'Cannot get property "%s"!',
153
                    $property
154
                ));
155
            }
156
157
            return false;
158
        }
159
160
        return true;
161
    }
162
163
    /**
164
     * Resolves the lazy value cache.
165
     *
166
     * @param string $oid
167
     * @param string $property
168
     *
169
     * @return mixed|null
170
     */
171
    private function resolveCache($oid, $property)
172
    {
173
        if (isset($this->lazyValueCache[$oid])) {
174
            if (isset($this->lazyValueCache[$oid][$property])) {
175
                return $this->lazyValueCache[$oid][$property];
176
            }
177
178
            return;
179
        }
180
181
        $this->lazyValueCache[$oid] = array();
182
    }
183
}
184