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:
| 1 | <?php |
||
| 5 | class Backup { |
||
| 6 | |||
| 7 | private $excludes; |
||
| 8 | public $warnings = array(); |
||
| 9 | public $errors = array(); |
||
| 10 | private $backup_filename; |
||
| 11 | private $database_dump_filename; |
||
| 12 | private $backup_filepath = ''; |
||
| 13 | private $database_dump_filepath = ''; |
||
| 14 | private $status = null; |
||
| 15 | private $type = 'complete'; |
||
| 16 | |||
| 17 | public function __construct( $backup_filename, $database_dump_filename = null ) { |
||
| 21 | |||
| 22 | public function set_type( $type ) { |
||
| 25 | |||
| 26 | public function set_backup_filename( $filename ) { |
||
| 29 | |||
| 30 | public function set_status( Backup_Status $status ) { |
||
| 33 | |||
| 34 | public function set_excludes( Excludes $excludes ) { |
||
| 37 | |||
| 38 | public function run() { |
||
| 39 | |||
| 40 | Path::get_instance()->cleanup(); |
||
| 41 | |||
| 42 | if ( 'file' !== $this->type ) { |
||
| 43 | $this->backup_database(); |
||
| 44 | } |
||
| 45 | |||
| 46 | if ( 'database' !== $this->type ) { |
||
| 47 | $this->backup_files(); |
||
| 48 | } |
||
| 49 | |||
| 50 | Path::get_instance()->cleanup(); |
||
| 51 | |||
| 52 | } |
||
| 53 | |||
| 54 | public function backup_database() { |
||
| 55 | |||
| 56 | if ( $this->status ) { |
||
| 57 | $this->status->set_status( __( 'Backing up database...', 'backupwordpress' ) ); |
||
| 58 | } |
||
| 59 | |||
| 60 | $database_backup_engines = apply_filters( 'hmbkp_database_backup_engines', array( |
||
| 61 | new Mysqldump_Database_Backup_Engine, |
||
| 62 | new IMysqldump_Database_Backup_Engine, |
||
| 63 | ) ); |
||
| 64 | |||
| 65 | // Set the file backup engine settings |
||
| 66 | if ( $this->database_dump_filename ) { |
||
| 67 | foreach ( $database_backup_engines as &$backup_engine ) { |
||
| 68 | $backup_engine->set_backup_filename( $this->database_dump_filename ); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | // Dump the database |
||
| 73 | $database_dump = $this->perform_backup( $database_backup_engines ); |
||
| 74 | |||
| 75 | if ( is_a( $database_dump, __NAMESPACE__ . '\\Backup_Engine' ) ) { |
||
| 76 | $this->database_dump_filepath = $database_dump->get_backup_filepath(); |
||
| 77 | } |
||
| 78 | |||
| 79 | // Fire up the file backup engines |
||
| 80 | $file_backup_engines = apply_filters( 'hmbkp_file_backup_engines', array( |
||
| 81 | new Zip_File_Backup_Engine, |
||
| 82 | new Zip_Archive_File_Backup_Engine, |
||
| 83 | ) ); |
||
| 84 | |||
| 85 | // Set the file backup engine settings |
||
| 86 | foreach ( $file_backup_engines as &$backup_engine ) { |
||
| 87 | $backup_engine->set_backup_filename( $this->backup_filename ); |
||
| 88 | $backup_engine->set_excludes( new Excludes( array( '*.zip', 'index.html', '.htaccess', '.*-running', '.files' ) ) ); |
||
| 89 | } |
||
| 90 | |||
| 91 | // Zip up the database dump |
||
| 92 | $root = Path::get_root(); |
||
| 93 | Path::get_instance()->set_root( Path::get_path() ); |
||
| 94 | $file_backup = $this->perform_backup( $file_backup_engines ); |
||
| 95 | Path::get_instance()->set_root( $root ); |
||
| 96 | |||
| 97 | if ( is_a( $file_backup, __NAMESPACE__ . '\\Backup_Engine' ) ) { |
||
| 98 | $this->backup_filepath = $file_backup->get_backup_filepath(); |
||
| 99 | } |
||
| 100 | |||
| 101 | // Delete the Database Backup now that we've zipped it up |
||
| 102 | if ( file_exists( $this->database_dump_filepath ) ) { |
||
| 103 | unlink( $this->database_dump_filepath ); |
||
| 104 | } |
||
| 105 | |||
| 106 | } |
||
| 107 | |||
| 108 | public function backup_files() { |
||
| 109 | |||
| 110 | if ( $this->status ) { |
||
| 111 | $this->status->set_status( __( 'Backing up files...', 'backupwordpress' ) ); |
||
| 112 | } |
||
| 113 | |||
| 114 | // Fire up the file backup engines |
||
| 115 | $backup_engines = apply_filters( 'hmbkp_file_backup_engines', array( |
||
| 116 | new Zip_File_Backup_Engine, |
||
| 117 | new Zip_Archive_File_Backup_Engine, |
||
| 118 | ) ); |
||
| 119 | |||
| 120 | // Set the file backup engine settings |
||
| 121 | foreach ( $backup_engines as &$backup_engine ) { |
||
| 122 | $backup_engine->set_backup_filename( $this->backup_filename ); |
||
| 123 | if ( $this->excludes ) { |
||
| 124 | $backup_engine->set_excludes( $this->excludes ); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | $file_backup = $this->perform_backup( $backup_engines ); |
||
| 129 | |||
| 130 | if ( is_a( $file_backup, __NAMESPACE__ . '\\Backup_Engine' ) ) { |
||
| 131 | $this->backup_filepath = $file_backup->get_backup_filepath(); |
||
| 132 | } |
||
| 133 | |||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Perform the backup by iterating through each Backup_Engine in turn until |
||
| 138 | * we find one which works. If a backup filename or any excludes have been |
||
| 139 | * set then those are passed to each Backup_Engine. |
||
| 140 | */ |
||
| 141 | public function perform_backup( array $backup_engines ) { |
||
|
|
|||
| 142 | |||
| 143 | foreach ( $backup_engines as $backup_engine ) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * If the backup_engine completes the backup then we |
||
| 147 | * clear any errors or warnings from previously failed backup_engines |
||
| 148 | * and return the successful one. |
||
| 149 | */ |
||
| 150 | if ( $backup_engine->backup() ) { |
||
| 151 | $this->errors = array(); |
||
| 152 | $this->warnings = $backup_engine->get_warnings(); |
||
| 153 | return $backup_engine; |
||
| 154 | } |
||
| 155 | |||
| 156 | // Store all the errors and warnings as they are shown if all engines fail |
||
| 157 | $this->warnings = array_merge( $this->warnings, $backup_engine->get_warnings() ); |
||
| 158 | $this->errors = array_merge( $this->errors, $backup_engine->get_errors() ); |
||
| 159 | |||
| 160 | } |
||
| 161 | |||
| 162 | return false; |
||
| 163 | |||
| 164 | } |
||
| 165 | |||
| 166 | public function get_warnings() { |
||
| 169 | |||
| 170 | public function get_errors() { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Add an warning to the errors warnings. |
||
| 176 | * |
||
| 177 | * A warning is always treat as non-fatal and should only be used for recoverable |
||
| 178 | * issues with the backup process. |
||
| 179 | * |
||
| 180 | * @param string $context The context for the warning. |
||
| 181 | * @param string $error The warning that was encountered. |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function warning( $context, $warning ) { |
|
| 184 | |||
| 185 | if ( empty( $context ) || empty( $warning ) ) { |
||
| 186 | return; |
||
| 187 | } |
||
| 188 | |||
| 189 | // Ensure we don't store duplicate warnings by md5'ing the error as the key |
||
| 190 | $this->warnings[ $context ][ md5( implode( ':', (array) $warning ) ) ] = $warning; |
||
| 191 | |||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Back compat with old error method |
||
| 196 | * |
||
| 197 | * Only the backup engines themselves can fire fatal errors |
||
| 198 | * |
||
| 199 | * @deprecated 3.4 Backup->warning( $context, $warning ) |
||
| 200 | */ |
||
| 201 | public function error( $context, $message ) { |
||
| 202 | _deprecated_function( __FUNCTION__, '3.4', 'Backup->warning( $context, $warning )' ); |
||
| 203 | $this->warning( $context, $message ); |
||
| 204 | } |
||
| 205 | |||
| 206 | public function get_database_backup_filepath() { |
||
| 209 | |||
| 210 | public function get_backup_filepath() { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Back compat with old method name |
||
| 216 | * |
||
| 217 | * @see Backup::get_backup_filepath() |
||
| 218 | * @deprecated 3.4 Use Backup::get_backup_filepath() |
||
| 219 | */ |
||
| 220 | public function get_archive_filepath() { |
||
| 224 | } |
||
| 225 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.