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 ) { |
||
18 | $this->backup_filename = $backup_filename; |
||
19 | $this->database_dump_filename = $database_dump_filename; |
||
20 | } |
||
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() { |
||
53 | |||
54 | public function backup_database() { |
||
107 | |||
108 | public function backup_files() { |
||
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 ) { |
||
156 | |||
157 | public function get_warnings() { |
||
160 | |||
161 | public function get_errors() { |
||
164 | |||
165 | /** |
||
166 | * Add an warning to the errors warnings. |
||
167 | * |
||
168 | * A warning is always treat as non-fatal and should only be used for recoverable |
||
169 | * issues with the backup process. |
||
170 | * |
||
171 | * @param string $context The context for the warning. |
||
172 | * @param string $error The warning that was encountered. |
||
173 | */ |
||
174 | View Code Duplication | public function warning( $context, $warning ) { |
|
184 | |||
185 | /** |
||
186 | * Back compat with old error mathod |
||
187 | * |
||
188 | * @deprecated 3.4 Backup->warning( $context, $warning ) |
||
189 | */ |
||
190 | public function error( $context, $message ) { |
||
194 | |||
195 | public function get_database_backup_filepath() { |
||
198 | |||
199 | public function get_backup_filepath() { |
||
202 | |||
203 | /** |
||
204 | * Back compat with old method name |
||
205 | * |
||
206 | * @see Backup::get_backup_filepath() |
||
207 | * @deprecated 3.4 Use Backup::get_backup_filepath() |
||
208 | */ |
||
209 | public function get_archive_filepath() { |
||
213 | |||
214 | } |
||
215 |
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
@return
annotation as described here.