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 |
||
10 | class Backup_Status { |
||
11 | |||
12 | /** |
||
13 | * The filename for the backup file we are the tracking status of |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | private $filename = ''; |
||
18 | |||
19 | /** |
||
20 | * [$lock_handler description] |
||
21 | * |
||
22 | * @var LockHandler |
||
23 | */ |
||
24 | private $lock_handler = ''; |
||
25 | |||
26 | private $callback; |
||
27 | |||
28 | /** |
||
29 | * @param string $id The unique id for the backup job |
||
30 | */ |
||
31 | public function __construct( $id ) { |
||
32 | $this->id = (string) $id; |
||
|
|||
33 | $this->cleanup_after_crash(); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Start the tracking a backup process. |
||
38 | * |
||
39 | * This creates a backup running file and issues a file lock. This prevents duplicate |
||
40 | * instances of this backup process running concurrently and allows us to detect if |
||
41 | * the PHP thread running the process is killed as that will clear the lock. |
||
42 | * |
||
43 | * @param string $backup_filename The filename for the backup file that we're tracking |
||
44 | * @param string $status_message The initial status for the backup process |
||
45 | * |
||
46 | * @return boolean Whether the backup process was success marked as started |
||
47 | */ |
||
48 | public function start( $backup_filename, $status_message ) { |
||
49 | $this->filename = $backup_filename; |
||
50 | |||
51 | add_action( 'shutdown', array( $this, 'catch_fatals' ), 10 ); |
||
52 | |||
53 | $this->lock_handler = new LockHandler( basename( $this->get_status_filepath() ), Path::get_path() ); |
||
54 | |||
55 | if ( ! $this->lock_handler->lock() || $this->get_status() ) { |
||
56 | return false; |
||
57 | } |
||
58 | |||
59 | return $this->set_status( $status_message ); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Mark a backup process as finished. |
||
64 | * |
||
65 | * This removes the file lock and deletes the running file. |
||
66 | */ |
||
67 | public function finish() { |
||
68 | |||
69 | if ( isset( $this->lock_handler ) && is_a( $this->lock_handler, 'LockHandler' ) ) { |
||
70 | $this->lock_handler->release(); |
||
71 | } |
||
72 | |||
73 | // Delete the backup running file |
||
74 | if ( file_exists( $this->get_status_filepath() ) ) { |
||
75 | return unlink( $this->get_status_filepath() ); |
||
76 | } |
||
77 | |||
78 | return false; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Check if the backup has been started by checking if the running file |
||
83 | * exists. |
||
84 | * |
||
85 | * @return boolean Whether the backup process has been started |
||
86 | */ |
||
87 | public function is_started() { |
||
88 | return (bool) file_exists( $this->get_status_filepath() ); |
||
89 | } |
||
90 | |||
91 | public function is_running() { |
||
106 | |||
107 | /** |
||
108 | * If the running file exists but isn't locked then the thread that |
||
109 | * the backup process is running in must have been killed. |
||
110 | * |
||
111 | * You should only be running this command from a separate thread |
||
112 | * |
||
113 | * @return boolean Whether the backup process has crashed or not |
||
114 | */ |
||
115 | public function has_crashed() { |
||
118 | |||
119 | /** |
||
120 | * Handle a process that's previouly crashed. |
||
121 | * |
||
122 | * Delete the partially created backup if it exists and then run the standard |
||
123 | * cleanup tasks and set an error message for the user. |
||
124 | * |
||
125 | * @return bool Whether the crash was handled or not |
||
126 | */ |
||
127 | public function cleanup_after_crash() { |
||
145 | |||
146 | /** |
||
147 | * Catch fatal errors and react accordingly. |
||
148 | * |
||
149 | * Hooked into the shutdown action. If we've shutdown because of a Fatal error |
||
150 | * then we cleanup and set an error message for the user. |
||
151 | */ |
||
152 | public function catch_fatals() { |
||
174 | |||
175 | /** |
||
176 | * Get the filepath for the backup file we're tracking |
||
177 | * |
||
178 | * @return string The path to the backup file |
||
179 | */ |
||
180 | public function get_backup_filename() { |
||
181 | |||
182 | if ( $this->is_started() ) { |
||
183 | $status = json_decode( file_get_contents( $this->get_status_filepath() ) ); |
||
184 | |||
185 | if ( ! empty( $status->filename ) ) { |
||
186 | $this->filename = $status->filename; |
||
187 | } |
||
188 | } |
||
189 | |||
190 | return $this->filename; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Get the status of the running backup. |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | public function get_status() { |
||
199 | |||
200 | if ( ! file_exists( $this->get_status_filepath() ) ) { |
||
201 | return false; |
||
202 | } |
||
203 | |||
204 | $status = json_decode( file_get_contents( $this->get_status_filepath() ) ); |
||
205 | |||
206 | if ( ! empty( $status->status ) ) { |
||
207 | return $status->status; |
||
208 | } |
||
209 | |||
210 | return false; |
||
211 | |||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Set the status of the running backup |
||
216 | * |
||
217 | * @param string $message |
||
218 | * |
||
219 | * @return null |
||
220 | */ |
||
221 | public function set_status( $message ) { |
||
222 | |||
223 | if ( is_callable( $this->callback ) ) { |
||
224 | call_user_func( $this->callback, $message ); |
||
225 | } |
||
226 | |||
227 | // If start hasn't been called yet then we wont' have a backup filename |
||
228 | if ( ! $this->filename ) { |
||
229 | return false; |
||
230 | } |
||
231 | |||
232 | $status = json_encode( (object) array( |
||
233 | 'filename' => $this->filename, |
||
234 | 'started' => $this->get_start_time(), |
||
235 | 'status' => $message, |
||
236 | ) ); |
||
237 | |||
238 | return (bool) file_put_contents( $this->get_status_filepath(), $status ); |
||
239 | |||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Get the time that the current running backup was started |
||
244 | * |
||
245 | * @return int $timestamp |
||
246 | */ |
||
247 | public function get_start_time() { |
||
248 | |||
249 | if ( ! file_exists( $this->get_status_filepath() ) ) { |
||
250 | return 0; |
||
251 | } |
||
252 | |||
253 | $status = json_decode( file_get_contents( $this->get_status_filepath() ) ); |
||
254 | |||
255 | if ( ! empty( $status->started ) && (int) (string) $status->started === $status->started ) { |
||
256 | return $status->started; |
||
257 | } |
||
258 | |||
259 | return time(); |
||
260 | |||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Get the path to the backup running file that stores the running backup status |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | public function get_status_filepath() { |
||
269 | return Path::get_path() . '/.backup-' . $this->id . '-running'; |
||
270 | } |
||
271 | |||
272 | public function set_status_callback( $callback ) { |
||
275 | } |
||
276 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: