Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DatabasebackupLog 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 DatabasebackupLog, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class DatabasebackupLog extends DataObject |
||
| 13 | { |
||
| 14 | private static $singular_name = "Database Backup"; |
||
|
|
|||
| 15 | |||
| 16 | private static $plural_name = "Database Backups"; |
||
| 17 | |||
| 18 | private static $db = array( |
||
| 19 | "Title" => "Varchar(255)", |
||
| 20 | "Notes" => "Text", |
||
| 21 | "FullLocation" => "Varchar(255)", |
||
| 22 | "SizeInBytes" => "Int", |
||
| 23 | "DebugMessage" => "Text" |
||
| 24 | ); |
||
| 25 | |||
| 26 | private static $indexes = array( |
||
| 27 | "FullLocation" => true |
||
| 28 | ); |
||
| 29 | |||
| 30 | private static $default_sort = "Created DESC"; |
||
| 31 | |||
| 32 | private static $casting = array( |
||
| 33 | "SizeInMegabytes" => "Int" |
||
| 34 | ); |
||
| 35 | |||
| 36 | private static $summary_fields = array( |
||
| 37 | "Title" => "Title", |
||
| 38 | "Created" => "Created", |
||
| 39 | "SizeInMegabytes" => "Size (in Mb.)", |
||
| 40 | ); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * location for backup file e.g. /var/backups/db.sql |
||
| 44 | * @var String |
||
| 45 | */ |
||
| 46 | private static $full_location_for_db_backup_file = ""; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * number of cycles before the database backups get deleted forgood... |
||
| 50 | * @var Int |
||
| 51 | */ |
||
| 52 | private static $max_db_copies = 3; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * at the moment only the gzip compression is supported! |
||
| 56 | * @var String |
||
| 57 | */ |
||
| 58 | private static $compression = ""; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * for security reasons we set this to false ... |
||
| 62 | * @var Boolean |
||
| 63 | */ |
||
| 64 | private static $allow_restores_in_live_environment = false; |
||
| 65 | |||
| 66 | public function canCreate($member = null) |
||
| 70 | |||
| 71 | public function canDelete($member = null) |
||
| 75 | |||
| 76 | public function canEdit($member = null) |
||
| 80 | |||
| 81 | public function getCMSFields() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @casting |
||
| 95 | * @return Int |
||
| 96 | */ |
||
| 97 | public function getSizeInMegabytes() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Adds a button the Site Config page of the CMS to rebuild the Lucene search index. |
||
| 108 | */ |
||
| 109 | public function getCMSActions() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * if backup does not exist then make it ... |
||
| 147 | * set size |
||
| 148 | */ |
||
| 149 | public function onBeforeWrite() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * delete me if file does not exist |
||
| 182 | */ |
||
| 183 | public function onAfterWrite() |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * delete file if I get deleted |
||
| 195 | */ |
||
| 196 | public function onBeforeDelete() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * |
||
| 207 | * @return Boolean |
||
| 208 | */ |
||
| 209 | public function restoreDatabaseBackup() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * we have this so that when we restore a database |
||
| 235 | * we dont loose the backup information... |
||
| 236 | * saves all the logs to session |
||
| 237 | */ |
||
| 238 | protected function saveToSession() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * |
||
| 253 | * retrieves and updates all the logs from session |
||
| 254 | */ |
||
| 255 | protected function retrieveFromSession() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * move all of the database copies up one, |
||
| 271 | * deleting the upper one. |
||
| 272 | * |
||
| 273 | * Returns the name of the file name freed up ... (by moving all of them one up...) |
||
| 274 | * |
||
| 275 | * @param string $fileLocation |
||
| 276 | * |
||
| 277 | * @return string File Location |
||
| 278 | * |
||
| 279 | */ |
||
| 280 | protected function cycleDatabaseBackupFiles($fileLocation) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * returns best file location with compression extension... |
||
| 351 | * |
||
| 352 | * @return String | Null |
||
| 353 | */ |
||
| 354 | protected function getFullLocationWithExtension() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * returns file name for older back up file (cycled one) |
||
| 369 | * |
||
| 370 | * @param String $fileLocation |
||
| 371 | * @param Int $position |
||
| 372 | * |
||
| 373 | * @return String |
||
| 374 | */ |
||
| 375 | protected function olderBackupFileName($fileLocation, $position) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * check for existing backups |
||
| 382 | * |
||
| 383 | */ |
||
| 384 | public function requireDefaultRecords() |
||
| 413 | } |
||
| 414 |