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