Conditions | 5 |
Paths | 11 |
Total Lines | 58 |
Code Lines | 29 |
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 |
||
124 | public function trimDataSamples(int $limit = null): int |
||
125 | { |
||
126 | $affectedRows = 0; |
||
127 | $db = Craft::$app->getDb(); |
||
128 | $quotedTable = $db->quoteTableName('{{%webperf_data_samples}}'); |
||
129 | $limit = $limit ?? Webperf::$settings->dataSamplesStoredLimit; |
||
130 | |||
131 | if ($limit !== null) { |
||
132 | // https://stackoverflow.com/questions/578867/sql-query-delete-all-records-from-the-table-except-latest-n |
||
133 | try { |
||
134 | if ($db->getIsMysql()) { |
||
135 | // Handle MySQL |
||
136 | $affectedRows = $db->createCommand(/** @lang mysql */ |
||
137 | " |
||
138 | DELETE FROM {$quotedTable} |
||
139 | WHERE id NOT IN ( |
||
140 | SELECT id |
||
141 | FROM ( |
||
142 | SELECT id |
||
143 | FROM {$quotedTable} |
||
144 | ORDER BY dateUpdated DESC |
||
145 | LIMIT {$limit} |
||
146 | ) foo |
||
147 | ) |
||
148 | " |
||
149 | )->execute(); |
||
150 | } |
||
151 | if ($db->getIsPgsql()) { |
||
152 | // Handle Postgres |
||
153 | $affectedRows = $db->createCommand(/** @lang mysql */ |
||
154 | " |
||
155 | DELETE FROM {$quotedTable} |
||
156 | WHERE id NOT IN ( |
||
157 | SELECT id |
||
158 | FROM ( |
||
159 | SELECT id |
||
160 | FROM {$quotedTable} |
||
161 | ORDER BY \"dateUpdated\" DESC |
||
162 | LIMIT {$limit} |
||
163 | ) foo |
||
164 | ) |
||
165 | " |
||
166 | )->execute(); |
||
167 | } |
||
168 | } catch (\Exception $e) { |
||
169 | Craft::error($e->getMessage(), __METHOD__); |
||
170 | } |
||
171 | Craft::info( |
||
172 | Craft::t( |
||
173 | 'webperf', |
||
174 | 'Trimmed {rows} from webperf_data_samples table', |
||
175 | ['rows' => $affectedRows] |
||
176 | ), |
||
177 | __METHOD__ |
||
178 | ); |
||
179 | } |
||
180 | |||
181 | return $affectedRows; |
||
182 | } |
||
184 |