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