| Total Complexity | 55 |
| Total Lines | 437 |
| Duplicated Lines | 0 % |
| Changes | 24 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CleanUp often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CleanUp, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class CleanUp extends BuildTask |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var bool if set to FALSE, keep it from showing in the list |
||
| 16 | * and from being executable through URL or CLI |
||
| 17 | */ |
||
| 18 | protected $enabled = true; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string Shown in the overview on the {@link TaskRunner} |
||
| 22 | * HTML or CLI interface. Should be short and concise, no HTML allowed. |
||
| 23 | */ |
||
| 24 | protected $title = 'Cleanup and anonymise database - CAREFUL! Data will be deleted.'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string Describe the implications the task has, |
||
| 28 | * and the changes it makes. Accepts HTML formatting. |
||
| 29 | */ |
||
| 30 | protected $description = 'Goes through database and deletes data that may expose personal information and bloat database.'; |
||
| 31 | |||
| 32 | protected $forReal = false; |
||
| 33 | |||
| 34 | protected $anonymise = false; |
||
| 35 | |||
| 36 | protected $removeObsolete = false; |
||
| 37 | |||
| 38 | protected $removeOldVersions = false; |
||
| 39 | |||
| 40 | protected $debug = false; |
||
| 41 | |||
| 42 | protected $emptyFields = false; |
||
| 43 | |||
| 44 | protected $removeRows = false; |
||
| 45 | |||
| 46 | protected $selectedTables = false; |
||
| 47 | |||
| 48 | protected $selectedTableList = []; |
||
| 49 | |||
| 50 | protected $data = []; |
||
| 51 | |||
| 52 | private static $tables_to_delete_forever = []; |
||
| 53 | |||
| 54 | private static $tables_to_be_cleaned = []; |
||
| 55 | |||
| 56 | private static $fields_to_be_cleaned = []; |
||
| 57 | |||
| 58 | private static $field_table_comboes_to_be_cleaned = []; |
||
| 59 | |||
| 60 | private static $tables_to_keep = []; |
||
| 61 | |||
| 62 | private static $fields_to_keep = [ |
||
| 63 | 'ClassName', |
||
| 64 | 'Created', |
||
| 65 | 'LastEdited', |
||
| 66 | ]; |
||
| 67 | |||
| 68 | private static $field_table_combos_to_keep = []; |
||
| 69 | |||
| 70 | private static $max_table_size_in_mb = 20; |
||
| 71 | |||
| 72 | private static $max_column_size_in_mb = 2; |
||
| 73 | |||
| 74 | private static $dependencies = [ |
||
| 75 | 'anonymiser' => '%$' . Anonymiser::class, |
||
| 76 | 'database' => '%$' . DatabaseActions::class, |
||
| 77 | ]; |
||
| 78 | |||
| 79 | private $anonymiser; |
||
| 80 | |||
| 81 | private $database; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Set a custom url segment (to follow dev/tasks/). |
||
| 85 | * |
||
| 86 | * @config |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | private static $segment = 'database-share-clean-up'; |
||
| 91 | |||
| 92 | public function setAnonymiser($anonymiser) |
||
| 93 | { |
||
| 94 | $this->anonymiser = $anonymiser; |
||
| 95 | |||
| 96 | return $this; |
||
| 97 | } |
||
| 98 | |||
| 99 | public function setDatabase($database) |
||
| 100 | { |
||
| 101 | $this->database = $database; |
||
| 102 | |||
| 103 | return $this; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Implement this method in the task subclass to |
||
| 108 | * execute via the TaskRunner. |
||
| 109 | * |
||
| 110 | * @param HTTPRequest $request |
||
| 111 | */ |
||
| 112 | public function run($request) |
||
| 113 | { |
||
| 114 | $this->anonymise = (bool) $request->getVar('anonymise'); |
||
| 115 | $this->removeObsolete = (bool) $request->getVar('removeobsolete'); |
||
| 116 | $this->removeOldVersions = (bool) $request->getVar('removeoldversions'); |
||
| 117 | $this->removeRows = (bool) $request->getVar('removerows'); |
||
| 118 | $this->emptyFields = (bool) $request->getVar('emptyfields'); |
||
| 119 | |||
| 120 | $this->selectedTables = (bool) $request->getVar('selectedtables'); |
||
| 121 | $this->debug = (bool) $request->getVar('debug'); |
||
| 122 | $this->forReal = (bool) $request->getVar('forreal'); |
||
| 123 | if ($this->forReal) { |
||
| 124 | $this->debug = true; |
||
| 125 | } |
||
| 126 | $this->selectedTableList = $request->getVar('selectedtablelist') ?? []; |
||
| 127 | |||
| 128 | $this->anonymiser->setDatabaseActions($this->database); |
||
| 129 | $this->database->setForReal($this->forReal); |
||
| 130 | $this->database->setDebug($this->debug); |
||
| 131 | |||
| 132 | if ($this->forReal) { |
||
| 133 | FlushNowImplementor::do_flush('<h3>Running in FOR REAL mode</h3>', 'bad'); |
||
| 134 | } else { |
||
| 135 | FlushNowImplementor::do_flush('<h3>Not runing FOR REAL</h3>', 'good'); |
||
| 136 | } |
||
| 137 | if ($this->anonymise) { |
||
| 138 | $this->anonymiser->AnonymisePresets(); |
||
| 139 | } |
||
| 140 | |||
| 141 | $this->createForm(); |
||
| 142 | $this->runInner(); |
||
| 143 | $this->createTable(); |
||
| 144 | echo "MEMORY USED:" . $this->formatBytes(memory_get_peak_usage()); |
||
| 145 | } |
||
| 146 | |||
| 147 | protected function runInner() |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | protected function createForm() |
||
| 350 | <label>debug</label> |
||
| 351 | </div> |
||
| 352 | <hr /> |
||
| 353 | <div class="field" style="padding: 10px;"> |
||
| 354 | <input type="submit" value="let's do it!" /> |
||
| 355 | </div> |
||
| 356 | </div> |
||
| 357 | |||
| 358 | |||
| 359 | html; |
||
| 360 | } |
||
| 361 | |||
| 362 | protected function createTable() |
||
| 439 | </table> |
||
| 440 | </form>'; |
||
| 441 | } |
||
| 442 | |||
| 443 | private function formatBytes($size, $precision = 2) |
||
| 449 | } |
||
| 450 | } |
||
| 451 |