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 ( 6daa1e...29b462 )
by Maximilian
03:41
created

ClassMetadata::modifyProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 3
crap 1
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
class ClassMetadata
12
{
13
    const LOGIN_PROPERTY = 1;
14
    const PASSWORD_PROPERTY = 2;
15
    const API_KEY_PROPERTY = 3;
16
    const LAST_ACTION_PROPERTY = 4;
17
18
    /**
19
     * @var int[]
20
     */
21
    private static $necessaryProperties = array(
22
        self::LOGIN_PROPERTY    => 'login',
23
        self::PASSWORD_PROPERTY => 'password',
24
        self::API_KEY_PROPERTY  => 'apiKey',
25
    );
26
27
    /**
28
     * @var ReflectionClass
29
     */
30
    private $reflectionInstance;
31
32
    /**
33
     * @var string
34
     */
35
    private $className;
36
37
    /**
38
     * @var ReflectionProperty[]
39
     */
40
    private $properties = array();
41
42
    /**
43
     * @var string[]
44
     */
45
    private $lazyPropertyNameCache = array();
46
47
    /**
48
     * @var string[]
49
     */
50
    private $lazyValueCache = array();
51
52
    /**
53
     * Constructor.
54
     *
55
     * @param ReflectionClass      $reflection
56
     * @param string               $className
57
     * @param ReflectionProperty[] $properties
58
     *
59
     * @throws \InvalidArgumentException If one necessary property is missing
60
     */
61 9
    public function __construct(ReflectionClass $reflection, $className, array $properties)
62
    {
63 9
        $this->reflectionInstance = $reflection;
64 9
        $this->className = (string) $className;
65 9
        $this->properties = $properties;
66
67 9
        foreach (self::$necessaryProperties as $propertyIndex => $alias) {
68 9
            if (!isset($this->properties[$propertyIndex])) {
69 1
                throw new \InvalidArgumentException(sprintf(
70 1
                    'Missing required property "%s"!',
71
                    $alias
72 1
                ));
73
            }
74 9
        }
75 8
    }
76
77
    /**
78
     * Gets the value of the given property.
79
     *
80
     * @param object $user
81
     * @param int    $property
82
     * @param bool   $strict
83
     *
84
     * @return mixed
85
     */
86 3
    public function getPropertyValue($user, $property = self::LOGIN_PROPERTY, $strict = false)
87
    {
88 3
        if ($this->checkProperty($property, $strict)) {
89 3
            $oid = spl_object_hash($user);
90 3
            if (isset($this->lazyValueCache[$oid])) {
91
                if (isset($this->lazyValueCache[$oid][$property])) {
92
                    return $this > $this->lazyValueCache[$oid][$property];
93
                }
94
            } else {
95 3
                $this->lazyValueCache[$oid] = array();
96
            }
97
98 3
            $this->properties[$property]->setAccessible(true);
99
100 3
            return $this->lazyValueCache[$oid][$property] = $this->properties[$property]->getValue($user);
101
        }
102
103
        return;
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 6
    public function getPropertyName($property = self::LOGIN_PROPERTY, $strict = false)
115
    {
116 6
        if ($this->checkProperty($property, $strict)) {
117 6
            if (isset($this->lazyPropertyNameCache[$property])) {
118 3
                return $this->lazyPropertyNameCache[$property];
119
            }
120
121 6
            return $this->lazyPropertyNameCache[$property] = $this->properties[$property]->getName();
122
        }
123
124
        return;
125
    }
126
127
    /**
128
     * Modifies a property and clears the cache.
129
     *
130
     * @param object $user
131
     * @param mixed  $newValue
132
     * @param int    $property
133
     */
134 4
    public function modifyProperty($user, $newValue, $property = self::LOGIN_PROPERTY)
135
    {
136 4
        $this->checkProperty($property, true);
137
138 3
        $propertyObject = $this->properties[$property];
139 3
        $propertyObject->setAccessible(true);
140 3
        $propertyObject->setValue($user, $newValue);
141
142 3
        unset($this->lazyValueCache[spl_object_hash($user)]);
143 3
    }
144
145
    /**
146
     * Validates a property.
147
     *
148
     * @param int  $property
149
     * @param bool $strict
150
     *
151
     * @return bool
152
     */
153 7
    private function checkProperty($property = self::LOGIN_PROPERTY, $strict = false)
154
    {
155 7
        if (!isset($this->properties[$property])) {
156 1
            if ($strict) {
157 1
                throw new \LogicException(sprintf(
158 1
                    'Cannot get property "%s"!',
159
                    $property
160 1
                ));
161
            }
162
163
            return false;
164
        }
165
166 6
        return true;
167
    }
168
}
169