Conditions | 9 |
Paths | 72 |
Total Lines | 53 |
Code Lines | 36 |
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 actionDashboardStatsAverage( |
||
56 | string $range = 'day', |
||
57 | string $column = 'pageLoad', |
||
58 | int $siteId = 0 |
||
59 | ): Response { |
||
60 | PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
||
61 | $data = []; |
||
62 | $days = 1; |
||
63 | switch ($range) { |
||
64 | case 'day': |
||
65 | $days = 1; |
||
66 | break; |
||
67 | case 'week': |
||
68 | $days = 7; |
||
69 | break; |
||
70 | case 'month': |
||
71 | $days = 30; |
||
72 | break; |
||
73 | } |
||
74 | // Different dbs do it different ways |
||
75 | $stats = null; |
||
76 | $db = Craft::$app->getDb(); |
||
77 | if ($db->getIsMysql()) { |
||
78 | // Query the db |
||
79 | $query = (new Query()) |
||
80 | ->from('{{%webperf_data_samples}}') |
||
81 | ->select([ |
||
82 | 'AVG('.$column.') AS avg', |
||
83 | ]) |
||
84 | ->where("dateUpdated >= ( CURDATE() - INTERVAL '{$days}' DAY )"); |
||
85 | if ((int)$siteId !== 0) { |
||
86 | $query->andWhere(['siteId' => $siteId]); |
||
87 | } |
||
88 | $stats = $query->all(); |
||
89 | } |
||
90 | if ($db->getIsPgsql()) { |
||
91 | // Query the db |
||
92 | $query = (new Query()) |
||
93 | ->from('{{%retour_stats}}') |
||
94 | ->select([ |
||
95 | 'AVG("'.$column.'") AS avg', |
||
96 | ]) |
||
97 | ->where("\"dateUpdated\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )"); |
||
98 | if ((int)$siteId !== 0) { |
||
99 | $query->andWhere(['siteId' => $siteId]); |
||
100 | } |
||
101 | $stats = $query->all(); |
||
102 | } |
||
103 | if ($stats) { |
||
104 | $data = ArrayHelper::getColumn($stats, 'avg'); |
||
105 | } |
||
106 | |||
107 | return $this->asJson($data); |
||
108 | } |
||
161 |