| Conditions | 8 |
| Paths | 18 |
| Total Lines | 60 |
| Code Lines | 40 |
| 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 |
||
| 109 | public function actionDashboardSlowestPages( |
||
| 110 | int $days = 30, |
||
| 111 | string $column = 'pageLoad', |
||
| 112 | int $limit = 3, |
||
| 113 | int $siteId = 0 |
||
| 114 | ): Response { |
||
| 115 | PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
||
| 116 | $data = []; |
||
| 117 | // Different dbs do it different ways |
||
| 118 | $stats = null; |
||
| 119 | $db = Craft::$app->getDb(); |
||
| 120 | if ($db->getIsMysql()) { |
||
| 121 | // Query the db |
||
| 122 | $query = (new Query()) |
||
| 123 | ->from('{{%webperf_data_samples}}') |
||
| 124 | ->select([ |
||
| 125 | 'url', |
||
| 126 | 'title', |
||
| 127 | 'AVG('.$column.') AS avg', |
||
| 128 | ]) |
||
| 129 | ->where("dateUpdated >= ( CURDATE() - INTERVAL '{$days}' DAY )"); |
||
| 130 | if ((int)$siteId !== 0) { |
||
| 131 | $query->andWhere(['siteId' => $siteId]); |
||
| 132 | } |
||
| 133 | $query |
||
| 134 | ->orderBy('avg DESC') |
||
| 135 | ->groupBy('url') |
||
| 136 | ->limit($limit); |
||
| 137 | $stats = $query->all(); |
||
| 138 | } |
||
| 139 | if ($db->getIsPgsql()) { |
||
| 140 | // Query the db |
||
| 141 | $query = (new Query()) |
||
| 142 | ->from('{{%webperf_data_samples}}') |
||
| 143 | ->select([ |
||
| 144 | 'url', |
||
| 145 | 'title', |
||
| 146 | 'AVG("'.$column.'") AS avg', |
||
| 147 | ]) |
||
| 148 | ->where("\"dateUpdated\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )"); |
||
| 149 | if ((int)$siteId !== 0) { |
||
| 150 | $query->andWhere(['siteId' => $siteId]); |
||
| 151 | $query |
||
| 152 | ->orderBy('avg DESC') |
||
| 153 | ->groupBy('url') |
||
| 154 | ->limit($limit); |
||
| 155 | } |
||
| 156 | $stats = $query->all(); |
||
| 157 | } |
||
| 158 | if ($stats) { |
||
| 159 | // Decode any emojis in the title |
||
| 160 | foreach ($stats as &$stat) { |
||
| 161 | if (!empty($stat['title'])) { |
||
| 162 | $stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | $data = $stats; |
||
| 166 | } |
||
| 167 | |||
| 168 | return $this->asJson($data); |
||
| 169 | } |
||
| 222 |