| Conditions | 8 |
| Paths | 16 |
| Total Lines | 72 |
| Lines | 3 |
| Ratio | 4.17 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 69 | public function getPercentileForUrl($percentile, $url, $search = array()) |
||
| 70 | { |
||
| 71 | $result = $this->_mapper->convert(array( |
||
| 72 | 'conditions' => $search + array('simple_url' => $url) |
||
| 73 | )); |
||
| 74 | $match = $result['conditions']; |
||
| 75 | |||
| 76 | $col = '$meta.request_date'; |
||
| 77 | View Code Duplication | if (!empty($search['limit']) && $search['limit'][0] == "P") { |
|
| 78 | $col = '$meta.request_ts'; |
||
| 79 | } |
||
| 80 | |||
| 81 | $results = $this->_collection->aggregate(array( |
||
| 82 | array('$match' => $match), |
||
| 83 | array( |
||
| 84 | '$project' => array( |
||
| 85 | 'date' => $col, |
||
| 86 | 'profile.main()' => 1 |
||
| 87 | ) |
||
| 88 | ), |
||
| 89 | array( |
||
| 90 | '$group' => array( |
||
| 91 | '_id' => '$date', |
||
| 92 | 'row_count' => array('$sum' => 1), |
||
| 93 | 'wall_times' => array('$push' => '$profile.main().wt'), |
||
| 94 | 'cpu_times' => array('$push' => '$profile.main().cpu'), |
||
| 95 | 'mu_times' => array('$push' => '$profile.main().mu'), |
||
| 96 | 'pmu_times' => array('$push' => '$profile.main().pmu'), |
||
| 97 | ) |
||
| 98 | ), |
||
| 99 | array( |
||
| 100 | '$project' => array( |
||
| 101 | 'date' => '$date', |
||
| 102 | 'row_count' => '$row_count', |
||
| 103 | 'raw_index' => array( |
||
| 104 | '$multiply' => array( |
||
| 105 | '$row_count', |
||
| 106 | $percentile / 100 |
||
| 107 | ) |
||
| 108 | ), |
||
| 109 | 'wall_times' => '$wall_times', |
||
| 110 | 'cpu_times' => '$cpu_times', |
||
| 111 | 'mu_times' => '$mu_times', |
||
| 112 | 'pmu_times' => '$pmu_times', |
||
| 113 | ) |
||
| 114 | ), |
||
| 115 | array('$sort' => array('_id' => 1)), |
||
| 116 | ), |
||
| 117 | array('cursor' => array('batchSize' => 0)) |
||
| 118 | ); |
||
| 119 | |||
| 120 | if (empty($results['result'])) { |
||
| 121 | return array(); |
||
| 122 | } |
||
| 123 | $keys = array( |
||
| 124 | 'wall_times' => 'wt', |
||
| 125 | 'cpu_times' => 'cpu', |
||
| 126 | 'mu_times' => 'mu', |
||
| 127 | 'pmu_times' => 'pmu' |
||
| 128 | ); |
||
| 129 | foreach ($results['result'] as &$result) { |
||
| 130 | $result['date'] = ($result['_id'] instanceof MongoDate) ? date('Y-m-d H:i:s', $result['_id']->sec) : $result['_id']; |
||
| 131 | unset($result['_id']); |
||
| 132 | $index = max(round($result['raw_index']) - 1, 0); |
||
| 133 | foreach ($keys as $key => $out) { |
||
| 134 | sort($result[$key]); |
||
| 135 | $result[$out] = isset($result[$key][$index]) ? $result[$key][$index] : null; |
||
| 136 | unset($result[$key]); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | return $results['result']; |
||
| 140 | } |
||
| 141 | |||
| 277 |
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.