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:
Complex classes like ClassMirror often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ClassMirror, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class ClassMirror |
||
27 | { |
||
28 | private static $reflectableMethods = array( |
||
29 | '__construct', |
||
30 | '__destruct', |
||
31 | '__sleep', |
||
32 | '__wakeup', |
||
33 | '__toString', |
||
34 | '__call', |
||
35 | '__invoke' |
||
36 | ); |
||
37 | |||
38 | /** |
||
39 | * Reflects provided arguments into class node. |
||
40 | * |
||
41 | * @param ReflectionClass $class |
||
42 | * @param ReflectionClass[] $interfaces |
||
43 | * |
||
44 | * @return Node\ClassNode |
||
45 | * |
||
46 | * @throws \Prophecy\Exception\InvalidArgumentException |
||
47 | */ |
||
48 | public function reflect(ReflectionClass $class = null, array $interfaces) |
||
87 | |||
88 | private function reflectClassToNode(ReflectionClass $class, Node\ClassNode $node) |
||
120 | |||
121 | private function reflectInterfaceToNode(ReflectionClass $interface, Node\ClassNode $node) |
||
129 | |||
130 | private function reflectMethodToNode(ReflectionMethod $method, Node\ClassNode $classNode) |
||
131 | { |
||
132 | $node = new Node\MethodNode($method->getName()); |
||
133 | |||
134 | if (true === $method->isProtected()) { |
||
135 | $node->setVisibility('protected'); |
||
136 | } |
||
137 | |||
138 | if (true === $method->isStatic()) { |
||
139 | $node->setStatic(); |
||
140 | } |
||
141 | |||
142 | if (true === $method->returnsReference()) { |
||
143 | $node->setReturnsReference(); |
||
144 | } |
||
145 | |||
146 | if (version_compare(PHP_VERSION, '7.0', '>=') && $method->hasReturnType()) { |
||
147 | $returnType = PHP_VERSION_ID >= 70100 ? $method->getReturnType()->getName() : (string) $method->getReturnType(); |
||
148 | $returnTypeLower = strtolower($returnType); |
||
149 | |||
150 | if ('self' === $returnTypeLower) { |
||
151 | $returnType = $method->getDeclaringClass()->getName(); |
||
152 | } |
||
153 | if ('parent' === $returnTypeLower) { |
||
154 | $returnType = $method->getDeclaringClass()->getParentClass()->getName(); |
||
155 | } |
||
156 | |||
157 | $node->setReturnType($returnType); |
||
158 | |||
159 | if (version_compare(PHP_VERSION, '7.1', '>=') && $method->getReturnType()->allowsNull()) { |
||
160 | $node->setNullableReturnType(true); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | if (is_array($params = $method->getParameters()) && count($params)) { |
||
165 | foreach ($params as $param) { |
||
166 | $this->reflectArgumentToNode($param, $node); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | $classNode->addMethod($node); |
||
171 | } |
||
172 | |||
173 | private function reflectArgumentToNode(ReflectionParameter $parameter, Node\MethodNode $methodNode) |
||
196 | |||
197 | private function hasDefaultValue(ReflectionParameter $parameter) |
||
209 | |||
210 | private function getDefaultValue(ReflectionParameter $parameter) |
||
218 | |||
219 | private function getTypeHint(ReflectionParameter $parameter) |
||
220 | { |
||
221 | if (null !== $className = $this->getParameterClassName($parameter)) { |
||
222 | return $className; |
||
223 | } |
||
224 | |||
225 | if (true === $parameter->isArray()) { |
||
226 | return 'array'; |
||
227 | } |
||
228 | |||
229 | if (version_compare(PHP_VERSION, '5.4', '>=') && true === $parameter->isCallable()) { |
||
230 | return 'callable'; |
||
231 | } |
||
232 | |||
233 | if (version_compare(PHP_VERSION, '7.0', '>=') && true === $parameter->hasType()) { |
||
234 | return PHP_VERSION_ID >= 70100 ? $parameter->getType()->getName() : (string) $parameter->getType(); |
||
235 | } |
||
236 | |||
237 | return null; |
||
238 | } |
||
239 | |||
240 | private function isVariadic(ReflectionParameter $parameter) |
||
244 | |||
245 | private function isNullable(ReflectionParameter $parameter) |
||
249 | |||
250 | private function getParameterClassName(ReflectionParameter $parameter) |
||
260 | } |
||
261 |
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.