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 |
||
16 | class DocDescriptionHelper |
||
17 | { |
||
18 | /** |
||
19 | * The php code sniffer file. |
||
20 | * |
||
21 | * @var File |
||
22 | */ |
||
23 | private $file; |
||
24 | |||
25 | /** |
||
26 | * The doc comment helper. |
||
27 | * |
||
28 | * @var DocHelper |
||
29 | */ |
||
30 | private $docHelper; |
||
31 | |||
32 | /** |
||
33 | * The token stack of the cs file. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | private $tokens; |
||
38 | |||
39 | /** |
||
40 | * The doc comment summary helper. |
||
41 | * |
||
42 | * @var DocSummaryHelper |
||
43 | */ |
||
44 | private $summaryHelper; |
||
45 | |||
46 | /** |
||
47 | * Indicator if a description is required. |
||
48 | * |
||
49 | * @var bool |
||
50 | */ |
||
51 | private $descriptionRequired; |
||
52 | |||
53 | /** |
||
54 | * DocSummaryHelper constructor. |
||
55 | * |
||
56 | * @param File $file The php cs file |
||
57 | * @param DocHelper $docHelper The doc comment helper |
||
58 | * @param DocSummaryHelper $summaryHelper The doc comment summary helper |
||
59 | */ |
||
60 | 121 | public function __construct(File $file, DocHelper $docHelper, DocSummaryHelper $summaryHelper) |
|
67 | |||
68 | /** |
||
69 | * Checks for comment description. |
||
70 | * |
||
71 | * @param bool $descriptionRequired Indicator if the description is required. |
||
72 | * |
||
73 | * @return void |
||
74 | */ |
||
75 | 113 | public function checkCommentDescription(bool $descriptionRequired) |
|
145 | 16 | ||
146 | /** |
||
147 | * Checks if the description starts with a capital letter. |
||
148 | * |
||
149 | * @param int $descriptionStartPtr Pointer to the start of the description. |
||
150 | * |
||
151 | * @return void |
||
152 | */ |
||
153 | private function checkCommentDescriptionUcFirst(int $descriptionStartPtr) |
||
173 | 8 | ||
174 | /** |
||
175 | * Returns pointer to the end of the description. |
||
176 | * |
||
177 | * @return int Pointer to the end of the description or false |
||
178 | */ |
||
179 | View Code Duplication | private function getCommentDescriptionEndPointer(): int |
|
204 | 4 | ||
205 | 86 | /** |
|
206 | * Returns pointer to the start of the long description or false if not found. |
||
207 | * |
||
208 | * @return int Pointer to the start of the description or -1 |
||
209 | 86 | */ |
|
210 | View Code Duplication | private function getCommentDescriptionStartPointer(): int |
|
235 | 77 | ||
236 | 77 | /** |
|
237 | 77 | * Adds error when description is not found. |
|
238 | 77 | * |
|
239 | * @param int $summaryPtr Pointer to summary token. |
||
240 | * |
||
241 | * @return void |
||
242 | */ |
||
243 | private function addDescriptionNotFoundError(int $summaryPtr) |
||
253 | |||
254 | /** |
||
255 | 109 | * Fixes no line after description. |
|
256 | 12 | * |
|
257 | 12 | * @return void |
|
258 | 12 | */ |
|
259 | 12 | private function fixNoLineAfterDescription() |
|
273 | |||
274 | /** |
||
275 | * Fixes much lines after description. |
||
276 | * |
||
277 | * @param int $startLine Line to start removing |
||
278 | * @param int $endLine Line to end removing |
||
279 | * |
||
280 | 24 | * @return void |
|
281 | */ |
||
282 | 24 | private function fixMuchLinesAfterDescription(int $startLine, int $endLine) |
|
290 | |||
291 | /** |
||
292 | * Fixes the description uc first. |
||
293 | * |
||
294 | * @param int $descriptionStartPtr Pointer to the description start |
||
295 | * |
||
296 | 8 | * @return void |
|
297 | */ |
||
298 | 8 | private function fixDescriptionUcFirst(int $descriptionStartPtr) |
|
311 | } |
||
312 |
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.