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 |
||
14 | class EntryParser |
||
15 | { |
||
16 | /** |
||
17 | * Content creator |
||
18 | * |
||
19 | * @var ContentCreator |
||
20 | */ |
||
21 | private $contentCreator; |
||
22 | |||
23 | /** |
||
24 | * Markdown document parser |
||
25 | * |
||
26 | * @var MarkdownDocumentParser |
||
27 | */ |
||
28 | private $mdDocumentParser; |
||
29 | |||
30 | /** |
||
31 | * Config |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | private $config; |
||
36 | |||
37 | /** |
||
38 | * Content dir |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $contentDir; |
||
43 | |||
44 | /** |
||
45 | * Constructor |
||
46 | * |
||
47 | * @param ContentCreator $contentCreator |
||
48 | * @param MarkdownDocumentParser $mdDocumentParser |
||
49 | * @param array $config |
||
50 | */ |
||
51 | View Code Duplication | public function __construct(ContentCreator $contentCreator, MarkdownDocumentParser $mdDocumentParser, |
|
59 | |||
60 | /** |
||
61 | * Get created content type |
||
62 | * |
||
63 | * @param string $id |
||
64 | * @return ContentTypeInterface |
||
65 | */ |
||
66 | public function __invoke(string $id) : ContentTypeInterface |
||
75 | |||
76 | /** |
||
77 | * Get content creator |
||
78 | * |
||
79 | * @return ContentCreator |
||
80 | */ |
||
81 | public function getContentCreator() : ContentCreator |
||
85 | |||
86 | /** |
||
87 | * Get markdown document parser |
||
88 | * |
||
89 | * @return MarkdownDocumentParser |
||
90 | */ |
||
91 | public function getMarkdownDocumentParser() : MarkdownDocumentParser |
||
95 | |||
96 | /** |
||
97 | * Get config |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | public function getConfig() : array |
||
105 | |||
106 | /** |
||
107 | * Get content dir |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getContentDir() : string |
||
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.