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 namespace Nord\Lumen\Search; |
||
7 | class Search |
||
8 | { |
||
9 | |||
10 | /** |
||
11 | * @var Filter[] |
||
12 | */ |
||
13 | private $filters = []; |
||
14 | |||
15 | /** |
||
16 | * @var Sort[] |
||
17 | */ |
||
18 | private $sorts = []; |
||
19 | |||
20 | /** |
||
21 | * @var SearchAdapter |
||
22 | */ |
||
23 | private $adapter; |
||
24 | |||
25 | /** |
||
26 | * @var StringParser |
||
27 | */ |
||
28 | private $parser; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * Search constructor. |
||
33 | * |
||
34 | * @param mixed $filters |
||
35 | * @param mixed $sorts |
||
36 | * @param SearchAdapter $adapter |
||
37 | * @param StringParser $parser |
||
38 | */ |
||
39 | public function __construct($filters, $sorts, SearchAdapter $adapter, StringParser $parser = null) |
||
46 | |||
47 | |||
48 | /** |
||
49 | * @inheritdoc |
||
50 | */ |
||
51 | public function run() |
||
58 | |||
59 | |||
60 | /** |
||
61 | * @inheritdoc |
||
62 | */ |
||
63 | public function runWithPagination($pageNumber, $pageSize) |
||
70 | |||
71 | |||
72 | /** |
||
73 | * |
||
74 | */ |
||
75 | protected function applyFilters() |
||
122 | |||
123 | |||
124 | /** |
||
125 | * |
||
126 | */ |
||
127 | protected function applySorts() |
||
133 | |||
134 | |||
135 | /** |
||
136 | * @param mixed $filters |
||
137 | * |
||
138 | * @throws InvalidArgument |
||
139 | */ |
||
140 | View Code Duplication | protected function setFilters($filters) |
|
158 | |||
159 | |||
160 | /** |
||
161 | * @param array $config |
||
162 | * |
||
163 | * @return Filter |
||
164 | */ |
||
165 | protected function createFilterFromConfig(array $config) |
||
169 | |||
170 | |||
171 | /** |
||
172 | * @param mixed $sorts |
||
173 | * |
||
174 | * @throws InvalidArgument |
||
175 | */ |
||
176 | View Code Duplication | protected function setSorts($sorts) |
|
194 | |||
195 | |||
196 | /** |
||
197 | * @param array $config |
||
198 | * |
||
199 | * @return Sort |
||
200 | */ |
||
201 | protected function createSortFromConfig(array $config) |
||
205 | |||
206 | |||
207 | /** |
||
208 | * @param SearchAdapter $adapter |
||
209 | */ |
||
210 | private function setAdapter(SearchAdapter $adapter) |
||
214 | |||
215 | |||
216 | /** |
||
217 | * @param StringParser $parser |
||
218 | */ |
||
219 | private function setParser(StringParser $parser) |
||
223 | } |
||
224 |
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.