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 Index 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 Index, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Index |
||
10 | { |
||
11 | private $fqcns = []; |
||
12 | private $classes = []; |
||
13 | private $interfaces = []; |
||
14 | private $classMap = []; |
||
15 | private $flippedClassMap = []; |
||
16 | private $extends = []; |
||
17 | private $implements = []; |
||
18 | private $parsedFiles = []; |
||
19 | private $functions = []; |
||
20 | /** @var Index $coreIndex */ |
||
21 | private static $coreIndex; |
||
22 | |||
23 | public function getFQCNs() |
||
27 | |||
28 | /** |
||
29 | * @return FQCN |
||
30 | */ |
||
31 | public function findFQCNByFile($file) |
||
45 | |||
46 | /** |
||
47 | * @return ClassData |
||
48 | */ |
||
49 | View Code Duplication | public function findClassByFQCN(FQCN $fqcn) { |
|
58 | |||
59 | /** |
||
60 | * @return InterfaceData |
||
61 | */ |
||
62 | View Code Duplication | public function findInterfaceByFQCN(FQCN $fqcn) { |
|
71 | |||
72 | /** |
||
73 | * @return FunctionData |
||
74 | */ |
||
75 | public function findFunctionByName($functionName) |
||
84 | |||
85 | /** |
||
86 | * @return ClassData[] |
||
87 | */ |
||
88 | View Code Duplication | public function findClassChildren(FQCN $class) { |
|
96 | |||
97 | /** |
||
98 | * @return ClassData[] |
||
99 | */ |
||
100 | View Code Duplication | public function findInterfaceChildrenClasses(FQCN $interface) { |
|
108 | |||
109 | /** |
||
110 | * @return ClassData[] |
||
111 | */ |
||
112 | public function getClasses() |
||
120 | |||
121 | /** |
||
122 | * @return InterfaceData[] |
||
123 | */ |
||
124 | public function getInterfaces() |
||
132 | |||
133 | /** |
||
134 | * @return FunctionData[] |
||
135 | */ |
||
136 | public function getFunctions() |
||
144 | |||
145 | public function addClass(ClassData $class) { |
||
159 | |||
160 | public function addInterface(InterfaceData $interface) { |
||
171 | |||
172 | public function addFunction(FunctionData $function) |
||
176 | |||
177 | public function addFQCN(FQCN $fqcn) { |
||
180 | |||
181 | public function getClassMap() { |
||
193 | |||
194 | public function setClassMap(array $classMap) { |
||
206 | |||
207 | protected function addImplement($class, FQCN $fqcn) { |
||
215 | |||
216 | public function isParsed($file) { |
||
225 | |||
226 | private function hasCoreIndex() |
||
230 | } |
||
231 |
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.