Conditions | 35 |
Paths | 4010 |
Total Lines | 139 |
Code Lines | 87 |
Lines | 0 |
Ratio | 0 % |
Changes | 12 | ||
Bugs | 0 | Features | 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 |
||
147 | protected function runInner() |
||
148 | { |
||
149 | $maxTableSize = $this->Config()->get('max_table_size_in_mb'); |
||
150 | $maxColumnSize = $this->Config()->get('max_column_size_in_mb'); |
||
151 | |||
152 | $tablesToDeleteForever = $this->Config()->get('tables_to_delete_forever'); |
||
153 | |||
154 | $tablesToKeep = $this->Config()->get('tables_to_keep'); |
||
155 | $fieldsToKeep = $this->Config()->get('fields_to_keep'); |
||
156 | $fieldTableCombosToKeep = $this->Config()->get('field_table_combos_to_keep'); |
||
157 | |||
158 | $tablesToBeCleaned = $this->Config()->get('tables_to_be_cleaned'); |
||
159 | $fieldsToBeCleaned = $this->Config()->get('fields_to_be_cleaned'); |
||
160 | $tableFieldCombosToBeCleaned = $this->Config()->get('field_table_comboes_to_be_cleaned'); |
||
161 | |||
162 | $tables = $this->database->getAllTables(); |
||
163 | foreach ($tables as $tableName) { |
||
164 | if (!$this->database->tableExists($tableName)) { |
||
165 | continue; |
||
166 | } |
||
167 | $this->data[$tableName] = [ |
||
168 | 'TableName' => $tableName, |
||
169 | 'SizeAfter' => 0, |
||
170 | 'SizeBefore' => 0, |
||
171 | 'Actions' => [], |
||
172 | ]; |
||
173 | |||
174 | if (in_array($tableName, $tablesToKeep, true)) { |
||
175 | if ($this->debug) { |
||
176 | $this->data[$tableName]['Actions'][] = 'Skipped because it is in list of tables to keep.'; |
||
177 | } |
||
178 | |||
179 | continue; |
||
180 | } |
||
181 | |||
182 | if ($this->removeObsolete) { |
||
183 | if (in_array($tableName, $tablesToDeleteForever, true)) { |
||
184 | $this->database->deleteTable($tableName); |
||
185 | $this->data[$tableName]['Actions'][] = 'DELETING FOREVER.'; |
||
186 | |||
187 | continue; |
||
188 | } |
||
189 | $outcome = $this->database->deleteObsoleteTables($tableName); |
||
190 | if ($outcome) { |
||
191 | $this->data[$tableName]['Actions'][] = 'Deleted because it is obsolete.'; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | if ($this->database->isEmptyTable($tableName)) { |
||
196 | if ($this->debug) { |
||
197 | $this->data[$tableName]['Actions'][] = 'Skipped because table is empty.'; |
||
198 | } |
||
199 | |||
200 | continue; |
||
201 | } |
||
202 | $this->data[$tableName]['SizeBefore'] = $this->database->getTableSizeInMegaBytes($tableName); |
||
203 | if ($this->selectedTables && !in_array($tableName, $this->selectedTableList, true)) { |
||
204 | $this->data[$tableName]['Actions'][] = 'Skipped because it is not a selected table.'; |
||
205 | |||
206 | continue; |
||
207 | } |
||
208 | |||
209 | if ($this->removeOldVersions) { |
||
210 | $outcome = $this->database->emptyVersionedTable($tableName); |
||
211 | if ($outcome) { |
||
212 | $this->data[$tableName]['Actions'][] = 'Remove all versions.'; |
||
213 | } |
||
214 | } |
||
215 | |||
216 | if ($this->anonymise) { |
||
217 | $outcome = $this->anonymiser->AnonymiseTable($tableName); |
||
218 | if ($outcome) { |
||
219 | $this->data[$tableName]['Actions'][] = 'Anonymised Table.'; |
||
220 | } |
||
221 | } |
||
222 | //get fields |
||
223 | $fields = $this->database->getAllFieldsForOneTable($tableName); |
||
224 | |||
225 | foreach ($fields as $fieldName) { |
||
226 | if ('ID' === substr((string) $fieldName, -2)) { |
||
227 | if ($this->debug) { |
||
228 | $this->data[$tableName]['Actions'][] = ' ... ' . $fieldName . ': skipping!'; |
||
229 | } |
||
230 | |||
231 | continue; |
||
232 | } |
||
233 | if (in_array($fieldName, $fieldsToKeep, true)) { |
||
234 | if ($this->debug) { |
||
235 | $this->data[$tableName]['Actions'][] = ' ... ' . $fieldName . ': skipping (field is marked as KEEP)!'; |
||
236 | } |
||
237 | |||
238 | continue; |
||
239 | } |
||
240 | |||
241 | $combo = $tableName . '.' . $fieldName; |
||
242 | if (in_array($combo, $fieldTableCombosToKeep, true)) { |
||
243 | if ($this->debug) { |
||
244 | $this->data[$tableName]['Actions'][] = ' ... ' . $fieldName . ': skipping (table.field is marked as KEEP).'; |
||
245 | } |
||
246 | |||
247 | continue; |
||
248 | } |
||
249 | if ($this->anonymise) { |
||
250 | $outcome = $this->anonymiser->AnonymiseTableField($tableName, $fieldName); |
||
251 | if ($outcome) { |
||
252 | $this->data[$tableName]['Actions'][] = ' ... ' . $fieldName . ': anonymised.'; |
||
253 | } |
||
254 | } |
||
255 | if ($this->emptyFields) { |
||
256 | $columnSize = $this->database->getColumnSizeInMegabytes($tableName, $fieldName); |
||
257 | $test1 = $columnSize > $maxColumnSize; |
||
258 | $test2 = in_array($fieldName, $fieldsToBeCleaned, true); |
||
259 | $test3 = in_array($combo, $tableFieldCombosToBeCleaned, true); |
||
260 | if ($test1 || $test2 || $test3) { |
||
261 | $percentageToKeep = $test2 || $test3 ? 0 : $maxColumnSize / $columnSize; |
||
262 | $outcome = $this->database->removeOldColumnsFromTable($tableName, $fieldName, $percentageToKeep); |
||
263 | if ($outcome) { |
||
264 | $this->data[$tableName]['Actions'][] = ' ... ' . $fieldName . ': Removed most rows.'; |
||
265 | } |
||
266 | } |
||
267 | } |
||
268 | } |
||
269 | |||
270 | // clean table |
||
271 | if ($this->removeRows) { |
||
272 | $removeAllRows = in_array($tableName, $tablesToBeCleaned, true); |
||
273 | if ($removeAllRows) { |
||
274 | $this->database->removeOldRowsFromTable($tableName, 0.01); |
||
275 | $this->data[$tableName]['Actions'][] = 'Removed most rows.'; |
||
276 | } else { |
||
277 | $tableSize = $this->database->getTableSizeInMegaBytes($tableName); |
||
278 | if ($tableSize > $maxTableSize) { |
||
279 | $percentageToKeep = $maxTableSize / $tableSize; |
||
280 | $this->database->removeOldRowsFromTable($tableName, $percentageToKeep); |
||
281 | $this->data[$tableName]['Actions'][] = 'Removed old rows.'; |
||
282 | } |
||
283 | } |
||
284 | } |
||
285 | $this->data[$tableName]['SizeAfter'] = $this->database->getTableSizeInMegaBytes($tableName); |
||
286 | } |
||
451 |