Conditions | 10 |
Paths | 60 |
Total Lines | 77 |
Code Lines | 54 |
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 |
||
55 | public function actionPagesIndex( |
||
56 | string $sort = 'pageLoad|desc', |
||
57 | int $page = 1, |
||
58 | int $per_page = 20, |
||
59 | $filter = '', |
||
60 | $siteId = 0 |
||
61 | ): Response { |
||
62 | PermissionHelper::controllerPermissionCheck('webperf:pages'); |
||
63 | $data = []; |
||
64 | $sortField = 'pageLoad'; |
||
65 | $sortType = 'DESC'; |
||
66 | // Figure out the sorting type |
||
67 | if ($sort !== '') { |
||
68 | if (strpos($sort, '|') === false) { |
||
69 | $sortField = $sort; |
||
70 | } else { |
||
71 | list($sortField, $sortType) = explode('|', $sort); |
||
72 | } |
||
73 | } |
||
74 | // Query the db table |
||
75 | $offset = ($page - 1) * $per_page; |
||
76 | $query = (new Query()) |
||
77 | ->select([ |
||
78 | 'url', |
||
79 | 'MIN(title) AS title', |
||
80 | 'COUNT(url) AS cnt', |
||
81 | 'AVG(pageLoad) AS pageLoad', |
||
82 | 'AVG(craftDbCnt) AS craftDbCnt', |
||
83 | 'AVG(craftTwigCnt) AS craftTwigCnt', |
||
84 | 'AVG(craftOtherCnt) AS craftOtherCnt', |
||
85 | 'AVG(craftTotalMemory) AS craftTotalMemory', |
||
86 | ]) |
||
87 | ->from(['{{%webperf_data_samples}}']) |
||
88 | ->offset($offset); |
||
89 | if ((int)$siteId !== 0) { |
||
90 | $query->where(['siteId' => $siteId]); |
||
91 | } |
||
92 | if ($filter !== '') { |
||
93 | $query->where(['like', 'url', $filter]); |
||
94 | $query->orWhere(['like', 'title', $filter]); |
||
95 | } |
||
96 | $query |
||
97 | ->orderBy("{$sortField} {$sortType}") |
||
98 | ->groupBy('url') |
||
99 | ->limit($per_page); |
||
100 | |||
101 | $stats = $query->all(); |
||
102 | if ($stats) { |
||
103 | // Decode any emojis in the title |
||
104 | foreach ($stats as &$stat) { |
||
105 | if (!empty($stat['title'])) { |
||
106 | $stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); |
||
107 | } |
||
108 | } |
||
109 | // Format the data for the API |
||
110 | $data['data'] = $stats; |
||
111 | $query = (new Query()) |
||
112 | ->from(['{{%webperf_data_samples}}']) |
||
113 | ->groupBy('url'); |
||
114 | if ($filter !== '') { |
||
115 | $query->where(['like', 'url', $filter]); |
||
116 | $query->orWhere(['like', 'title', $filter]); |
||
117 | } |
||
118 | $count = $query->count(); |
||
119 | $data['links']['pagination'] = [ |
||
120 | 'total' => $count, |
||
121 | 'per_page' => $per_page, |
||
122 | 'current_page' => $page, |
||
123 | 'last_page' => ceil($count / $per_page), |
||
124 | 'next_page_url' => null, |
||
125 | 'prev_page_url' => null, |
||
126 | 'from' => $offset + 1, |
||
127 | 'to' => $offset + ($count > $per_page ? $per_page : $count), |
||
128 | ]; |
||
129 | } |
||
130 | |||
131 | return $this->asJson($data); |
||
132 | } |
||
213 |