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 ( 0db119...017a4b )
by Maximilian
14:33 queued 12:08
created

ClassMetadata::getPropertyName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
rs 9.4285
cc 3
eloc 5
nc 3
nop 2
crap 3.3332
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 1
    public function getPropertyValue($user, $property = self::LOGIN_PROPERTY, $strict = false)
89
    {
90 1
        if ($this->checkProperty($property, $strict)) {
91 1
            $oid = spl_object_hash($user);
92 1
            if (isset($this->lazyValueCache[$oid])) {
93
                if (isset($this->lazyValueCache[$oid][$property])) {
94
                    return $this > $this->lazyValueCache[$oid][$property];
95
                }
96
            } else {
97 1
                $this->lazyValueCache[$oid] = array();
98
            }
99
100 1
            $this->properties[$property]->setAccessible(true);
101
102 1
            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 3
    public function getPropertyName($property = self::LOGIN_PROPERTY, $strict = false)
115
    {
116 3
        if ($this->checkProperty($property, $strict)) {
117 3
            if (isset($this->lazyPropertyNameCache[$property])) {
118
                return $this->lazyPropertyNameCache[$property];
119
            }
120
121 3
            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 2
    public function modifyProperty($user, $newValue, $property = self::LOGIN_PROPERTY)
133
    {
134 2
        $this->checkProperty($property, true);
135
136 1
        $propertyObject = $this->properties[$property];
137 1
        $propertyObject->setAccessible(true);
138 1
        $propertyObject->setValue($user, $newValue);
139
140 1
        unset($this->lazyValueCache[spl_object_hash($user)]);
141 1
    }
142
143
    /**
144
     * Validates a property.
145
     *
146
     * @param int  $property
147
     * @param bool $strict
148
     *
149
     * @return bool
150
     */
151 4
    private function checkProperty($property = self::LOGIN_PROPERTY, $strict = false)
152
    {
153 4
        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 3
        return true;
165
    }
166
}
167