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 | public function getParameterClassName(ReflectionParameter $param, $class_name, $index) |
||
| 151 | { |
||
| 152 | View Code Duplication | if (isset($this->parameter_classes[ $class_name ][ $index ]['param_class_name'])) { |
|
| 153 | return $this->parameter_classes[ $class_name ][ $index ]['param_class_name']; |
||
| 154 | } |
||
| 155 | if (! isset($this->parameter_classes[ $class_name ])) { |
||
| 156 | $this->parameter_classes[ $class_name ] = array(); |
||
| 157 | } |
||
| 158 | View Code Duplication | if (! isset($this->parameter_classes[ $class_name ][ $index ])) { |
|
| 159 | $this->parameter_classes[ $class_name ][ $index ] = array(); |
||
| 160 | } |
||
| 161 | // ReflectionParameter::getClass() is deprecated in PHP 8+ |
||
| 162 | if (PHP_VERSION_ID >= 80000) { |
||
| 163 | $this->parameter_classes[ $class_name ][ $index ]['param_class_name'] = |
||
| 164 | $param->getType() instanceof ReflectionNamedType |
||
| 165 | ? $param->getType()->getName() |
||
| 166 | : null; |
||
| 167 | } else { |
||
| 168 | $this->parameter_classes[ $class_name ][ $index ]['param_class_name'] = $param->getClass() |
||
| 169 | ? $param->getClass()->getName() |
||
| 170 | : null; |
||
| 171 | } |
||
| 172 | return $this->parameter_classes[ $class_name ][ $index ]['param_class_name']; |
||
| 173 | } |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * @param ReflectionParameter $param |
||
| 178 | * @param string $class_name |
||
| 179 | * @param string $index |
||
| 180 | * @return string|null |
||
| 181 | */ |
||
| 182 | public function getParameterDefaultValue(ReflectionParameter $param, $class_name, $index) |
||
| 183 | { |
||
| 184 | View Code Duplication | if (isset($this->parameter_classes[ $class_name ][ $index ]['param_class_default'])) { |
|
| 185 | return $this->parameter_classes[ $class_name ][ $index ]['param_class_default']; |
||
| 186 | } |
||
| 187 | if (! isset($this->parameter_classes[ $class_name ])) { |
||
| 188 | $this->parameter_classes[ $class_name ] = array(); |
||
| 189 | } |
||
| 190 | View Code Duplication | if (! isset($this->parameter_classes[ $class_name ][ $index ])) { |
|
| 191 | $this->parameter_classes[ $class_name ][ $index ] = array(); |
||
| 192 | } |
||
| 193 | $this->parameter_classes[ $class_name ][ $index ]['param_class_default'] = $param->isDefaultValueAvailable() |
||
| 194 | ? $param->getDefaultValue() |
||
| 195 | : null; |
||
| 196 | return $this->parameter_classes[ $class_name ][ $index ]['param_class_default']; |
||
| 197 | } |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * @param string $class_name |
||
| 202 | * @return ReflectionProperty[] |
||
| 203 | * @throws InvalidDataTypeException |
||
| 204 | * @throws ReflectionException |
||
| 205 | */ |
||
| 206 | View Code Duplication | public function getProperties($class_name) |
|
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * @param ReflectionClass $reflection_class |
||
| 218 | * @return ReflectionProperty[] |
||
| 219 | * @throws InvalidDataTypeException |
||
| 220 | * @throws ReflectionException |
||
| 221 | */ |
||
| 222 | public function getPropertiesFromReflection(ReflectionClass $reflection_class) |
||
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $class_name |
||
| 230 | * @return ReflectionMethod[] |
||
| 231 | * @throws InvalidDataTypeException |
||
| 232 | * @throws ReflectionException |
||
| 233 | */ |
||
| 234 | View Code Duplication | public function getMethods($class_name) |
|
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * @param ReflectionClass $reflection_class ) |
||
| 246 | * @return ReflectionMethod[] |
||
| 247 | * @throws InvalidDataTypeException |
||
| 248 | * @throws ReflectionException |
||
| 249 | */ |
||
| 250 | public function getMethodsFromReflection(ReflectionClass $reflection_class) |
||
| 254 | } |
||
| 255 |