| Conditions | 18 | 
| Paths | 60 | 
| Total Lines | 137 | 
| Code Lines | 93 | 
| 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  | 
            ||
| 58 | public function actionPagesIndex(  | 
            ||
| 59 | string $start = '',  | 
            ||
| 60 | string $end = '',  | 
            ||
| 61 | string $sort = 'totalPageLoad|desc',  | 
            ||
| 62 | int $page = 1,  | 
            ||
| 63 | int $per_page = 20,  | 
            ||
| 64 | $filter = '',  | 
            ||
| 65 | $siteId = 0  | 
            ||
| 66 |     ): Response { | 
            ||
| 67 |         PermissionHelper::controllerPermissionCheck('webperf:pages'); | 
            ||
| 68 | $data = [];  | 
            ||
| 69 | $sortField = 'totalPageLoad';  | 
            ||
| 70 | $sortType = 'DESC';  | 
            ||
| 71 | // Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00  | 
            ||
| 72 |         $end = date('Y-m-d', strtotime($end.'+1 day')); | 
            ||
| 73 | // Figure out the sorting type  | 
            ||
| 74 |         if ($sort !== '') { | 
            ||
| 75 |             if (strpos($sort, '|') === false) { | 
            ||
| 76 | $sortField = $sort;  | 
            ||
| 77 |             } else { | 
            ||
| 78 |                 list($sortField, $sortType) = explode('|', $sort); | 
            ||
| 79 | }  | 
            ||
| 80 | }  | 
            ||
| 81 | // Query the db table  | 
            ||
| 82 | $offset = ($page - 1) * $per_page;  | 
            ||
| 83 | $query = (new Query())  | 
            ||
| 84 | ->select([  | 
            ||
| 85 | 'url',  | 
            ||
| 86 | 'MIN(title) AS title',  | 
            ||
| 87 | 'COUNT(url) AS cnt',  | 
            ||
| 88 | 'GREATEST(AVG(pageLoad), AVG(craftTotalMs)) AS totalPageLoad',  | 
            ||
| 89 | 'AVG(pageLoad) AS pageLoad',  | 
            ||
| 90 | 'AVG(domInteractive) AS domInteractive',  | 
            ||
| 91 | 'AVG(firstContentfulPaint) AS firstContentfulPaint',  | 
            ||
| 92 | 'AVG(firstPaint) AS firstPaint',  | 
            ||
| 93 | 'AVG(firstByte) AS firstByte',  | 
            ||
| 94 | 'AVG(connect) AS connect',  | 
            ||
| 95 | 'AVG(dns) AS dns',  | 
            ||
| 96 | 'AVG(craftTotalMs) AS craftTotalMs',  | 
            ||
| 97 | 'AVG(craftDbCnt) AS craftDbCnt',  | 
            ||
| 98 | 'AVG(craftDbMs) AS craftDbMs',  | 
            ||
| 99 | 'AVG(craftTwigCnt) AS craftTwigCnt',  | 
            ||
| 100 | 'AVG(craftTwigMs) AS craftTwigMs',  | 
            ||
| 101 | 'AVG(craftTotalMemory) AS craftTotalMemory',  | 
            ||
| 102 | ])  | 
            ||
| 103 |             ->from(['{{%webperf_data_samples}}']) | 
            ||
| 104 | ->offset($offset)  | 
            ||
| 105 | ->where(['between', 'dateUpdated', $start, $end])  | 
            ||
| 106 | ;  | 
            ||
| 107 |         if ((int)$siteId !== 0) { | 
            ||
| 108 | $query->andWhere(['siteId' => $siteId]);  | 
            ||
| 109 | }  | 
            ||
| 110 |         if ($filter !== '') { | 
            ||
| 111 | $query  | 
            ||
| 112 | ->andWhere(['like', 'url', $filter])  | 
            ||
| 113 | ->orWhere(['like', 'title', $filter])  | 
            ||
| 114 | ;  | 
            ||
| 115 | }  | 
            ||
| 116 | $query  | 
            ||
| 117 |             ->orderBy("{$sortField} {$sortType}") | 
            ||
| 118 |             ->groupBy('url') | 
            ||
| 119 | ->limit($per_page)  | 
            ||
| 120 | ;  | 
            ||
| 121 | |||
| 122 | $stats = $query->all();  | 
            ||
| 123 |         if ($stats) { | 
            ||
| 124 | // Compute the largest page load time  | 
            ||
| 125 | $maxTotalPageLoad = 0;  | 
            ||
| 126 |             foreach ($stats as &$stat) { | 
            ||
| 127 | // Determine the stat type  | 
            ||
| 128 |                 if (!empty($stat['pageLoad']) && !empty($stat['craftTotalMs'])) { | 
            ||
| 129 | $stat['type'] = 'both';  | 
            ||
| 130 | }  | 
            ||
| 131 |                 if (empty($stat['pageLoad'])) { | 
            ||
| 132 | $stat['type'] = 'craft';  | 
            ||
| 133 | }  | 
            ||
| 134 |                 if (empty($stat['craftTotalMs'])) { | 
            ||
| 135 | $stat['type'] = 'frontend';  | 
            ||
| 136 | }  | 
            ||
| 137 |                 if ($stat['totalPageLoad'] > $maxTotalPageLoad) { | 
            ||
| 138 | $maxTotalPageLoad = $stat['totalPageLoad'];  | 
            ||
| 139 | }  | 
            ||
| 140 | }  | 
            ||
| 141 | // Massage the stats  | 
            ||
| 142 | $index = 1;  | 
            ||
| 143 |             foreach ($stats as &$stat) { | 
            ||
| 144 | $stat['id'] = $index++;  | 
            ||
| 145 | $stat['cnt'] = (int)$stat['cnt'];  | 
            ||
| 146 | $stat['totalPageLoad'] = (int)$stat['totalPageLoad'];  | 
            ||
| 147 | $stat['maxTotalPageLoad'] = (int)$maxTotalPageLoad;  | 
            ||
| 148 | // Decode any emojis in the title  | 
            ||
| 149 |                 if (!empty($stat['title'])) { | 
            ||
| 150 | $stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8');  | 
            ||
| 151 | }  | 
            ||
| 152 | // Set up the appropriate helper links  | 
            ||
| 153 |                 $stat['deleteLink'] = UrlHelper::actionUrl('webperf/data-samples/delete-samples-by-url', [ | 
            ||
| 154 | 'pageUrl' => $stat['url'],  | 
            ||
| 155 | 'siteId' => $siteId  | 
            ||
| 156 | ]);  | 
            ||
| 157 |                 $stat['detailPageUrl'] = UrlHelper::cpUrl('webperf/page-detail', [ | 
            ||
| 158 | 'pageUrl' => $stat['url'],  | 
            ||
| 159 | 'siteId' => $siteId,  | 
            ||
| 160 | ]);  | 
            ||
| 161 | // Override based on permissions  | 
            ||
| 162 | $user = Craft::$app->getUser()->getIdentity();  | 
            ||
| 163 |                 if (!$user->can('webperf:delete-data-samples')) { | 
            ||
| 164 | $stat['deleteLink'] = '';  | 
            ||
| 165 | }  | 
            ||
| 166 |                 if (!$user->can('webperf:page-detail')) { | 
            ||
| 167 | $stat['detailPageUrl'] = '';  | 
            ||
| 168 | }  | 
            ||
| 169 | }  | 
            ||
| 170 | // Format the data for the API  | 
            ||
| 171 | $data['data'] = $stats;  | 
            ||
| 172 | $query = (new Query())  | 
            ||
| 173 |                 ->from(['{{%webperf_data_samples}}']) | 
            ||
| 174 |                 ->groupBy('url') | 
            ||
| 175 | ->where(['between', 'dateUpdated', $start, $end])  | 
            ||
| 176 | ;  | 
            ||
| 177 |             if ($filter !== '') { | 
            ||
| 178 | $query->andWhere(['like', 'url', $filter]);  | 
            ||
| 179 | $query->orWhere(['like', 'title', $filter]);  | 
            ||
| 180 | }  | 
            ||
| 181 | $count = $query->count();  | 
            ||
| 182 | $data['links']['pagination'] = [  | 
            ||
| 183 | 'total' => $count,  | 
            ||
| 184 | 'per_page' => $per_page,  | 
            ||
| 185 | 'current_page' => $page,  | 
            ||
| 186 | 'last_page' => ceil($count / $per_page),  | 
            ||
| 187 | 'next_page_url' => null,  | 
            ||
| 188 | 'prev_page_url' => null,  | 
            ||
| 189 | 'from' => $offset + 1,  | 
            ||
| 190 | 'to' => $offset + ($count > $per_page ? $per_page : $count),  | 
            ||
| 191 | ];  | 
            ||
| 192 | }  | 
            ||
| 193 | |||
| 194 | return $this->asJson($data);  | 
            ||
| 195 | }  | 
            ||
| 335 |