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
Push — master ( 017a4b...9acd45 )
by Maximilian
11:50 queued 09:23
created

ClassMetadata   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 86.05%

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 14
c 4
b 3
f 0
lcom 1
cbo 0
dl 0
loc 154
ccs 37
cts 43
cp 0.8605
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A getPropertyValue() 0 17 4
A getPropertyName() 0 10 3
A modifyProperty() 0 10 1
A checkProperty() 0 15 3
1
<?php
2
3
namespace Ma27\ApiKeyAuthenticationBundle\Model\User;
4
5
use ReflectionClass;
6
use ReflectionProperty;
7
8
/**
9
 * Metadata object that contains the necessary data of the user model.
10
 *
11
 * @internal This code is part of the internal API to gather the appropriate model information and shouldn't be used for else use-cases
12
 */
13
class ClassMetadata
14
{
15
    const LOGIN_PROPERTY = 1;
16
    const PASSWORD_PROPERTY = 2;
17
    const API_KEY_PROPERTY = 3;
18
    const LAST_ACTION_PROPERTY = 4;
19
20
    /**
21
     * @var int[]
22
     */
23
    private static $necessaryProperties = array(
24
        self::LOGIN_PROPERTY    => 'login',
25
        self::PASSWORD_PROPERTY => 'password',
26
        self::API_KEY_PROPERTY  => 'apiKey',
27
    );
28
29
    /**
30
     * @var ReflectionClass
31
     */
32
    private $reflectionInstance;
33
34
    /**
35
     * @var string
36
     */
37
    private $className;
38
39
    /**
40
     * @var ReflectionProperty[]
41
     */
42
    private $properties = array();
43
44
    /**
45
     * @var string[]
46
     */
47
    private $lazyPropertyNameCache = array();
48
49
    /**
50
     * @var string[]
51
     */
52
    private $lazyValueCache = array();
53
54
    /**
55
     * Constructor.
56
     *
57
     * @param ReflectionClass      $reflection
58
     * @param string               $className
59
     * @param ReflectionProperty[] $properties
60
     *
61
     * @throws \InvalidArgumentException If one necessary property is missing
62
     */
63 10
    public function __construct(ReflectionClass $reflection, $className, array $properties)
64
    {
65 10
        $this->reflectionInstance = $reflection;
66 10
        $this->className = (string) $className;
67 10
        $this->properties = $properties;
68
69 10
        foreach (self::$necessaryProperties as $propertyIndex => $alias) {
70 10
            if (!isset($this->properties[$propertyIndex])) {
71 1
                throw new \InvalidArgumentException(sprintf(
72 1
                    'Missing required property "%s"!',
73
                    $alias
74 1
                ));
75
            }
76 10
        }
77 9
    }
78
79
    /**
80
     * Gets the value of the given property.
81
     *
82
     * @param object $user
83
     * @param int    $property
84
     * @param bool   $strict
85
     *
86
     * @return mixed
87
     */
88 3
    public function getPropertyValue($user, $property = self::LOGIN_PROPERTY, $strict = false)
89
    {
90 3
        if ($this->checkProperty($property, $strict)) {
91 3
            $oid = spl_object_hash($user);
92 3
            if (isset($this->lazyValueCache[$oid])) {
93
                if (isset($this->lazyValueCache[$oid][$property])) {
94
                    return $this > $this->lazyValueCache[$oid][$property];
95
                }
96
            } else {
97 3
                $this->lazyValueCache[$oid] = array();
98
            }
99
100 3
            $this->properties[$property]->setAccessible(true);
101
102 3
            return $this->lazyValueCache[$oid][$property] = $this->properties[$property]->getValue($user);
103
        }
104
    }
105
106
    /**
107
     * Gets the name of a specific property by its metadata constant.
108
     *
109
     * @param int  $property
110
     * @param bool $strict
111
     *
112
     * @return null|string
113
     */
114 7
    public function getPropertyName($property = self::LOGIN_PROPERTY, $strict = false)
115
    {
116 7
        if ($this->checkProperty($property, $strict)) {
117 7
            if (isset($this->lazyPropertyNameCache[$property])) {
118 4
                return $this->lazyPropertyNameCache[$property];
119
            }
120
121 7
            return $this->lazyPropertyNameCache[$property] = $this->properties[$property]->getName();
122
        }
123
    }
124
125
    /**
126
     * Modifies a property and clears the cache.
127
     *
128
     * @param object $user
129
     * @param mixed  $newValue
130
     * @param int    $property
131
     */
132 4
    public function modifyProperty($user, $newValue, $property = self::LOGIN_PROPERTY)
133
    {
134 4
        $this->checkProperty($property, true);
135
136 3
        $propertyObject = $this->properties[$property];
137 3
        $propertyObject->setAccessible(true);
138 3
        $propertyObject->setValue($user, $newValue);
139
140 3
        unset($this->lazyValueCache[spl_object_hash($user)]);
141 3
    }
142
143
    /**
144
     * Validates a property.
145
     *
146
     * @param int  $property
147
     * @param bool $strict
148
     *
149
     * @return bool
150
     */
151 8
    private function checkProperty($property = self::LOGIN_PROPERTY, $strict = false)
152
    {
153 8
        if (!isset($this->properties[$property])) {
154 1
            if ($strict) {
155 1
                throw new \LogicException(sprintf(
156 1
                    'Cannot get property "%s"!',
157
                    $property
158 1
                ));
159
            }
160
161
            return false;
162
        }
163
164 7
        return true;
165
    }
166
}
167