Conditions | 18 |
Paths | 60 |
Total Lines | 117 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
211 | public function actionPageDetail( |
||
212 | string $start = '', |
||
213 | string $end = '', |
||
214 | string $sort = 'pageLoad|DESC', |
||
215 | int $page = 1, |
||
216 | int $per_page = 20, |
||
217 | $filter = '', |
||
218 | $pageUrl = '', |
||
219 | $siteId = 0 |
||
220 | ): Response { |
||
221 | PermissionHelper::controllerPermissionCheck('webperf:pages'); |
||
222 | $data = []; |
||
223 | $sortField = 'pageLoad'; |
||
224 | $sortType = 'DESC'; |
||
225 | // Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
||
226 | $end = date('Y-m-d', strtotime($end.'+1 day')); |
||
227 | $pageUrl = urldecode($pageUrl); |
||
228 | // Figure out the sorting type |
||
229 | if ($sort !== '') { |
||
230 | if (strpos($sort, '|') === false) { |
||
231 | $sortField = $sort; |
||
232 | } else { |
||
233 | list($sortField, $sortType) = explode('|', $sort); |
||
234 | } |
||
235 | } |
||
236 | // Query the db table |
||
237 | $offset = ($page - 1) * $per_page; |
||
238 | $query = (new Query()) |
||
239 | ->from(['{{%webperf_data_samples}}']) |
||
240 | ->offset($offset) |
||
241 | ->where(['url' => $pageUrl]) |
||
242 | ->andWhere(['between', 'dateCreated', $start, $end]) |
||
243 | ; |
||
244 | if ((int)$siteId !== 0) { |
||
245 | $query->andWhere(['siteId' => $siteId]); |
||
246 | } |
||
247 | if ($filter !== '') { |
||
248 | $query |
||
249 | ->andWhere(['like', 'device', $filter]) |
||
250 | ->orWhere(['like', 'os', $filter]) |
||
251 | ->orWhere(['like', 'browser', $filter]) |
||
252 | ->orWhere(['like', 'countryCode', $filter]) |
||
253 | ; |
||
254 | } |
||
255 | $query |
||
256 | ->orderBy("{$sortField} {$sortType}") |
||
257 | ->limit($per_page) |
||
258 | ; |
||
259 | $stats = $query->all(); |
||
260 | if ($stats) { |
||
261 | $user = Craft::$app->getUser()->getIdentity(); |
||
262 | // Compute the largest page load time |
||
263 | $maxTotalPageLoad = 0; |
||
264 | foreach ($stats as &$stat) { |
||
265 | // Determine the stat type |
||
266 | if (!empty($stat['pageLoad']) && !empty($stat['craftTotalMs'])) { |
||
267 | $stat['type'] = 'both'; |
||
268 | } |
||
269 | if (empty($stat['firstByte'])) { |
||
270 | $stat['type'] = 'craft'; |
||
271 | } |
||
272 | if (empty($stat['craftTotalMs'])) { |
||
273 | $stat['type'] = 'frontend'; |
||
274 | } |
||
275 | if ($stat['pageLoad'] > $maxTotalPageLoad) { |
||
276 | $maxTotalPageLoad = (int)$stat['pageLoad']; |
||
277 | } |
||
278 | } |
||
279 | // Massage the stats |
||
280 | foreach ($stats as &$stat) { |
||
281 | if (!empty($stats['dateCreated'])) { |
||
282 | $date = DateTimeHelper::toDateTime($stats['dateCreated']); |
||
283 | $stats['dateCreated'] = $date->format('Y-m-d H:i:s'); |
||
284 | } |
||
285 | $stat['mobile'] = (bool)$stat['mobile']; |
||
286 | $stat['maxTotalPageLoad'] = (int)$maxTotalPageLoad; |
||
287 | // Decode any emojis in the title |
||
288 | if (!empty($stat['title'])) { |
||
289 | $stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); |
||
290 | } |
||
291 | $stat['deleteLink'] = UrlHelper::actionUrl('webperf/data-samples/delete-sample-by-id', [ |
||
292 | 'id' => $stat['id'] |
||
293 | ]); |
||
294 | // Override based on permissions |
||
295 | if (!$user->can('webperf:delete-data-samples')) { |
||
296 | $stat['deleteLink'] = ''; |
||
297 | } |
||
298 | } |
||
299 | // Format the data for the API |
||
300 | $data['data'] = $stats; |
||
301 | $query = (new Query()) |
||
302 | ->from(['{{%webperf_data_samples}}']) |
||
303 | ->where(['url' => $pageUrl]) |
||
304 | ->andWhere(['between', 'dateCreated', $start, $end]) |
||
305 | ; |
||
306 | if ($filter !== '') { |
||
307 | $query |
||
308 | ->andWhere(['like', 'device', $filter]) |
||
309 | ->orWhere(['like', 'os', $filter]) |
||
310 | ->orWhere(['like', 'browser', $filter]) |
||
311 | ->orWhere(['like', 'countryCode', $filter]) |
||
312 | ; |
||
313 | } |
||
314 | $count = $query->count(); |
||
315 | $data['links']['pagination'] = [ |
||
316 | 'total' => $count, |
||
317 | 'per_page' => $per_page, |
||
318 | 'current_page' => $page, |
||
319 | 'last_page' => ceil($count / $per_page), |
||
320 | 'next_page_url' => null, |
||
321 | 'prev_page_url' => null, |
||
322 | 'from' => $offset + 1, |
||
323 | 'to' => $offset + ($count > $per_page ? $per_page : $count), |
||
324 | ]; |
||
325 | } |
||
326 | |||
327 | return $this->asJson($data); |
||
328 | } |
||
333 |