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 |
||
12 | class DocBlockGenerator extends Generator |
||
13 | { |
||
14 | /** |
||
15 | * @var TagGenerator |
||
16 | */ |
||
17 | protected $tagGenerator; |
||
18 | |||
19 | /** |
||
20 | * @param TagGenerator $tagGenerator |
||
21 | */ |
||
22 | 5 | public function __construct(TagGenerator $tagGenerator) |
|
26 | |||
27 | /** |
||
28 | * @param DocBlock $docBlock |
||
29 | * |
||
30 | * @return string |
||
31 | */ |
||
32 | 5 | public function generate(DocBlock $docBlock): string |
|
66 | |||
67 | /** |
||
68 | * @param string $text |
||
69 | * @param bool $wrap |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | 4 | protected function textToComment(string $text, bool $wrap = false): string |
|
86 | |||
87 | /** |
||
88 | * @param DocBlock $docBlock |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | 5 | protected function sortTags(DocBlock $docBlock): array |
|
114 | } |
||
115 |
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.