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 |
||
35 | class PMF_Search_Elasticsearch extends PMF_Search_Abstract implements PMF_Search_Interface |
||
36 | { |
||
37 | /** @var Client */ |
||
38 | private $client = null; |
||
39 | |||
40 | /** @var array */ |
||
41 | private $esConfig = []; |
||
42 | |||
43 | /** @var string */ |
||
44 | private $language = ''; |
||
45 | |||
46 | /** @var array */ |
||
47 | private $categoryIds = []; |
||
48 | |||
49 | /** |
||
50 | * Constructor. |
||
51 | * |
||
52 | * @param PMF_Configuration |
||
53 | */ |
||
54 | public function __construct(PMF_Configuration $config) |
||
61 | |||
62 | /** |
||
63 | * Prepares the search and executes it. |
||
64 | * |
||
65 | * @param string $searchTerm Search term |
||
66 | * |
||
67 | * @throws PMF_Search_Exception |
||
68 | * |
||
69 | * @return array |
||
70 | */ |
||
71 | public function search($searchTerm) |
||
126 | |||
127 | /** |
||
128 | * Prepares the autocomplete search and executes it. |
||
129 | * |
||
130 | * @param string $searchTerm Search term for autocompletion |
||
131 | * |
||
132 | * @throws PMF_Search_Exception |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | public function autoComplete($searchTerm) |
||
187 | |||
188 | /** |
||
189 | * Returns the current category ID |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | public function getCategoryIds() |
||
197 | |||
198 | /** |
||
199 | * Sets the current category ID |
||
200 | * |
||
201 | * @param array $categoryIds |
||
202 | */ |
||
203 | public function setCategoryIds(Array $categoryIds) |
||
207 | |||
208 | /** |
||
209 | * Returns the current language, empty string if all languages |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | public function getLanguage() |
||
217 | |||
218 | /** |
||
219 | * Sets the current language |
||
220 | * |
||
221 | * @param string $language |
||
222 | */ |
||
223 | public function setLanguage($language) |
||
227 | } |
||
228 |
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.