| Conditions | 6 |
| Paths | 20 |
| Total Lines | 82 |
| Code Lines | 57 |
| 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 |
||
| 213 | public function actionPagesAreaChart( |
||
| 214 | string $start, |
||
| 215 | string $end, |
||
| 216 | $pageUrl = '', |
||
| 217 | int $siteId = 0 |
||
| 218 | ): Response { |
||
| 219 | PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
||
| 220 | $data = []; |
||
| 221 | // Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
||
| 222 | $end = date('Y-m-d', strtotime($end.'+1 day')); |
||
| 223 | $pageUrl = urldecode($pageUrl); |
||
| 224 | // Different dbs do it different ways |
||
| 225 | $stats = null; |
||
| 226 | $db = Craft::$app->getDb(); |
||
| 227 | if ($db->getIsMysql()) { |
||
| 228 | // Query the db |
||
| 229 | $query = (new Query()) |
||
| 230 | ->from('{{%webperf_data_samples}}') |
||
| 231 | ->select([ |
||
| 232 | 'AVG(pageLoad) AS pageLoad', |
||
| 233 | 'AVG(domInteractive) AS domInteractive', |
||
| 234 | 'AVG(firstContentfulPaint) AS firstContentfulPaint', |
||
| 235 | 'AVG(firstPaint) AS firstPaint', |
||
| 236 | 'AVG(firstByte) AS firstByte', |
||
| 237 | 'AVG(connect) AS connect', |
||
| 238 | 'AVG(dns) AS dns', |
||
| 239 | "DATE_FORMAT(dateUpdated, '%Y-%m-%d %l%p') AS sampleDate", |
||
| 240 | ]) |
||
| 241 | ->where(['between', 'dateUpdated', $start, $end]) |
||
| 242 | ; |
||
| 243 | if ((int)$siteId !== 0) { |
||
| 244 | $query->andWhere(['siteId' => $siteId]); |
||
| 245 | } |
||
| 246 | if ($pageUrl !== '') { |
||
| 247 | $query->andWhere(['url' => $pageUrl]); |
||
| 248 | } |
||
| 249 | $stats = $query |
||
| 250 | ->groupBy('sampleDate') |
||
| 251 | ->all(); |
||
| 252 | } |
||
| 253 | if ($db->getIsPgsql()) { |
||
| 254 | } |
||
| 255 | // Massage the data |
||
| 256 | if ($stats) { |
||
| 257 | $data[] = [ |
||
| 258 | 'name' => 'DNS Lookup', |
||
| 259 | 'data' => ArrayHelper::getColumn($stats, 'dns'), |
||
| 260 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 261 | ]; |
||
| 262 | $data[] = [ |
||
| 263 | 'name' => 'Connect', |
||
| 264 | 'data' => ArrayHelper::getColumn($stats, 'connect'), |
||
| 265 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 266 | ]; |
||
| 267 | $data[] = [ |
||
| 268 | 'name' => 'First Byte', |
||
| 269 | 'data' => ArrayHelper::getColumn($stats, 'firstByte'), |
||
| 270 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 271 | ]; |
||
| 272 | $data[] = [ |
||
| 273 | 'name' => 'First Paint', |
||
| 274 | 'data' => ArrayHelper::getColumn($stats, 'firstPaint'), |
||
| 275 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 276 | ]; |
||
| 277 | $data[] = [ |
||
| 278 | 'name' => 'First Contentful Paint', |
||
| 279 | 'data' => ArrayHelper::getColumn($stats, 'firstContentfulPaint'), |
||
| 280 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 281 | ]; |
||
| 282 | $data[] = [ |
||
| 283 | 'name' => 'DOM Interactive', |
||
| 284 | 'data' => ArrayHelper::getColumn($stats, 'domInteractive'), |
||
| 285 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 286 | ]; |
||
| 287 | $data[] = [ |
||
| 288 | 'name' => 'Page Load', |
||
| 289 | 'data' => ArrayHelper::getColumn($stats, 'pageLoad'), |
||
| 290 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 291 | ]; |
||
| 292 | } |
||
| 293 | |||
| 294 | return $this->asJson($data); |
||
| 295 | } |
||
| 313 |