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 |
||
149 | public function trimDataSamples(int $limit = null): int |
||
150 | { |
||
151 | $affectedRows = 0; |
||
152 | $db = Craft::$app->getDb(); |
||
153 | $quotedTable = $db->quoteTableName('{{%webperf_data_samples}}'); |
||
154 | $limit = $limit ?? Webperf::$settings->dataSamplesStoredLimit; |
||
155 | |||
156 | if ($limit !== null) { |
||
157 | // https://stackoverflow.com/questions/578867/sql-query-delete-all-records-from-the-table-except-latest-n |
||
158 | try { |
||
159 | if ($db->getIsMysql()) { |
||
160 | // Handle MySQL |
||
161 | $affectedRows = $db->createCommand(/** @lang mysql */ |
||
162 | " |
||
163 | DELETE FROM {$quotedTable} |
||
164 | WHERE id NOT IN ( |
||
165 | SELECT id |
||
166 | FROM ( |
||
167 | SELECT id |
||
168 | FROM {$quotedTable} |
||
169 | ORDER BY dateUpdated DESC |
||
170 | LIMIT {$limit} |
||
171 | ) foo |
||
172 | ) |
||
173 | " |
||
174 | )->execute(); |
||
175 | } |
||
176 | if ($db->getIsPgsql()) { |
||
177 | // Handle Postgres |
||
178 | $affectedRows = $db->createCommand(/** @lang mysql */ |
||
179 | " |
||
180 | DELETE FROM {$quotedTable} |
||
181 | WHERE id NOT IN ( |
||
182 | SELECT id |
||
183 | FROM ( |
||
184 | SELECT id |
||
185 | FROM {$quotedTable} |
||
186 | ORDER BY \"dateUpdated\" DESC |
||
187 | LIMIT {$limit} |
||
188 | ) foo |
||
189 | ) |
||
190 | " |
||
191 | )->execute(); |
||
192 | } |
||
193 | } catch (\Exception $e) { |
||
194 | Craft::error($e->getMessage(), __METHOD__); |
||
195 | } |
||
196 | Craft::info( |
||
197 | Craft::t( |
||
198 | 'webperf', |
||
199 | 'Trimmed {rows} from webperf_data_samples table', |
||
200 | ['rows' => $affectedRows] |
||
201 | ), |
||
202 | __METHOD__ |
||
203 | ); |
||
204 | } |
||
205 | |||
206 | return $affectedRows; |
||
207 | } |
||
209 |