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 |
||
21 | class Mirror |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * @var ReflectionClass[] $classes |
||
26 | */ |
||
27 | private $classes = array(); |
||
28 | |||
29 | /** |
||
30 | * @var ReflectionMethod[] $constructors |
||
31 | */ |
||
32 | private $constructors = array(); |
||
33 | |||
34 | /** |
||
35 | * @var ReflectionParameter[][] $parameters |
||
36 | */ |
||
37 | private $parameters = array(); |
||
38 | |||
39 | /** |
||
40 | * @var ReflectionParameter[][] $parameters |
||
41 | */ |
||
42 | private $parameter_classes = array(); |
||
43 | |||
44 | /** |
||
45 | * @var ReflectionProperty[][] $properties |
||
46 | */ |
||
47 | private $properties = array(); |
||
48 | |||
49 | /** |
||
50 | * @var ReflectionMethod[][] $methods |
||
51 | */ |
||
52 | private $methods = array(); |
||
53 | |||
54 | |||
55 | /** |
||
56 | * @param string $class_name |
||
57 | * @return ReflectionClass |
||
58 | * @throws ReflectionException |
||
59 | * @throws InvalidDataTypeException |
||
60 | */ |
||
61 | public function getReflectionClass($class_name) |
||
71 | |||
72 | |||
73 | /** |
||
74 | * @param string $class_name |
||
75 | * @return ReflectionMethod |
||
76 | * @throws InvalidDataTypeException |
||
77 | * @throws ReflectionException |
||
78 | */ |
||
79 | public function getConstructor($class_name) |
||
90 | |||
91 | |||
92 | /** |
||
93 | * @param ReflectionClass $reflection_class |
||
94 | * @return ReflectionMethod |
||
95 | * @throws InvalidDataTypeException |
||
96 | * @throws ReflectionException |
||
97 | */ |
||
98 | public function getConstructorFromReflection(ReflectionClass $reflection_class) |
||
102 | |||
103 | |||
104 | /** |
||
105 | * @param string $class_name |
||
106 | * @return ReflectionParameter[] |
||
107 | * @throws InvalidDataTypeException |
||
108 | * @throws ReflectionException |
||
109 | */ |
||
110 | public function getParameters($class_name) |
||
118 | |||
119 | |||
120 | /** |
||
121 | * @param ReflectionClass $reflection_class |
||
122 | * @return ReflectionParameter[] |
||
123 | * @throws InvalidDataTypeException |
||
124 | * @throws ReflectionException |
||
125 | */ |
||
126 | public function getParametersFromReflection(ReflectionClass $reflection_class) |
||
130 | |||
131 | |||
132 | /** |
||
133 | * @param ReflectionMethod $constructor |
||
134 | * @return ReflectionParameter[] |
||
135 | * @throws InvalidDataTypeException |
||
136 | * @throws ReflectionException |
||
137 | */ |
||
138 | public function getParametersFromReflectionConstructor(ReflectionMethod $constructor) |
||
142 | |||
143 | |||
144 | /** |
||
145 | * @param ReflectionParameter $param |
||
146 | * @param string $class_name |
||
147 | * @param string $index |
||
148 | * @return string|null |
||
149 | */ |
||
150 | View Code Duplication | public function getParameterClassName(ReflectionParameter $param, $class_name, $index) |
|
166 | |||
167 | |||
168 | /** |
||
169 | * @param ReflectionParameter $param |
||
170 | * @param string $class_name |
||
171 | * @param string $index |
||
172 | * @return string|null |
||
173 | */ |
||
174 | View Code Duplication | public function getParameterDefaultValue(ReflectionParameter $param, $class_name, $index) |
|
190 | |||
191 | |||
192 | /** |
||
193 | * @param string $class_name |
||
194 | * @return ReflectionProperty[] |
||
195 | * @throws InvalidDataTypeException |
||
196 | * @throws ReflectionException |
||
197 | */ |
||
198 | View Code Duplication | public function getProperties($class_name) |
|
206 | |||
207 | |||
208 | /** |
||
209 | * @param ReflectionClass $reflection_class |
||
210 | * @return ReflectionProperty[] |
||
211 | * @throws InvalidDataTypeException |
||
212 | * @throws ReflectionException |
||
213 | */ |
||
214 | public function getPropertiesFromReflection(ReflectionClass $reflection_class) |
||
218 | |||
219 | |||
220 | /** |
||
221 | * @param string $class_name |
||
222 | * @return ReflectionMethod[] |
||
223 | * @throws InvalidDataTypeException |
||
224 | * @throws ReflectionException |
||
225 | */ |
||
226 | View Code Duplication | public function getMethods($class_name) |
|
234 | |||
235 | |||
236 | /** |
||
237 | * @param ReflectionClass $reflection_class ) |
||
238 | * @return ReflectionMethod[] |
||
239 | * @throws InvalidDataTypeException |
||
240 | * @throws ReflectionException |
||
241 | */ |
||
242 | public function getMethodsFromReflection(ReflectionClass $reflection_class) |
||
246 | } |
||
247 |