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 |
||
18 | class SearchIndex |
||
19 | { |
||
20 | /** |
||
21 | * Database handler. |
||
22 | * |
||
23 | * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler |
||
24 | */ |
||
25 | protected $dbHandler; |
||
26 | |||
27 | /** |
||
28 | * SearchIndex constructor. |
||
29 | * |
||
30 | * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler |
||
31 | */ |
||
32 | public function __construct( |
||
37 | |||
38 | /** |
||
39 | * Fetch already indexed words from database (legacy db table: ezsearch_word). |
||
40 | * |
||
41 | * @param array $words |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | public function getWords(array $words) |
||
59 | |||
60 | /** |
||
61 | * Increase the object count of the given words by one. |
||
62 | * |
||
63 | * @param array $wordId |
||
64 | */ |
||
65 | public function incrementWordObjectCount(array $wordId) |
||
69 | |||
70 | /** |
||
71 | * Decrease the object count of the given words by one. |
||
72 | * |
||
73 | * @param array $wordId |
||
74 | */ |
||
75 | public function decrementWordObjectCount(array $wordId) |
||
79 | |||
80 | /** |
||
81 | * Insert new words (legacy db table: ezsearch_word). |
||
82 | * |
||
83 | * @param array $words |
||
84 | */ |
||
85 | public function addWords(array $words) |
||
104 | |||
105 | /** |
||
106 | * Remove entire search index. |
||
107 | */ |
||
108 | public function purge() |
||
125 | |||
126 | /** |
||
127 | * Link word with specific content object (legacy db table: ezsearch_object_word_link). |
||
128 | * |
||
129 | * @param $wordId |
||
130 | * @param $contentId |
||
131 | * @param $frequency |
||
132 | * @param $placement |
||
133 | * @param $nextWordId |
||
134 | * @param $prevWordId |
||
135 | * @param $contentTypeId |
||
136 | * @param $fieldTypeId |
||
137 | * @param $published |
||
138 | * @param $sectionId |
||
139 | * @param $identifier |
||
140 | * @param $integerValue |
||
141 | */ |
||
142 | public function addObjectWordLink($wordId, $contentId, $frequency, $placement, $nextWordId, $prevWordId, |
||
168 | |||
169 | /** |
||
170 | * Get all words related to the content object (legacy db table: ezsearch_object_word_link). |
||
171 | * |
||
172 | * @param $contentId |
||
173 | * |
||
174 | * @return array |
||
175 | */ |
||
176 | public function getContentObjectWords($contentId) |
||
193 | |||
194 | /** |
||
195 | * Delete words not related to any content object. |
||
196 | */ |
||
197 | View Code Duplication | public function deleteWordsWithoutObjects() |
|
207 | |||
208 | /** |
||
209 | * Delete relation between a word and a content object. |
||
210 | * |
||
211 | * @param $contentId |
||
212 | */ |
||
213 | View Code Duplication | public function deleteObjectWordsLink($contentId) |
|
222 | |||
223 | /** |
||
224 | * Set query selecting word ids for content object (method was extracted to be reusable). |
||
225 | * |
||
226 | * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query |
||
227 | */ |
||
228 | private function setContentObjectWordsSelectQuery(SelectQuery $query) |
||
234 | |||
235 | /** |
||
236 | * Update object count for words (legacy db table: ezsearch_word). |
||
237 | * |
||
238 | * @param array $wordId list of word IDs |
||
239 | * @param array $columns map of columns and values to be updated ([column => value]) |
||
240 | */ |
||
241 | private function updateWordObjectCount(array $wordId, array $columns) |
||
255 | } |
||
256 |