| Conditions | 34 |
| Paths | 4003 |
| Total Lines | 135 |
| Code Lines | 85 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 10 | ||
| 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 | $this->data[$tableName] = [ |
||
| 167 | 'TableName' => $tableName, |
||
| 168 | 'SizeAfter' => 0, |
||
| 169 | 'SizeBefore' => 0, |
||
| 170 | 'Actions' => [], |
||
| 171 | ]; |
||
| 172 | |||
| 173 | if (in_array($tableName, $tablesToKeep, true)) { |
||
| 174 | if ($this->debug) { |
||
| 175 | $this->data[$tableName]['Actions'][] = 'Skipped because it is in list of tables to keep.'; |
||
| 176 | } |
||
| 177 | |||
| 178 | continue; |
||
| 179 | } |
||
| 180 | if ($this->database->isEmptyTable($tableName)) { |
||
| 181 | if ($this->debug) { |
||
| 182 | $this->data[$tableName]['Actions'][] = 'Skipped because table is empty.'; |
||
| 183 | } |
||
| 184 | |||
| 185 | continue; |
||
| 186 | } |
||
| 187 | $this->data[$tableName]['SizeBefore'] = $this->database->getTableSizeInMegaBytes($tableName); |
||
| 188 | if ($this->selectedTables && ! in_array($tableName, $this->selectedTableList, true)) { |
||
| 189 | $this->data[$tableName]['Actions'][] = 'Skipped because it is not a selected table.'; |
||
| 190 | |||
| 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 | |||
| 199 | continue; |
||
| 200 | } |
||
| 201 | $outcome = $this->database->deleteObsoleteTables($tableName); |
||
| 202 | if ($outcome) { |
||
| 203 | $this->data[$tableName]['Actions'][] = 'Deleted because it is obsolete.'; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | if ($this->removeOldVersions) { |
||
| 208 | $outcome = $this->database->emptyVersionedTable($tableName); |
||
| 209 | if ($outcome) { |
||
| 210 | $this->data[$tableName]['Actions'][] = 'Remove all and replace with one entry for each record.'; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | if ($this->anonymise) { |
||
| 215 | $outcome = $this->anonymiser->AnonymiseTable($tableName); |
||
| 216 | if ($outcome) { |
||
| 217 | $this->data[$tableName]['Actions'][] = 'Anonymised Table.'; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | //get fields |
||
| 221 | $fields = $this->database->getAllFieldsForOneTable($tableName); |
||
| 222 | |||
| 223 | foreach ($fields as $fieldName) { |
||
| 224 | if ('ID' === substr($fieldName, -2)) { |
||
| 225 | if ($this->debug) { |
||
| 226 | $this->data[$tableName]['Actions'][] = ' ... ' . $fieldName . ': skipping!'; |
||
| 227 | } |
||
| 228 | |||
| 229 | continue; |
||
| 230 | } |
||
| 231 | if (in_array($fieldName, $fieldsToKeep, true)) { |
||
| 232 | if ($this->debug) { |
||
| 233 | $this->data[$tableName]['Actions'][] = ' ... ' . $fieldName . ': skipping!'; |
||
| 234 | } |
||
| 235 | |||
| 236 | continue; |
||
| 237 | } |
||
| 238 | |||
| 239 | $combo = $tableName . '.' . $fieldName; |
||
| 240 | if (in_array($combo, $fieldTableCombosToKeep, true)) { |
||
| 241 | if ($this->debug) { |
||
| 242 | $this->data[$tableName]['Actions'][] = ' ... ' . $fieldName . ': skipping.'; |
||
| 243 | } |
||
| 244 | |||
| 245 | continue; |
||
| 246 | } |
||
| 247 | if ($this->anonymise) { |
||
| 248 | $outcome = $this->anonymiser->AnonymiseTableField($tableName, $fieldName); |
||
| 249 | if ($outcome) { |
||
| 250 | $this->data[$tableName]['Actions'][] = ' ... ' . $fieldName . ': anonymised.'; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | if ($this->emptyFields) { |
||
| 254 | $columnSize = $this->database->getColumnSizeInMegabytes($tableName, $fieldName); |
||
| 255 | $test1 = $columnSize > $maxColumnSize; |
||
| 256 | $test2 = in_array($fieldName, $fieldsToBeCleaned, true); |
||
| 257 | $test3 = in_array($combo, $tableFieldCombosToBeCleaned, true); |
||
| 258 | if ($test1 || $test2 || $test3) { |
||
| 259 | $percentageToKeep = $test2 || $test3 ? 0 : $maxColumnSize / $columnSize; |
||
| 260 | $outcome = $this->database->removeOldColumnsFromTable($tableName, $fieldName, $percentageToKeep); |
||
| 261 | if ($outcome) { |
||
| 262 | $this->data[$tableName]['Actions'][] = ' ... ' . $fieldName . ': Removed most rows.'; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | // clean table |
||
| 269 | if ($this->removeRows) { |
||
| 270 | $removeAllRows = in_array($tableName, $tablesToBeCleaned, true); |
||
| 271 | if ($removeAllRows) { |
||
| 272 | $this->database->removeOldRowsFromTable($tableName, 0.01); |
||
| 273 | $this->data[$tableName]['Actions'][] = 'Removed most rows.'; |
||
| 274 | } else { |
||
| 275 | $tableSize = $this->database->getTableSizeInMegaBytes($tableName); |
||
| 276 | if ($tableSize > $maxTableSize) { |
||
| 277 | $percentageToKeep = $maxTableSize / $tableSize; |
||
| 278 | $this->database->removeOldRowsFromTable($tableName, $percentageToKeep); |
||
| 279 | $this->data[$tableName]['Actions'][] = 'Removed old rows.'; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | } |
||
| 283 | $this->data[$tableName]['SizeAfter'] = $this->database->getTableSizeInMegaBytes($tableName); |
||
| 284 | } |
||
| 441 |