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 ContentParser |
||
17 | { |
||
18 | /** |
||
19 | * Markdown file iterator |
||
20 | * |
||
21 | * @var MarkdownFileIterator |
||
22 | */ |
||
23 | private $markdownFileIterator; |
||
24 | |||
25 | /** |
||
26 | * Content creator |
||
27 | * |
||
28 | * @var ContentCreator |
||
29 | */ |
||
30 | private $contentCreator; |
||
31 | |||
32 | /** |
||
33 | * Markdown document parser |
||
34 | * |
||
35 | * @var MarkdownDocumentParser |
||
36 | */ |
||
37 | private $mdDocumentParser; |
||
38 | |||
39 | /** |
||
40 | * Markdown options |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | private $markdownOptions; |
||
45 | |||
46 | /** |
||
47 | * Content path |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | private $contentDir; |
||
52 | |||
53 | /** |
||
54 | * Constructor |
||
55 | * |
||
56 | * @param MarkdownFileIterator $markdownFileIterator |
||
57 | * @param ContentCreator $contentCreator |
||
58 | * @param MarkdownDocumentParser $mdDocumentParser |
||
59 | */ |
||
60 | View Code Duplication | public function __construct(MarkdownFileIterator $markdownFileIterator, ContentCreator $contentCreator, |
|
69 | |||
70 | public function __invoke() |
||
110 | |||
111 | /** |
||
112 | * Get markdown file iterator |
||
113 | * |
||
114 | * @return MarkdownFileIterator |
||
115 | */ |
||
116 | public function getMarkdownFileIterator() : MarkdownFileIterator |
||
120 | |||
121 | /** |
||
122 | * Get content creator |
||
123 | * |
||
124 | * @return ContentCreator |
||
125 | */ |
||
126 | public function getContentCreator() : ContentCreator |
||
130 | |||
131 | /** |
||
132 | * Get markdown document parser |
||
133 | * |
||
134 | * @return MarkdownDocumentParser |
||
135 | */ |
||
136 | public function getMarkdownDocumentParser() : MarkdownDocumentParser |
||
140 | |||
141 | /** |
||
142 | * Get markdown option |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | public function getMarkdownOptions() : array |
||
150 | |||
151 | /** |
||
152 | * Sort by date |
||
153 | * |
||
154 | * @param array $config |
||
155 | */ |
||
156 | private function sortByDate(array &$config) |
||
160 | |||
161 | /** |
||
162 | * Get relative path |
||
163 | * |
||
164 | * @param \SplFileInfo $file |
||
165 | * @return string |
||
166 | */ |
||
167 | private function getRelativePath(\SplFileInfo $file) : string |
||
172 | |||
173 | /** |
||
174 | * Sort by entry count descending |
||
175 | * |
||
176 | * @param array $a |
||
177 | * @param array $b |
||
178 | * @return number |
||
179 | */ |
||
180 | private function sortByEntryCountDescending(array $configEntryA, array $configEntryB) |
||
187 | } |
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.