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 |
||
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 | * @param array $metadata |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | public function generateDocumentClass(array $metadata) |
||
73 | |||
74 | /** |
||
75 | * Generates document body |
||
76 | * |
||
77 | * @param array $metadata |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | private function generateDocumentBody(array $metadata) |
||
95 | |||
96 | /** |
||
97 | * Generates document properties |
||
98 | * |
||
99 | * @param array $metadata |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | View Code Duplication | private function generateDocumentProperties(array $metadata) |
|
114 | |||
115 | /** |
||
116 | * Generates document methods |
||
117 | * |
||
118 | * @param array $metadata |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | View Code Duplication | private function generateDocumentMethods(array $metadata) |
|
133 | |||
134 | /** |
||
135 | * Generates document method |
||
136 | * |
||
137 | * @param array $metadata |
||
138 | * @param string $template |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | private function generateDocumentMethod(array $metadata, $template) |
||
152 | |||
153 | /** |
||
154 | * Returns property doc block |
||
155 | * |
||
156 | * @param array $metadata |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | private function generatePropertyDocBlock(array $metadata) |
||
199 | |||
200 | /** |
||
201 | * Generates document doc block |
||
202 | * |
||
203 | * @param array $metadata |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | private function generateDocumentDocBlock(array $metadata) |
||
224 | |||
225 | /** |
||
226 | * @param string $code |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | private function prefixWithSpaces($code) |
||
242 | |||
243 | /** |
||
244 | * Returns class name |
||
245 | * |
||
246 | * @param array $metadata |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | private function getClassName(array $metadata) |
||
255 | } |
||
256 |
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.