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 |
||
| 13 | abstract class Backup_Engine { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * An array of backup errors. |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | private $errors = array(); |
||
| 21 | |||
| 22 | /** |
||
| 23 | * An array of backup warnings. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private $warnings = array(); |
||
| 28 | |||
| 29 | public function __construct() { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Backup Engine Types should always implement the `verify_backup` method. |
||
| 47 | * |
||
| 48 | * @return bool Whether the backup completed successfully or not. |
||
| 49 | */ |
||
| 50 | abstract public function verify_backup(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the full filepath to the backup file. |
||
| 54 | * |
||
| 55 | * @return string The backup filepath. |
||
| 56 | */ |
||
| 57 | public function get_backup_filepath() { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get the filename of the backup. |
||
| 63 | * |
||
| 64 | * @return string The backup filename. |
||
| 65 | */ |
||
| 66 | public function get_backup_filename() { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Set the filename of the backup. |
||
| 72 | * |
||
| 73 | * @param string $filename The backup filename. |
||
| 74 | */ |
||
| 75 | public function set_backup_filename( $filename ) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get the array of errors encountered during the backup process. |
||
| 81 | * |
||
| 82 | * @param string $context The context for the error, usually the Backup |
||
| 83 | * Engine that encountered the error. |
||
| 84 | * |
||
| 85 | * @return array The array of errors. |
||
| 86 | */ |
||
| 87 | View Code Duplication | public function get_errors( $context = null ) { |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Add an error to the errors array. |
||
| 100 | * |
||
| 101 | * An error is always treat as fatal and should only be used for unrecoverable |
||
| 102 | * issues with the backup process. |
||
| 103 | * |
||
| 104 | * @param string $context The context for the error. |
||
| 105 | * @param string $error The error that was encountered. |
||
| 106 | */ |
||
| 107 | View Code Duplication | public function error( $context, $error ) { |
|
|
1 ignored issue
–
show
|
|||
| 108 | |||
| 109 | if ( empty( $context ) || empty( $error ) ) { |
||
| 110 | return; |
||
| 111 | } |
||
| 112 | |||
| 113 | // Ensure we don't store duplicate errors by md5'ing the error as the key |
||
| 114 | $this->errors[ $context ][ $_key = md5( implode( ':', (array) $error ) ) ] = $error; |
||
| 115 | |||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get the array of warnings encountered during the backup process. |
||
| 120 | * |
||
| 121 | * @param string $context The context for the warning, usually the Backup |
||
| 122 | * Engine that encountered the warning. |
||
| 123 | * |
||
| 124 | * @return array The array of warnings. |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function get_warnings( $context = null ) { |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Add an warning to the errors warnings. |
||
| 139 | * |
||
| 140 | * A warning is always treat as non-fatal and should only be used for recoverable |
||
| 141 | * issues with the backup process. |
||
| 142 | * |
||
| 143 | * @param string $context The context for the warning. |
||
| 144 | * @param string $error The warning that was encountered. |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function warning( $context, $warning ) { |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Hooked into `set_error_handler` to catch any PHP errors that happen during |
||
| 159 | * the backup process. |
||
| 160 | * |
||
| 161 | * PHP errors are always treat as warnings rather than errors. |
||
| 162 | * |
||
| 163 | * @param int $type The level of error raised |
||
| 164 | * |
||
| 165 | * @return false Return false to pass the error back to PHP so it can |
||
| 166 | * be handled natively. |
||
| 167 | */ |
||
| 168 | public function error_handler( $type ) { |
||
| 196 | |||
| 197 | } |
||
| 198 |
If you suppress an error, we recommend checking for the error condition explicitly: