| Conditions | 7 |
| Paths | 40 |
| Total Lines | 107 |
| Code Lines | 78 |
| 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 | $dateStart = new \DateTime($start); |
||
| 225 | $dateEnd = new \DateTime($end); |
||
| 226 | $interval = date_diff($dateStart, $dateEnd); |
||
| 227 | $dateFormat = '"%Y-%m-%d %l%p"'; |
||
| 228 | if ($interval->days > 30) { |
||
| 229 | $dateFormat = '"%Y-%m-%d"'; |
||
| 230 | } |
||
| 231 | // Different dbs do it different ways |
||
| 232 | $stats = null; |
||
| 233 | $db = Craft::$app->getDb(); |
||
| 234 | if ($db->getIsMysql()) { |
||
| 235 | // Query the db |
||
| 236 | $query = (new Query()) |
||
| 237 | ->from('{{%webperf_data_samples}}') |
||
| 238 | ->select([ |
||
| 239 | 'AVG(pageLoad) AS pageLoad', |
||
| 240 | 'AVG(domInteractive) AS domInteractive', |
||
| 241 | 'AVG(firstContentfulPaint) AS firstContentfulPaint', |
||
| 242 | 'AVG(firstPaint) AS firstPaint', |
||
| 243 | 'AVG(firstByte) AS firstByte', |
||
| 244 | 'AVG(connect) AS connect', |
||
| 245 | 'AVG(dns) AS dns', |
||
| 246 | 'AVG(craftTotalMs) AS craftTotalMs', |
||
| 247 | 'AVG(craftTwigMs) AS craftTwigMs', |
||
| 248 | 'AVG(craftDbMs) AS craftDbMs', |
||
| 249 | 'DATE_FORMAT(dateUpdated, '.$dateFormat.') AS sampleDate', |
||
| 250 | ]) |
||
| 251 | ->where(['between', 'dateUpdated', $start, $end]) |
||
| 252 | ; |
||
| 253 | if ((int)$siteId !== 0) { |
||
| 254 | $query->andWhere(['siteId' => $siteId]); |
||
| 255 | } |
||
| 256 | if ($pageUrl !== '') { |
||
| 257 | $query->andWhere(['url' => $pageUrl]); |
||
| 258 | } |
||
| 259 | $stats = $query |
||
| 260 | ->groupBy('sampleDate') |
||
| 261 | ->all(); |
||
| 262 | } |
||
| 263 | if ($db->getIsPgsql()) { |
||
| 264 | } |
||
| 265 | // Massage the data |
||
| 266 | if ($stats) { |
||
| 267 | $data[] = [ |
||
| 268 | 'name' => 'Database Queries', |
||
| 269 | 'data' => ArrayHelper::getColumn($stats, 'craftDbMs'), |
||
| 270 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 271 | ]; |
||
| 272 | $data[] = [ |
||
| 273 | 'name' => 'Twig Rendering', |
||
| 274 | 'data' => ArrayHelper::getColumn($stats, 'craftTwigMs'), |
||
| 275 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 276 | ]; |
||
| 277 | $data[] = [ |
||
| 278 | 'name' => 'Craft Rendering', |
||
| 279 | 'data' => ArrayHelper::getColumn($stats, 'craftTotalMs'), |
||
| 280 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 281 | ]; |
||
| 282 | $data[] = [ |
||
| 283 | 'name' => 'DNS Lookup', |
||
| 284 | 'data' => ArrayHelper::getColumn($stats, 'dns'), |
||
| 285 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 286 | ]; |
||
| 287 | $data[] = [ |
||
| 288 | 'name' => 'Connect', |
||
| 289 | 'data' => ArrayHelper::getColumn($stats, 'connect'), |
||
| 290 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 291 | ]; |
||
| 292 | $data[] = [ |
||
| 293 | 'name' => 'First Byte', |
||
| 294 | 'data' => ArrayHelper::getColumn($stats, 'firstByte'), |
||
| 295 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 296 | ]; |
||
| 297 | $data[] = [ |
||
| 298 | 'name' => 'First Paint', |
||
| 299 | 'data' => ArrayHelper::getColumn($stats, 'firstPaint'), |
||
| 300 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 301 | ]; |
||
| 302 | $data[] = [ |
||
| 303 | 'name' => 'First Contentful Paint', |
||
| 304 | 'data' => ArrayHelper::getColumn($stats, 'firstContentfulPaint'), |
||
| 305 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 306 | ]; |
||
| 307 | $data[] = [ |
||
| 308 | 'name' => 'DOM Interactive', |
||
| 309 | 'data' => ArrayHelper::getColumn($stats, 'domInteractive'), |
||
| 310 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 311 | ]; |
||
| 312 | $data[] = [ |
||
| 313 | 'name' => 'Page Load', |
||
| 314 | 'data' => ArrayHelper::getColumn($stats, 'pageLoad'), |
||
| 315 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 316 | ]; |
||
| 317 | } |
||
| 318 | |||
| 319 | return $this->asJson($data); |
||
| 320 | } |
||
| 338 |