| Conditions | 8 |
| Paths | 50 |
| Total Lines | 82 |
| Code Lines | 59 |
| 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 |
||
| 88 | public function data( |
||
| 89 | $pageUrl = '', |
||
| 90 | string $start = '', |
||
| 91 | string $end = '', |
||
| 92 | $siteId = 0 |
||
| 93 | ): array { |
||
| 94 | $data = []; |
||
| 95 | $db = Craft::$app->getDb(); |
||
| 96 | // Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
||
| 97 | $end = date('Y-m-d', strtotime($end.'+1 day')); |
||
| 98 | $pageUrl = urldecode($pageUrl); |
||
| 99 | if ($db->getIsMysql()) { |
||
| 100 | // Query the db table |
||
| 101 | $query = (new Query()) |
||
| 102 | ->select([ |
||
| 103 | 'COUNT(url) AS cnt', |
||
| 104 | |||
| 105 | 'AVG(pageLoad) AS pageLoad', |
||
| 106 | 'AVG(domInteractive) AS domInteractive', |
||
| 107 | 'AVG(firstContentfulPaint) AS firstContentfulPaint', |
||
| 108 | 'AVG(firstPaint) AS firstPaint', |
||
| 109 | 'AVG(firstByte) AS firstByte', |
||
| 110 | 'AVG(connect) AS connect', |
||
| 111 | 'AVG(dns) AS dns', |
||
| 112 | |||
| 113 | 'AVG(craftTotalMs) AS craftTotalMs', |
||
| 114 | 'AVG(craftDbMs) AS craftDbMs', |
||
| 115 | 'AVG(craftDbCnt) AS craftDbCnt', |
||
| 116 | 'AVG(craftTwigMs) AS craftTwigMs', |
||
| 117 | 'AVG(craftTwigCnt) AS craftTwigCnt', |
||
| 118 | 'AVG(craftOtherMs) AS craftOtherMs', |
||
| 119 | 'AVG(craftOtherCnt) AS craftOtherCnt', |
||
| 120 | 'AVG(craftTotalMemory) AS craftTotalMemory', |
||
| 121 | ]) |
||
| 122 | ->from(['{{%webperf_data_samples}}']) |
||
| 123 | ->where(['between', 'dateCreated', $start, $end]); |
||
| 124 | if (!empty($pageUrl)) { |
||
| 125 | $query->andWhere(['url' => $pageUrl]); |
||
| 126 | } |
||
| 127 | if ((int)$siteId !== 0) { |
||
| 128 | $query->andWhere(['siteId' => $siteId]); |
||
| 129 | } |
||
| 130 | $stats = $query->all(); |
||
| 131 | } |
||
| 132 | if ($db->getIsPgsql()) { |
||
| 133 | // Query the db table |
||
| 134 | $query = (new Query()) |
||
| 135 | ->select([ |
||
| 136 | 'COUNT("url") AS cnt', |
||
| 137 | |||
| 138 | 'AVG("pageLoad") AS pageLoad', |
||
| 139 | 'AVG("domInteractive") AS domInteractive', |
||
| 140 | 'AVG("firstContentfulPaint") AS firstContentfulPaint', |
||
| 141 | 'AVG("firstPaint") AS firstPaint', |
||
| 142 | 'AVG("firstByte") AS firstByte', |
||
| 143 | 'AVG("connect") AS connect', |
||
| 144 | 'AVG("dns") AS dns', |
||
| 145 | |||
| 146 | 'AVG("craftTotalMs") AS craftTotalMs', |
||
| 147 | 'AVG("craftDbMs") AS craftDbMs', |
||
| 148 | 'AVG("craftDbCnt") AS craftDbCnt', |
||
| 149 | 'AVG("craftTwigMs") AS craftTwigMs', |
||
| 150 | 'AVG("craftTwigCnt") AS craftTwigCnt', |
||
| 151 | 'AVG("craftOtherMs") AS craftOtherMs', |
||
| 152 | 'AVG("craftOtherCnt") AS craftOtherCnt', |
||
| 153 | 'AVG("craftTotalMemory") AS craftTotalMemory', |
||
| 154 | ]) |
||
| 155 | ->from(['{{%webperf_data_samples}}']) |
||
| 156 | ->where(['between', 'dateCreated', $start, $end]); |
||
| 157 | if (!empty($pageUrl)) { |
||
| 158 | $query->andWhere(['url' => $pageUrl]); |
||
| 159 | } |
||
| 160 | if ((int)$siteId !== 0) { |
||
| 161 | $query->andWhere(['siteId' => $siteId]); |
||
| 162 | } |
||
| 163 | $stats = $query->all(); |
||
| 164 | } |
||
| 165 | if ($stats) { |
||
| 166 | $data = $stats[0]; |
||
| 167 | } |
||
| 168 | |||
| 169 | return $data; |
||
| 170 | } |
||
| 172 |