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 |
||
6 | class Xhgui_Storage_Mongo implements \Xhgui_StorageInterface, \Xhgui_WatchedFunctionsStorageInterface |
||
7 | { |
||
8 | |||
9 | /** |
||
10 | * @var \MongoDB |
||
11 | */ |
||
12 | protected $connection; |
||
13 | |||
14 | /** |
||
15 | * @var int |
||
16 | */ |
||
17 | protected $defaultPerPage = 25; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $collection; |
||
23 | /** |
||
24 | * @var \MongoClient |
||
25 | */ |
||
26 | protected $mongoClient; |
||
27 | |||
28 | /** |
||
29 | * Mongo constructor. |
||
30 | * @param $config |
||
31 | * @throws \MongoConnectionException |
||
32 | * @throws \MongoException |
||
33 | */ |
||
34 | public function __construct($config, $collection = 'results') |
||
55 | |||
56 | /** |
||
57 | * @param $options |
||
58 | * @param bool $projections |
||
59 | * @return Xhgui_Storage_ResultSet |
||
60 | * @throws \MongoCursorException |
||
61 | */ |
||
62 | public function find(\Xhgui_Storage_Filter $filter, $projections = false) |
||
90 | |||
91 | /** |
||
92 | * @param $options |
||
93 | * @return int |
||
94 | * @throws \MongoCursorTimeoutException |
||
95 | * @throws \MongoException |
||
96 | */ |
||
97 | public function count(\Xhgui_Storage_Filter $filter) |
||
104 | |||
105 | /** |
||
106 | * @param $id |
||
107 | * @return array|null |
||
108 | * @throws \MongoException |
||
109 | */ |
||
110 | public function findOne($id) |
||
117 | |||
118 | /** |
||
119 | * @param $id |
||
120 | * @return array|bool |
||
121 | * @throws MongoCursorException |
||
122 | * @throws MongoCursorTimeoutException |
||
123 | * @throws MongoException |
||
124 | */ |
||
125 | View Code Duplication | public function remove($id) |
|
132 | |||
133 | /** |
||
134 | * @param $data |
||
135 | * @return array|bool |
||
136 | * @throws \MongoCursorException |
||
137 | * @throws \MongoCursorTimeoutException |
||
138 | * @throws \MongoException |
||
139 | */ |
||
140 | public function insert($data) |
||
147 | |||
148 | /** |
||
149 | * @param $id |
||
150 | * @param $data |
||
151 | * @return array|bool |
||
152 | * @throws \MongoCursorException |
||
153 | * @throws \MongoException |
||
154 | * @throws \MongoWriteConcernException |
||
155 | */ |
||
156 | View Code Duplication | public function update($id, $data) |
|
164 | |||
165 | /** |
||
166 | * |
||
167 | */ |
||
168 | public function drop() |
||
172 | |||
173 | /** |
||
174 | * @param $match |
||
175 | * @param $col |
||
176 | * @param int $percentile |
||
177 | * @return array |
||
178 | * @throws \MongoException |
||
179 | */ |
||
180 | public function aggregate(\Xhgui_Storage_Filter $filter, $col, $percentile = 1) |
||
231 | |||
232 | /** |
||
233 | * @return array |
||
234 | */ |
||
235 | public function getWatchedFunctions() |
||
248 | |||
249 | /** |
||
250 | * @param $name |
||
251 | * @return bool |
||
252 | */ |
||
253 | View Code Duplication | public function addWatchedFunction($name) |
|
275 | |||
276 | /** |
||
277 | * @param $id |
||
278 | * @param $name |
||
279 | * @return bool |
||
280 | */ |
||
281 | View Code Duplication | public function updateWatchedFunction($id, $name) |
|
302 | |||
303 | /** |
||
304 | * @param $id |
||
305 | * @return bool |
||
306 | */ |
||
307 | public function removeWatchedFunction($id) |
||
320 | |||
321 | /** |
||
322 | * @param \Xhgui_Storage_Filter $options |
||
323 | * @return \DateTime |
||
324 | */ |
||
325 | protected function getDateTimeFromString($date) |
||
340 | |||
341 | /** |
||
342 | * @param \Xhgui_Storage_Filter $filter |
||
343 | * @return array |
||
344 | */ |
||
345 | protected function getConditions(\Xhgui_Storage_Filter $filter) |
||
379 | } |
||
380 |
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.