ReflectionProperty   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 274
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 19
eloc 43
dl 0
loc 274
ccs 51
cts 54
cp 0.9444
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 3 1
A getPropertyName() 0 3 1
A __getClass() 0 3 1
A getClassName() 0 3 1
A fromPhpReflectionProperty() 0 9 1
A addAnnotationAlias() 0 3 1
A getAnnotation() 0 12 2
A __construct() 0 14 1
A getAnnotationAliases() 0 3 1
A getAnnotationsToIgnore() 0 3 1
A hasAnnotation() 0 4 1
A toPhpReflectionProperty() 0 3 1
A unserialize() 0 4 2
A getAnnotations() 0 10 2
A fromReflectionClass() 0 14 2
1
<?php
2
3
/**
4
 * AppserverIo\Lang\Reflection\ReflectionProperty
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/lang
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Lang\Reflection;
22
23
use AppserverIo\Lang\Objct;
24
25
/**
26
 * A wrapper instance for a reflection property.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2015 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/appserver-io/lang
32
 * @link      http://www.appserver.io
33
 */
34
class ReflectionProperty extends Objct implements PropertyInterface, \Serializable
35
{
36
37
    /**
38
     * Default filter for loading reflection properties from a reflection class.
39
     *
40
     * @var integer
41
     */
42
    const ALL_MODIFIERS = -1;
43
44
    /**
45
     * The properties class name.
46
     *
47
     * @var string
48
     */
49
    protected $className;
50
51
    /**
52
     * The property name.
53
     *
54
     * @var string
55
     */
56
    protected $propertyName;
57
58
    /**
59
     * The method annotations.
60
     *
61
     * @var array
62
     */
63
    protected $annotations;
64
65
    /**
66
     * Array with annotations names we want to ignore when loaded.
67
     *
68
     * @var array
69
     */
70
    protected $annotationsToIgnore;
71
72
    /**
73
     * Array with annotation aliases used when create annotation instances.
74
     *
75
     * @var array
76
     */
77
    protected $annotationAliases;
78
79
    /**
80
     * Initializes the reflection property with the passed data.
81
     *
82
     * @param string $className           The properties class name
83
     * @param string $propertyName        The property name
84
     * @param array  $annotationsToIgnore An array with annotations names we want to ignore when loaded
85
     * @param array  $annotationAliases   An array with annotation aliases used when create annotation instances
86
     */
87 6
    public function __construct($className, $propertyName, array $annotationsToIgnore = array(), array $annotationAliases = array())
88
    {
89
        // initialize property default values here, as declarative default values may break thread safety,
90
        // when utilizing static and non-static access on class methods within same thread context!
91 6
        $this->className = '';
92 6
        $this->propertyName = '';
93 6
        $this->annotations = null;
94 6
        $this->annotationsToIgnore = array();
95 6
        $this->annotationAliases = array();
96
97 6
        $this->className = $className;
98 6
        $this->propertyName = $propertyName;
99 6
        $this->annotationsToIgnore = $annotationsToIgnore;
100 6
        $this->annotationAliases = $annotationAliases;
101 6
    }
102
103
    /**
104
     * This method returns the class name as
105
     * a string.
106
     *
107
     * @return string
108
     */
109 1
    public static function __getClass()
110
    {
111 1
        return __CLASS__;
112
    }
113
114
    /**
115
     * Returns the properties class name.
116
     *
117
     * @return string The class name
118
     * @see \AppserverIo\Lang\Reflection\PropertyInterface::getClassName()
119
     */
120 2
    public function getClassName()
121
    {
122 2
        return $this->className;
123
    }
124
125
    /**
126
     * Returns the property name.
127
     *
128
     * @return string The property name
129
     * @see \AppserverIo\Lang\Reflection\PropertyInterface::getPropertyName()
130
     */
131 2
    public function getPropertyName()
132
    {
133 2
        return $this->propertyName;
134
    }
135
136
    /**
137
     * Returns an array with annotation names we want to ignore when loaded.
138
     *
139
     * @return array The annotation names we want to ignore
140
     * @see \AppserverIo\Lang\Reflection\PropertyInterface::getAnnotationsToIgnore()
141
     */
142 2
    public function getAnnotationsToIgnore()
143
    {
144 2
        return $this->annotationsToIgnore;
145
    }
146
147
    /**
148
     * Returns an array with annotation aliases used when create annotation instances.
149
     *
150
     * @return array The annotation aliases used when create annotation instances
151
     * @see \AppserverIo\Lang\Reflection\PropertyInterface::getAnnotationAliases()
152
     */
153 3
    public function getAnnotationAliases()
154
    {
155 3
        return $this->annotationAliases;
156
    }
157
158
    /**
159
     * Registers the annotation alias for the passed class name.
160
     *
161
     * @param string $annotationName      The alias
162
     * @param string $annotationClassName The resolving class name
163
     *
164
     * @return void
165
     * @see \AppserverIo\Lang\Reflection\PropertyInterface::addAnnotationAlias()
166
     */
167 1
    public function addAnnotationAlias($annotationName, $annotationClassName)
168
    {
169 1
        $this->annotationAliases[$annotationName] = $annotationClassName;
170 1
    }
171
172
    /**
173
     * Returns the method annotations.
174
     *
175
     * @return array The method annotations
176
     * @see \AppserverIo\Lang\Reflection\PropertyInterface::getAnnotations()
177
     */
178 2
    public function getAnnotations()
179
    {
180
181
        // check if the annotations has been loaded
182 2
        if (isset($this->annotations) === false) {
183 2
            $this->annotations = ReflectionAnnotation::fromReflectionProperty($this);
184
        }
185
186
        // return the annotations
187 2
        return $this->annotations;
188
    }
189
190
    /**
191
     * Queries whether the reflection method has an annotation with the passed name or not.
192
     *
193
     * @param string $annotationName The annotation we want to query
194
     *
195
     * @return boolean TRUE if the reflection method has the annotation, else FALSE
196
     * @see \AppserverIo\Lang\Reflection\PropertyInterface::hasAnnotation()
197
     */
198
    public function hasAnnotation($annotationName)
199
    {
200
        $annotations = $this->getAnnotations();
201
        return isset($annotations[$annotationName]);
202
    }
203
204
    /**
205
     * Returns the annotation instance with the passed name.
206
     *
207
     * @param string $annotationName The name of the requested annotation instance
208
     *
209
     * @return \AppserverIo\Lang\Reflection\AnnotationInterface|null The requested annotation instance
210
     * @throws \AppserverIo\Lang\Reflection\ReflectionException Is thrown if the requested annotation is not available
211
     * @see \AppserverIo\Lang\Reflection\PropertyInterface::hasAnnotation()
212
     */
213 2
    public function getAnnotation($annotationName)
214
    {
215
216
        // first check if the method is available
217 2
        $annotations = $this->getAnnotations();
218 2
        if (isset($annotations[$annotationName])) {
219
            // if yes, return it
220 1
            return $annotations[$annotationName];
221
        }
222
223
        // if not, throw an exception
224 1
        throw new ReflectionException(sprintf('The requested reflection annotation %s is not available', $annotationName));
225
    }
226
227
    /**
228
     * Serializes the timeout method and returns a string representation.
229
     *
230
     * @return string The serialized string representation of the instance
231
     * @see \Serializable::serialize()
232
     */
233 1
    public function serialize()
234
    {
235 1
        return serialize(get_object_vars($this));
236
    }
237
238
    /**
239
     * Restores the instance with the serialized data of the passed string.
240
     *
241
     * @param string $data The serialized property representation
242
     *
243
     * @return void
244
     * @see \Serializable::unserialize()
245
     */
246 1
    public function unserialize($data)
247
    {
248 1
        foreach (unserialize($data) as $propertyName => $propertyValue) {
249 1
            $this->$propertyName = $propertyValue;
250
        }
251 1
    }
252
253
    /**
254
     * Returns a PHP reflection property representation of this instance.
255
     *
256
     * @return \ReflectionProperty The PHP reflection property instance
257
     * @see \AppserverIo\Lang\Reflection\PropertyInterface::toPhpReflectionProperty()
258
     */
259 2
    public function toPhpReflectionProperty()
260
    {
261 2
        return new \ReflectionProperty($this->getClassName(), $this->getPropertyName());
262
    }
263
264
    /**
265
     * Returns an array of reflection property instances from the passed reflection class.
266
     *
267
     * @param \AppserverIo\Lang\Reflection\ReflectionClass $reflectionClass     The reflection class to return the properties for
268
     * @param integer                                      $filter              The filter used for loading the properties
269
     * @param array                                        $annotationsToIgnore An array with annotations names we want to ignore when loaded
270
     * @param array                                        $annotationAliases   An array with annotation aliases used when create annotation instances
271
     *
272
     * @return array An array with ReflectionProperty instances
273
     */
274 1
    public static function fromReflectionClass(ReflectionClass $reflectionClass, $filter = 0, array $annotationsToIgnore = array(), array $annotationAliases = array())
275
    {
276
277
        // initialize the array for the reflection properties
278 1
        $reflectionProperties = array();
279
280
        // load the reflection properties and initialize the array with the reflection properties
281 1
        $phpReflectionClass = $reflectionClass->toPhpReflectionClass();
282 1
        foreach ($phpReflectionClass->getProperties($filter) as $phpReflectionProperty) {
283 1
            $reflectionProperties[$phpReflectionProperty->getName()] = ReflectionProperty::fromPhpReflectionProperty($phpReflectionProperty, $annotationsToIgnore, $annotationAliases);
284
        }
285
286
        // return the array with the initialized reflection properties
287 1
        return $reflectionProperties;
288
    }
289
290
    /**
291
     * Creates a new reflection property instance from the passed PHP reflection property.
292
     *
293
     * @param \ReflectionProperty $reflectionProperty  The reflection property to load the data from
294
     * @param array               $annotationsToIgnore An array with annotations names we want to ignore when loaded
295
     * @param array               $annotationAliases   An array with annotation aliases used when create annotation instances
296
     *
297
     * @return \AppserverIo\Lang\Reflection\ReflectionProperty The instance
298
     */
299 1
    public static function fromPhpReflectionProperty(\ReflectionProperty $reflectionProperty, array $annotationsToIgnore = array(), array $annotationAliases = array())
300
    {
301
302
        // load class and method name from the reflection class
303 1
        $className = $reflectionProperty->getDeclaringClass()->getName();
304 1
        $propertyName = $reflectionProperty->getName();
305
306
        // initialize and return the timeout method instance
307 1
        return new ReflectionProperty($className, $propertyName, $annotationsToIgnore, $annotationAliases);
308
    }
309
}
310