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_Searcher_Mongo implements Xhgui_Searcher_Interface |
||
|
|
|||
| 7 | { |
||
| 8 | protected $_collection; |
||
| 9 | |||
| 10 | protected $_watches; |
||
| 11 | |||
| 12 | protected $_mapper; |
||
| 13 | |||
| 14 | public function __construct(MongoDb $db) |
||
| 20 | |||
| 21 | /** |
||
| 22 | * {@inheritdoc} |
||
| 23 | */ |
||
| 24 | public function latest() |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | public function query($conditions, $limit, $fields = []) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | public function get($id) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | public function getForUrl($url, $options, $conditions = []) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | public function getPercentileForUrl($percentile, $url, $search = []) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | public function getAvgsForUrl($url, $search = []) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | public function getAll($options = []) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | public function delete($id) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function truncate() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | */ |
||
| 215 | public function saveWatch(array $data) |
||
| 216 | { |
||
| 217 | if (empty($data['name'])) { |
||
| 218 | return false; |
||
| 219 | } |
||
| 220 | |||
| 221 | if (!empty($data['removed']) && isset($data['_id'])) { |
||
| 222 | $this->_watches->remove( |
||
| 223 | array('_id' => new MongoId($data['_id'])), |
||
| 224 | array('w' => 1) |
||
| 225 | ); |
||
| 226 | return true; |
||
| 227 | } |
||
| 228 | |||
| 229 | if (empty($data['_id'])) { |
||
| 230 | $this->_watches->insert( |
||
| 231 | $data, |
||
| 232 | array('w' => 1) |
||
| 233 | ); |
||
| 234 | return true; |
||
| 235 | } |
||
| 236 | |||
| 237 | |||
| 238 | $data['_id'] = new MongoId($data['_id']); |
||
| 239 | $this->_watches->update( |
||
| 240 | array('_id' => $data['_id']), |
||
| 241 | $data, |
||
| 242 | array('w' => 1) |
||
| 243 | ); |
||
| 244 | return true; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * {@inheritdoc} |
||
| 249 | */ |
||
| 250 | public function getAllWatches() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritdoc} |
||
| 258 | */ |
||
| 259 | public function truncateWatches() |
||
| 263 | |||
| 264 | private function paginate($options) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Converts arrays + MongoCursors into Xhgui_Profile instances. |
||
| 311 | * |
||
| 312 | * @param array|MongoCursor $data The data to transform. |
||
| 313 | * |
||
| 314 | * @return Xhgui_Profile[] The transformed/wrapped results. |
||
| 315 | */ |
||
| 316 | protected function _wrap($data) |
||
| 331 | } |
||
| 332 |
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.