| Conditions | 9 |
| Paths | 72 |
| Total Lines | 63 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 121 | public function actionDashboardSlowestPages( |
||
| 122 | string $range = 'day', |
||
| 123 | string $column = 'pageLoad', |
||
| 124 | int $limit = 3, |
||
| 125 | int $siteId = 0 |
||
| 126 | ): Response { |
||
| 127 | PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
||
| 128 | $data = []; |
||
| 129 | $days = 1; |
||
| 130 | switch ($range) { |
||
| 131 | case 'day': |
||
| 132 | $days = 1; |
||
| 133 | break; |
||
| 134 | case 'week': |
||
| 135 | $days = 7; |
||
| 136 | break; |
||
| 137 | case 'month': |
||
| 138 | $days = 30; |
||
| 139 | break; |
||
| 140 | } |
||
| 141 | // Different dbs do it different ways |
||
| 142 | $stats = null; |
||
| 143 | $db = Craft::$app->getDb(); |
||
| 144 | if ($db->getIsMysql()) { |
||
| 145 | // Query the db |
||
| 146 | $query = (new Query()) |
||
| 147 | ->from('{{%webperf_data_samples}}') |
||
| 148 | ->select([ |
||
| 149 | '*', |
||
| 150 | 'AVG('.$column.') AS avg', |
||
| 151 | ]) |
||
| 152 | ->where("dateUpdated >= ( CURDATE() - INTERVAL '{$days}' DAY )"); |
||
| 153 | if ((int)$siteId !== 0) { |
||
| 154 | $query->andWhere(['siteId' => $siteId]); |
||
| 155 | } |
||
| 156 | $query |
||
| 157 | ->orderBy('avg DESC') |
||
| 158 | ->groupBy('url') |
||
| 159 | ->limit($limit); |
||
| 160 | $stats = $query->all(); |
||
| 161 | } |
||
| 162 | if ($db->getIsPgsql()) { |
||
| 163 | // Query the db |
||
| 164 | $query = (new Query()) |
||
| 165 | ->from('{{%webperf_data_samples}}') |
||
| 166 | ->select([ |
||
| 167 | 'AVG("'.$column.'") AS avg', |
||
| 168 | ]) |
||
| 169 | ->where("\"dateUpdated\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )"); |
||
| 170 | if ((int)$siteId !== 0) { |
||
| 171 | $query->andWhere(['siteId' => $siteId]); |
||
| 172 | } |
||
| 173 | $stats = $query->all(); |
||
| 174 | } |
||
| 175 | if ($stats) { |
||
| 176 | $data = ArrayHelper::getColumn($stats, 'avg'); |
||
| 177 | } |
||
| 178 | |||
| 179 | Craft::debug('Slowest Pages: '.print_r($stats, true), __METHOD__); |
||
| 180 | |||
| 181 | $data = []; |
||
| 182 | |||
| 183 | return $this->asJson($data); |
||
| 184 | } |
||
| 237 |