Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
34 | class ReflectionProperty extends Object 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 | View Code Duplication | public function __construct($className, $propertyName, array $annotationsToIgnore = array(), array $annotationAliases = array()) |
102 | |||
103 | /** |
||
104 | * This method returns the class name as |
||
105 | * a string. |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | 1 | public static function __getClass() |
|
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() |
|
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() |
|
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() |
|
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() |
|
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) |
|
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) |
||
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 | View Code Duplication | public function getAnnotation($annotationName) |
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() |
|
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() |
|
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()) |
|
309 | } |
||
310 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.