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 DocumentGenerator 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 DocumentGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class DocumentGenerator |
||
18 | { |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $spaces = ' '; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $getMethodTemplate = |
||
28 | '/** |
||
29 | * Returns <fieldName> |
||
30 | * |
||
31 | * @return string |
||
32 | */ |
||
33 | public function get<methodName>() |
||
34 | { |
||
35 | <spaces>return $this-><fieldName>; |
||
36 | }'; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $setMethodTemplate = |
||
42 | '/** |
||
43 | * Sets <fieldName> |
||
44 | * |
||
45 | * @param string $<fieldName> |
||
46 | */ |
||
47 | public function set<methodName>($<fieldName>) |
||
48 | { |
||
49 | <spaces>$this-><fieldName> = $<fieldName>; |
||
50 | }'; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $constructorTemplate = |
||
56 | '/** |
||
57 | * Constructor |
||
58 | */ |
||
59 | public function __construct() |
||
60 | { |
||
61 | <fields> |
||
62 | }'; |
||
63 | |||
64 | /** |
||
65 | * @param array $metadata |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | public function generateDocumentClass(array $metadata) |
||
85 | |||
86 | /** |
||
87 | * Generates document body |
||
88 | * |
||
89 | * @param array $metadata |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | private function generateDocumentBody(array $metadata) |
||
111 | |||
112 | /** |
||
113 | * Generates document properties |
||
114 | * |
||
115 | * @param array $metadata |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | View Code Duplication | private function generateDocumentProperties(array $metadata) |
|
130 | |||
131 | /** |
||
132 | * Generates document methods |
||
133 | * |
||
134 | * @param array $metadata |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | View Code Duplication | private function generateDocumentMethods(array $metadata) |
|
149 | |||
150 | /** |
||
151 | * Generates document constructor |
||
152 | * |
||
153 | * @param array $metadata |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | private function generateDocumentConstructor(array $metadata) |
||
171 | |||
172 | /** |
||
173 | * Generates document method |
||
174 | * |
||
175 | * @param array $metadata |
||
176 | * @param string $template |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | private function generateDocumentMethod(array $metadata, $template) |
||
190 | |||
191 | /** |
||
192 | * Returns property doc block |
||
193 | * |
||
194 | * @param array $metadata |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | private function generatePropertyDocBlock(array $metadata) |
||
237 | |||
238 | /** |
||
239 | * Generates document doc block |
||
240 | * |
||
241 | * @param array $metadata |
||
242 | * |
||
243 | * @return string |
||
244 | */ |
||
245 | private function generateDocumentDocBlock(array $metadata) |
||
262 | |||
263 | /** |
||
264 | * @param string $code |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | private function prefixWithSpaces($code) |
||
280 | |||
281 | /** |
||
282 | * Returns class name |
||
283 | * |
||
284 | * @param array $metadata |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | private function getClassName(array $metadata) |
||
293 | |||
294 | /** |
||
295 | * Generates document use statements |
||
296 | * |
||
297 | * @param array $metadata |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | private function generateDocumentUse(array $metadata) |
||
311 | |||
312 | /** |
||
313 | * @param array $metadata |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | private function hasMultipleEmbedded(array $metadata) |
||
327 | } |
||
328 |
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.