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 |
||
5 | class Xhgui_Profiles |
||
|
|||
6 | { |
||
7 | protected $_collection; |
||
8 | |||
9 | protected $_mapper; |
||
10 | |||
11 | public function __construct(MongoDb $db) |
||
16 | |||
17 | /** |
||
18 | * Get the latest profile data. |
||
19 | * |
||
20 | * @return Xhgui_Profile |
||
21 | */ |
||
22 | public function latest() |
||
23 | { |
||
24 | $cursor = $this->_collection->find() |
||
25 | ->sort(array('meta.request_date' => -1)) |
||
26 | ->limit(1); |
||
27 | $result = $cursor->getNext(); |
||
28 | return $this->_wrap($result); |
||
29 | } |
||
30 | |||
31 | public function query($conditions, $fields = null) |
||
35 | |||
36 | /** |
||
37 | * Get a single profile run by id. |
||
38 | * |
||
39 | * @param string $id The id of the profile to get. |
||
40 | * @return Xhgui_Profile |
||
41 | */ |
||
42 | public function get($id) |
||
48 | |||
49 | /** |
||
50 | * Get the list of profiles for a simplified url. |
||
51 | * |
||
52 | * @param string $url The url to load profiles for. |
||
53 | * @param array $options Pagination options to use. |
||
54 | * @param array $conditions The search options. |
||
55 | * @return MongoCursor |
||
56 | */ |
||
57 | public function getForUrl($url, $options, $conditions = array()) |
||
68 | |||
69 | public function paginate($options) |
||
113 | |||
114 | /** |
||
115 | * Get the Percentile metrics for a URL |
||
116 | * |
||
117 | * This will group data by date and returns only the |
||
118 | * percentile + date, making the data ideal for time series graphs |
||
119 | * |
||
120 | * @param integer $percentile The percentile you want. e.g. 90. |
||
121 | * @param string $url |
||
122 | * @param array $search Search options containing date_start and or date_end |
||
123 | * @return array Array of metrics grouped by date |
||
124 | */ |
||
125 | public function getPercentileForUrl($percentile, $url, $search = array()) |
||
197 | |||
198 | /** |
||
199 | * Get the Average metrics for a URL |
||
200 | * |
||
201 | * This will group data by date and returns only the |
||
202 | * avg + date, making the data ideal for time series graphs |
||
203 | * |
||
204 | * @param string $url |
||
205 | * @param array $search Search options containing date_start and or date_end |
||
206 | * @return array Array of metrics grouped by date |
||
207 | */ |
||
208 | public function getAvgsForUrl($url, $search = array()) |
||
245 | |||
246 | /** |
||
247 | * Get a paginated set of results. |
||
248 | * |
||
249 | * @param array $options The find options to use. |
||
250 | * @return array An array of result data. |
||
251 | */ |
||
252 | public function getAll($options = array()) |
||
256 | |||
257 | /** |
||
258 | * Insert a profile run. |
||
259 | * |
||
260 | * Does unchecked inserts. |
||
261 | * |
||
262 | * @param array $profile The profile data to save. |
||
263 | */ |
||
264 | public function insert($profile) |
||
268 | |||
269 | /** |
||
270 | * Delete a profile run. |
||
271 | * |
||
272 | * @param $id The profile id to delete. |
||
273 | * @return array|bool |
||
274 | */ |
||
275 | public function delete($id) |
||
279 | |||
280 | /** |
||
281 | * Used to truncate a collection. |
||
282 | * |
||
283 | * Primarly used in test cases to reset the test db. |
||
284 | * |
||
285 | * @return boolean |
||
286 | */ |
||
287 | public function truncate() |
||
291 | |||
292 | /** |
||
293 | * Converts arrays + MongoCursors into Xhgui_Profile instances. |
||
294 | * |
||
295 | * @param array|MongoCursor $data The data to transform. |
||
296 | * @return Xhgui_Profile|array The transformed/wrapped results. |
||
297 | */ |
||
298 | protected function _wrap($data) |
||
313 | } |
||
314 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.