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 |
||
| 25 | class Mirror |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var ReflectionClass[] $classes |
||
| 30 | */ |
||
| 31 | private $classes = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var ReflectionMethod[] $constructors |
||
| 35 | */ |
||
| 36 | private $constructors = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var ReflectionParameter[][] $parameters |
||
| 40 | */ |
||
| 41 | private $parameters = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var ReflectionParameter[][] $parameters |
||
| 45 | */ |
||
| 46 | private $parameter_classes = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var ReflectionProperty[][] $properties |
||
| 50 | */ |
||
| 51 | private $properties = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var ReflectionMethod[][] $methods |
||
| 55 | */ |
||
| 56 | private $methods = array(); |
||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string $class_name |
||
| 61 | * @return ReflectionClass |
||
| 62 | * @throws ReflectionException |
||
| 63 | * @throws InvalidDataTypeException |
||
| 64 | */ |
||
| 65 | public function getReflectionClass($class_name) |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $class_name |
||
| 79 | * @return ReflectionMethod |
||
| 80 | * @throws InvalidDataTypeException |
||
| 81 | * @throws ReflectionException |
||
| 82 | */ |
||
| 83 | public function getConstructor($class_name) |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * @param ReflectionClass $reflection_class |
||
| 98 | * @return ReflectionMethod |
||
| 99 | * @throws InvalidDataTypeException |
||
| 100 | * @throws ReflectionException |
||
| 101 | */ |
||
| 102 | public function getConstructorFromReflection(ReflectionClass $reflection_class) |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $class_name |
||
| 110 | * @return ReflectionParameter[] |
||
| 111 | * @throws InvalidDataTypeException |
||
| 112 | * @throws ReflectionException |
||
| 113 | */ |
||
| 114 | public function getParameters($class_name) |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @param ReflectionClass $reflection_class |
||
| 126 | * @return ReflectionParameter[] |
||
| 127 | * @throws InvalidDataTypeException |
||
| 128 | * @throws ReflectionException |
||
| 129 | */ |
||
| 130 | public function getParametersFromReflection(ReflectionClass $reflection_class) |
||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * @param ReflectionMethod $constructor |
||
| 138 | * @return ReflectionParameter[] |
||
| 139 | * @throws InvalidDataTypeException |
||
| 140 | * @throws ReflectionException |
||
| 141 | */ |
||
| 142 | public function getParametersFromReflectionConstructor(ReflectionMethod $constructor) |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * @param ReflectionParameter $param |
||
| 150 | * @param string $class_name |
||
| 151 | * @param string $index |
||
| 152 | * @return string|null |
||
| 153 | */ |
||
| 154 | View Code Duplication | public function getParameterClassName(ReflectionParameter $param, $class_name, $index) |
|
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * @param ReflectionParameter $param |
||
| 174 | * @param string $class_name |
||
| 175 | * @param string $index |
||
| 176 | * @return string|null |
||
| 177 | */ |
||
| 178 | View Code Duplication | public function getParameterDefaultValue(ReflectionParameter $param, $class_name, $index) |
|
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $class_name |
||
| 198 | * @return ReflectionProperty[] |
||
| 199 | * @throws InvalidDataTypeException |
||
| 200 | * @throws ReflectionException |
||
| 201 | */ |
||
| 202 | View Code Duplication | public function getProperties($class_name) |
|
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * @param ReflectionClass $reflection_class |
||
| 214 | * @return ReflectionProperty[] |
||
| 215 | * @throws InvalidDataTypeException |
||
| 216 | * @throws ReflectionException |
||
| 217 | */ |
||
| 218 | public function getPropertiesFromReflection(ReflectionClass $reflection_class) |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $class_name |
||
| 226 | * @return ReflectionMethod[] |
||
| 227 | * @throws InvalidDataTypeException |
||
| 228 | * @throws ReflectionException |
||
| 229 | */ |
||
| 230 | View Code Duplication | public function getMethods($class_name) |
|
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * @param ReflectionClass $reflection_class ) |
||
| 242 | * @return ReflectionMethod[] |
||
| 243 | * @throws InvalidDataTypeException |
||
| 244 | * @throws ReflectionException |
||
| 245 | */ |
||
| 246 | public function getMethodsFromReflection(ReflectionClass $reflection_class) |
||
| 250 | } |
||
| 251 |