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