Conditions | 11 |
Paths | 72 |
Total Lines | 72 |
Code Lines | 51 |
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 |
||
121 | public function actionDashboardSlowestPages( |
||
122 | string $range = 'day', |
||
123 | string $column = 'pageLoad', |
||
124 | int $limit = 3, |
||
125 | int $siteId = 0 |
||
126 | ): Response { |
||
127 | PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
||
128 | $data = []; |
||
129 | $days = 1; |
||
130 | switch ($range) { |
||
131 | case 'day': |
||
132 | $days = 1; |
||
133 | break; |
||
134 | case 'week': |
||
135 | $days = 7; |
||
136 | break; |
||
137 | case 'month': |
||
138 | $days = 30; |
||
139 | break; |
||
140 | } |
||
141 | // Different dbs do it different ways |
||
142 | $stats = null; |
||
143 | $db = Craft::$app->getDb(); |
||
144 | if ($db->getIsMysql()) { |
||
145 | // Query the db |
||
146 | $query = (new Query()) |
||
147 | ->from('{{%webperf_data_samples}}') |
||
148 | ->select([ |
||
149 | 'url', |
||
150 | 'title', |
||
151 | 'AVG('.$column.') AS avg', |
||
152 | ]) |
||
153 | ->where("dateUpdated >= ( CURDATE() - INTERVAL '{$days}' DAY )"); |
||
154 | if ((int)$siteId !== 0) { |
||
155 | $query->andWhere(['siteId' => $siteId]); |
||
156 | } |
||
157 | $query |
||
158 | ->orderBy('avg DESC') |
||
159 | ->groupBy('url') |
||
160 | ->limit($limit); |
||
161 | $stats = $query->all(); |
||
162 | } |
||
163 | if ($db->getIsPgsql()) { |
||
164 | // Query the db |
||
165 | $query = (new Query()) |
||
166 | ->from('{{%webperf_data_samples}}') |
||
167 | ->select([ |
||
168 | 'url', |
||
169 | 'title', |
||
170 | 'AVG("'.$column.'") AS avg', |
||
171 | ]) |
||
172 | ->where("\"dateUpdated\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )"); |
||
173 | if ((int)$siteId !== 0) { |
||
174 | $query->andWhere(['siteId' => $siteId]); |
||
175 | $query |
||
176 | ->orderBy('avg DESC') |
||
177 | ->groupBy('url') |
||
178 | ->limit($limit); |
||
179 | } |
||
180 | $stats = $query->all(); |
||
181 | } |
||
182 | if ($stats) { |
||
183 | // Decode any emojis in the title |
||
184 | foreach ($stats as &$stat) { |
||
185 | if (!empty($stat['title'])) { |
||
186 | $stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); |
||
187 | } |
||
188 | } |
||
189 | $data = $stats; |
||
190 | } |
||
191 | |||
192 | return $this->asJson($data); |
||
193 | } |
||
246 |