| Conditions | 15 |
| Paths | 120 |
| Total Lines | 115 |
| Code Lines | 79 |
| 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 |
||
| 184 | public function actionPageDetail( |
||
| 185 | string $sort = 'pageLoad|desc', |
||
| 186 | int $page = 1, |
||
| 187 | int $per_page = 20, |
||
| 188 | $filter = '', |
||
| 189 | $pageUrl = '', |
||
| 190 | $siteId = 0 |
||
| 191 | ): Response { |
||
| 192 | PermissionHelper::controllerPermissionCheck('webperf:pages'); |
||
| 193 | $data = []; |
||
| 194 | $sortField = 'pageLoad'; |
||
| 195 | $sortType = 'DESC'; |
||
| 196 | $pageUrl = urldecode($pageUrl); |
||
| 197 | // Figure out the sorting type |
||
| 198 | if ($sort !== '') { |
||
| 199 | if (strpos($sort, '|') === false) { |
||
| 200 | $sortField = $sort; |
||
| 201 | } else { |
||
| 202 | list($sortField, $sortType) = explode('|', $sort); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | if ($sortField === 'totalPageLoad') { |
||
| 206 | $sortField = 'pageLoad'; |
||
| 207 | } |
||
| 208 | // Query the db table |
||
| 209 | $offset = ($page - 1) * $per_page; |
||
| 210 | $query = (new Query()) |
||
| 211 | ->select([ |
||
| 212 | 'url', |
||
| 213 | 'title', |
||
| 214 | 'pageLoad', |
||
| 215 | 'domInteractive', |
||
| 216 | 'firstContentfulPaint', |
||
| 217 | 'firstPaint', |
||
| 218 | 'firstByte', |
||
| 219 | 'connect', |
||
| 220 | 'dns', |
||
| 221 | 'craftTotalMs', |
||
| 222 | 'craftDbCnt', |
||
| 223 | 'craftDbMs', |
||
| 224 | 'craftTwigCnt', |
||
| 225 | 'craftTwigMs', |
||
| 226 | 'craftTotalMemory', |
||
| 227 | ]) |
||
| 228 | ->from(['{{%webperf_data_samples}}']) |
||
| 229 | ->offset($offset) |
||
| 230 | ->where(['url' => $pageUrl]) |
||
| 231 | ; |
||
| 232 | if ((int)$siteId !== 0) { |
||
| 233 | $query->where(['siteId' => $siteId]); |
||
| 234 | } |
||
| 235 | if ($filter !== '') { |
||
| 236 | $query |
||
| 237 | ->where(['like', 'url', $filter]) |
||
| 238 | ->orWhere(['like', 'title', $filter]) |
||
| 239 | ; |
||
| 240 | } |
||
| 241 | $query |
||
| 242 | ->orderBy("{$sortField} {$sortType}") |
||
| 243 | ->limit($per_page) |
||
| 244 | ; |
||
| 245 | |||
| 246 | $stats = $query->all(); |
||
| 247 | if ($stats) { |
||
| 248 | // Compute the largest page load time |
||
| 249 | $maxTotalPageLoad = 0; |
||
| 250 | foreach ($stats as &$stat) { |
||
| 251 | if (empty($stat['pageLoad'])) { |
||
| 252 | $pageLoad = $stat['craftTotalMs']; |
||
| 253 | } else { |
||
| 254 | $pageLoad = $stat['pageLoad']; |
||
| 255 | } |
||
| 256 | if ($pageLoad > $maxTotalPageLoad) { |
||
| 257 | $maxTotalPageLoad = $pageLoad; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | // Massage the stats |
||
| 261 | $index = 1; |
||
| 262 | foreach ($stats as &$stat) { |
||
| 263 | $stat['id'] = $index++; |
||
| 264 | $stat['maxTotalPageLoad'] = $maxTotalPageLoad; |
||
| 265 | // If there is no frontend beacon timing, use the Craft timing |
||
| 266 | if (empty($stat['pageLoad'])) { |
||
| 267 | $stat['totalPageLoad'] = $stat['craftTotalMs']; |
||
| 268 | } else { |
||
| 269 | $stat['totalPageLoad'] = $stat['pageLoad']; |
||
| 270 | } |
||
| 271 | // Decode any emojis in the title |
||
| 272 | if (!empty($stat['title'])) { |
||
| 273 | $stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | // Format the data for the API |
||
| 277 | $data['data'] = $stats; |
||
| 278 | $query = (new Query()) |
||
| 279 | ->from(['{{%webperf_data_samples}}']) |
||
| 280 | ; |
||
| 281 | if ($filter !== '') { |
||
| 282 | $query->where(['like', 'url', $filter]); |
||
| 283 | $query->orWhere(['like', 'title', $filter]); |
||
| 284 | } |
||
| 285 | $count = $query->count(); |
||
| 286 | $data['links']['pagination'] = [ |
||
| 287 | 'total' => $count, |
||
| 288 | 'per_page' => $per_page, |
||
| 289 | 'current_page' => $page, |
||
| 290 | 'last_page' => ceil($count / $per_page), |
||
| 291 | 'next_page_url' => null, |
||
| 292 | 'prev_page_url' => null, |
||
| 293 | 'from' => $offset + 1, |
||
| 294 | 'to' => $offset + ($count > $per_page ? $per_page : $count), |
||
| 295 | ]; |
||
| 296 | } |
||
| 297 | |||
| 298 | return $this->asJson($data); |
||
| 299 | } |
||
| 304 |