Complex classes like Backup 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 Backup, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Backup |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * |
||
| 17 | * @var Backup |
||
| 18 | */ |
||
| 19 | protected static $instance; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Backup directory |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $directory = BACKUPFOLDER; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Aviable extentions |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $ext = [ |
||
| 32 | 'sql', |
||
| 33 | 'zip', |
||
| 34 | 'gzip', |
||
| 35 | ]; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Patterns for backup file names |
||
| 39 | * key - regex pattern, value - boolean (allow delete) |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $filePatterns = [ |
||
| 43 | // для файлів із авт. підбором імені. |
||
| 44 | '/^[a-zA-Z]{1,10}_[0-9]{2}-[0-9]{2}-[0-9]{4}_[0-9]{2}.[0-9]{2}.[0-9]{2}.(zip|gzip|sql|txt)$/' => [ |
||
| 45 | 'allowDelete' => TRUE, |
||
| 46 | 'type' => 'default', |
||
| 47 | ], |
||
| 48 | // для старіших бекапів із обновлення |
||
| 49 | '/^[0-9]{10}.(zip|gzip|sql|txt)$/' => [ |
||
| 50 | 'allowDelete' => TRUE, |
||
| 51 | 'type' => 'update', |
||
| 52 | ], |
||
| 53 | // для новіших бекапів із обновлення |
||
| 54 | '/^backup.(zip|gzip|sql|txt)$/' => [ |
||
| 55 | 'allowDelete' => FALSE, |
||
| 56 | 'type' => 'update', |
||
| 57 | ], |
||
| 58 | ]; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * |
||
| 62 | * @var CodeIgniter |
||
| 63 | */ |
||
| 64 | protected $ci; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | public $error; |
||
| 71 | |||
| 72 | public function __construct() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * |
||
| 79 | * @return Backup |
||
| 80 | */ |
||
| 81 | public static function create() { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Setting the backup value into the DB |
||
| 88 | * @param string $key |
||
| 89 | * @param string|int $value |
||
| 90 | */ |
||
| 91 | public function setSetting($key, $value) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Getting the backup value from the DB |
||
| 102 | * @param string |
||
| 103 | * @param string $key |
||
| 104 | * @return mixed settings array or specified value |
||
| 105 | */ |
||
| 106 | public function getSetting($key = NULL) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Creating the backup file |
||
| 129 | * @param string $ext extention (txt|zip|gzip) |
||
| 130 | * @param string $prefix |
||
| 131 | * @param bool|string $fullName (optional) filename |
||
| 132 | * @param array $params |
||
| 133 | * @return false|string filename|FALSE |
||
| 134 | */ |
||
| 135 | public function createBackup($ext, $prefix = NULL, $fullName = FALSE, $params = []) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Init DB for backup with msql driver |
||
| 171 | */ |
||
| 172 | private function initBackupDB() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * |
||
| 203 | * @return boolean |
||
| 204 | */ |
||
| 205 | public function deleteOldFiles() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * |
||
| 248 | * @return boolean |
||
| 249 | */ |
||
| 250 | public function getOldestFileToDelete() { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Getting list of backup files |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | public function backupFiles() { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Checking file name by pattern |
||
| 328 | * @param string $fileName |
||
| 329 | * @param boolean|string $returnValue |
||
| 330 | * @return boolean|string |
||
| 331 | */ |
||
| 332 | protected function checkFileName($fileName, $returnValue = FALSE) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Deleting backup file |
||
| 343 | * @param string $file |
||
| 344 | * @return boolean true on success, false on error |
||
| 345 | */ |
||
| 346 | public function deleteFile($file) { |
||
| 355 | |||
| 356 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.