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 (#31)
by Maximilian
06:34
created

ClassMetadata   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 14
c 2
b 1
f 0
lcom 1
cbo 0
dl 0
loc 157
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A getPropertyValue() 0 18 4
A getPropertyName() 0 12 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
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
    public function __construct(ReflectionClass $reflection, $className, array $properties)
62
    {
63
        $this->reflectionInstance = $reflection;
64
        $this->className = (string) $className;
65
        $this->properties = $properties;
66
67
        foreach (self::$necessaryProperties as $propertyIndex => $alias) {
68
            if (!isset($this->properties[$propertyIndex])) {
69
                throw new \InvalidArgumentException(sprintf(
70
                    'Missing required property "%s"!',
71
                    $alias
72
                ));
73
            }
74
        }
75
    }
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
    public function getPropertyValue($user, $property = self::LOGIN_PROPERTY, $strict = false)
87
    {
88
        if ($this->checkProperty($property, $strict)) {
89
            $oid = spl_object_hash($user);
90
            if (isset($this->lazyValueCache[$oid])) {
91
                if (isset($this->lazyValueCache[$oid][$property])) {
92
                    return $this>$this->lazyValueCache[$oid][$property];
93
                }
94
            } else {
95
                $this->lazyValueCache[$oid] = array();
96
            }
97
98
            $this->properties[$property]->setAccessible(true);
99
            return $this->lazyValueCache[$oid][$property] = $this->properties[$property]->getValue($user);
100
        }
101
102
        return null;
103
    }
104
105
    /**
106
     * Gets the name of a specific property by its metadata constant.
107
     *
108
     * @param int  $property
109
     * @param bool $strict
110
     *
111
     * @return null|string
112
     */
113
    public function getPropertyName($property = self::LOGIN_PROPERTY, $strict = false)
114
    {
115
        if ($this->checkProperty($property, $strict)) {
116
            if (isset($this->lazyPropertyNameCache[$property])) {
117
                return $this->lazyPropertyNameCache[$property];
118
            }
119
120
            return $this->lazyPropertyNameCache[$property] = $this->properties[$property]->getName();
121
        }
122
123
        return null;
124
    }
125
126
    /**
127
     * Modifies a property and clears the cache.
128
     *
129
     * @param object $user
130
     * @param mixed  $newValue
131
     * @param int    $property
132
     */
133
    public function modifyProperty($user, $newValue, $property = self::LOGIN_PROPERTY)
134
    {
135
        $this->checkProperty($property, true);
136
137
        $propertyObject = $this->properties[$property];
138
        $propertyObject->setAccessible(true);
139
        $propertyObject->setValue($user, $newValue);
140
141
        unset($this->lazyValueCache[spl_object_hash($user)]);
142
    }
143
144
    /**
145
     * Validates a property.
146
     *
147
     * @param int  $property
148
     * @param bool $strict
149
     *
150
     * @return bool
151
     */
152
    private function checkProperty($property = self::LOGIN_PROPERTY, $strict = false)
153
    {
154
        if (!isset($this->properties[$property])) {
155
            if ($strict) {
156
                throw new \LogicException(sprintf(
157
                    'Cannot get property "%s"!',
158
                    $property
159
                ));
160
            }
161
162
            return false;
163
        }
164
165
        return true;
166
    }
167
}
168