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) |
||
| 70 | { |
||
| 71 | $opts = $this->_mapper->convert($options); |
||
| 72 | |||
| 73 | $totalRows = $this->_collection->find( |
||
| 74 | $opts['conditions'], |
||
| 75 | array('_id' => 1))->count(); |
||
| 76 | |||
| 77 | $totalPages = max(ceil($totalRows / $opts['perPage']), 1); |
||
| 78 | $page = 1; |
||
| 79 | if (isset($options['page'])) { |
||
| 80 | $page = min(max($options['page'], 1), $totalPages); |
||
| 81 | } |
||
| 82 | |||
| 83 | $projection = false; |
||
| 84 | if (isset($options['projection'])) { |
||
| 85 | if ($options['projection'] === true) { |
||
| 86 | $projection = array('meta' => 1, 'profile.main()' => 1); |
||
| 87 | } else { |
||
| 88 | $projection = $options['projection']; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($projection === false) { |
||
| 93 | $cursor = $this->_collection->find($opts['conditions']) |
||
| 94 | ->sort($opts['sort']) |
||
| 95 | ->skip((int)($page - 1) * $opts['perPage']) |
||
| 96 | ->limit($opts['perPage']); |
||
| 97 | } else { |
||
| 98 | $cursor = $this->_collection->find($opts['conditions'], $projection) |
||
| 99 | ->sort($opts['sort']) |
||
| 100 | ->skip((int)($page - 1) * $opts['perPage']) |
||
| 101 | ->limit($opts['perPage']); |
||
| 102 | } |
||
| 103 | |||
| 104 | return array( |
||
| 105 | 'results' => $this->_wrap($cursor), |
||
| 106 | 'sort' => $opts['sort'], |
||
| 107 | 'direction' => $opts['direction'], |
||
| 108 | 'page' => $page, |
||
| 109 | 'perPage' => $opts['perPage'], |
||
| 110 | 'totalPages' => $totalPages |
||
| 111 | ); |
||
| 112 | } |
||
| 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()) |
||
| 126 | { |
||
| 127 | $result = $this->_mapper->convert(array( |
||
| 128 | 'conditions' => $search + array('simple_url' => $url) |
||
| 129 | )); |
||
| 130 | $match = $result['conditions']; |
||
| 131 | |||
| 132 | $col = '$meta.request_date'; |
||
| 133 | View Code Duplication | if (!empty($search['limit']) && $search['limit'][0] == "P") { |
|
| 134 | $col = '$meta.request_ts'; |
||
| 135 | } |
||
| 136 | |||
| 137 | $results = $this->_collection->aggregate(array( |
||
| 138 | array('$match' => $match), |
||
| 139 | array( |
||
| 140 | '$project' => array( |
||
| 141 | 'date' => $col, |
||
| 142 | 'profile.main()' => 1 |
||
| 143 | ) |
||
| 144 | ), |
||
| 145 | array( |
||
| 146 | '$group' => array( |
||
| 147 | '_id' => '$date', |
||
| 148 | 'row_count' => array('$sum' => 1), |
||
| 149 | 'wall_times' => array('$push' => '$profile.main().wt'), |
||
| 150 | 'cpu_times' => array('$push' => '$profile.main().cpu'), |
||
| 151 | 'mu_times' => array('$push' => '$profile.main().mu'), |
||
| 152 | 'pmu_times' => array('$push' => '$profile.main().pmu'), |
||
| 153 | ) |
||
| 154 | ), |
||
| 155 | array( |
||
| 156 | '$project' => array( |
||
| 157 | 'date' => '$date', |
||
| 158 | 'row_count' => '$row_count', |
||
| 159 | 'raw_index' => array( |
||
| 160 | '$multiply' => array( |
||
| 161 | '$row_count', |
||
| 162 | $percentile / 100 |
||
| 163 | ) |
||
| 164 | ), |
||
| 165 | 'wall_times' => '$wall_times', |
||
| 166 | 'cpu_times' => '$cpu_times', |
||
| 167 | 'mu_times' => '$mu_times', |
||
| 168 | 'pmu_times' => '$pmu_times', |
||
| 169 | ) |
||
| 170 | ), |
||
| 171 | array('$sort' => array('_id' => 1)), |
||
| 172 | )); |
||
| 173 | |||
| 174 | if (empty($results['result'])) { |
||
| 175 | return array(); |
||
| 176 | } |
||
| 177 | $keys = array( |
||
| 178 | 'wall_times' => 'wt', |
||
| 179 | 'cpu_times' => 'cpu', |
||
| 180 | 'mu_times' => 'mu', |
||
| 181 | 'pmu_times' => 'pmu' |
||
| 182 | ); |
||
| 183 | foreach ($results['result'] as &$result) { |
||
| 184 | $result['date'] = ($result['_id'] instanceof MongoDate) ? date('Y-m-d H:i:s', $result['_id']->sec) : $result['_id']; |
||
| 185 | unset($result['_id']); |
||
| 186 | $index = max(round($result['raw_index']) - 1, 0); |
||
| 187 | foreach ($keys as $key => $out) { |
||
| 188 | sort($result[$key]); |
||
| 189 | $result[$out] = isset($result[$key][$index]) ? $result[$key][$index] : null; |
||
| 190 | unset($result[$key]); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | return $results['result']; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get the Average metrics for a URL |
||
| 198 | * |
||
| 199 | * This will group data by date and returns only the |
||
| 200 | * avg + date, making the data ideal for time series graphs |
||
| 201 | * |
||
| 202 | * @param string $url |
||
| 203 | * @param array $search Search options containing date_start and or date_end |
||
| 204 | * @return array Array of metrics grouped by date |
||
| 205 | */ |
||
| 206 | public function getAvgsForUrl($url, $search = array()) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get a paginated set of results. |
||
| 246 | * |
||
| 247 | * @param array $options The find options to use. |
||
| 248 | * @return array An array of result data. |
||
| 249 | */ |
||
| 250 | public function getAll($options = array()) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Encodes a profile to avoid mongodb key errors. |
||
| 257 | * @param array $profile |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | protected function encodeProfile($profile) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Insert a profile run. |
||
| 282 | * |
||
| 283 | * Does unchecked inserts. |
||
| 284 | * |
||
| 285 | * @param array $profile The profile data to save. |
||
| 286 | */ |
||
| 287 | public function insert($profile) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Used to truncate a collection. |
||
| 295 | * |
||
| 296 | * Primarly used in test cases to reset the test db. |
||
| 297 | * |
||
| 298 | * @return boolean |
||
| 299 | */ |
||
| 300 | public function truncate() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Converts arrays + MongoCursors into Xhgui_Profile instances. |
||
| 307 | * |
||
| 308 | * @param array|MongoCursor $data The data to transform. |
||
| 309 | * @return Xhgui_Profile|array The transformed/wrapped results. |
||
| 310 | */ |
||
| 311 | protected function _wrap($data) |
||
| 326 | } |
||
| 327 |
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.