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 DocumentationGenerator 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 DocumentationGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class DocumentationGenerator extends AbstractGenerator |
||
16 | { |
||
17 | /** |
||
18 | * @var boolean |
||
19 | * @todo see if it is needed or of phpdoc says |
||
20 | */ |
||
21 | private $addEmptyLine; |
||
22 | |||
23 | /** |
||
24 | * @param string $comment |
||
25 | * @return $this |
||
26 | */ |
||
27 | public function addComment($comment) |
||
33 | |||
34 | /** |
||
35 | * @param string $className |
||
36 | * @return $this |
||
37 | */ |
||
38 | public function setClass($className) |
||
44 | |||
45 | /** |
||
46 | * @param $interfaceName |
||
47 | * @return $this |
||
48 | */ |
||
49 | public function setInterface($interfaceName) |
||
55 | |||
56 | /** |
||
57 | * @param string $package |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function setPackage($package) |
||
66 | |||
67 | /** |
||
68 | * @param string $name |
||
69 | * @param array $typeHints |
||
70 | * @param string $comment |
||
71 | * @return $this |
||
72 | */ |
||
73 | public function addParameter($name, $typeHints = [], $comment = '') |
||
85 | |||
86 | /** |
||
87 | * @param array $typeHints |
||
88 | * @param string $comment |
||
89 | * @return $this |
||
90 | */ |
||
91 | public function setReturn($typeHints, $comment = '') |
||
102 | |||
103 | /** |
||
104 | * @param string $see |
||
105 | * @return $this |
||
106 | */ |
||
107 | public function addSee($see) |
||
113 | |||
114 | /** |
||
115 | * @param string $exception |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function addThrows($exception) |
||
124 | |||
125 | /** |
||
126 | * @param string $toDo |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function addTodoS($toDo) |
||
135 | |||
136 | /** |
||
137 | * @param string $name |
||
138 | * @param string $email |
||
139 | * @return $this |
||
140 | */ |
||
141 | public function setAuthor($name, $email = '') |
||
152 | |||
153 | /** |
||
154 | * @param string $name |
||
155 | * @param array $typeHints |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function setVariable($name, $typeHints = []) |
||
169 | |||
170 | /** |
||
171 | * @param string $number |
||
172 | * @param string $description |
||
173 | * @return $this |
||
174 | * @see http://www.phpdoc.org/docs/latest/for-users/phpdoc/tags/version.html |
||
175 | */ |
||
176 | public function setVersion($number, $description = '') |
||
186 | |||
187 | /** |
||
188 | * @throws InvalidArgumentException|RuntimeException |
||
189 | * @return string |
||
190 | * @todo implement exception throwing if mandatory parameter is missing |
||
191 | */ |
||
192 | public function generate() |
||
216 | |||
217 | View Code Duplication | private function generateAuthor() |
|
235 | |||
236 | private function generateClass() |
||
251 | |||
252 | private function generateInterface() |
||
267 | |||
268 | private function generatePackage() |
||
282 | |||
283 | View Code Duplication | private function generateComments() |
|
299 | |||
300 | private function generateParameters() |
||
323 | |||
324 | View Code Duplication | private function generateReturn() |
|
345 | |||
346 | View Code Duplication | private function generateSees() |
|
362 | |||
363 | View Code Duplication | private function generateThrows() |
|
377 | |||
378 | View Code Duplication | private function generateToDoS() |
|
394 | |||
395 | View Code Duplication | private function generateVariable() |
|
413 | |||
414 | View Code Duplication | private function generateVersion() |
|
431 | } |
||
432 |
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.