Complex classes like Scheduled_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 Scheduled_Backup, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Scheduled_Backup { |
||
14 | |||
15 | /** |
||
16 | * The unique schedule id |
||
17 | * |
||
18 | * @var string |
||
19 | * @access private |
||
20 | */ |
||
21 | private $id = ''; |
||
22 | |||
23 | /** |
||
24 | * The slugified version of the schedule name |
||
25 | * |
||
26 | * @var string |
||
27 | * @access private |
||
28 | */ |
||
29 | private $slug = ''; |
||
30 | |||
31 | /** |
||
32 | * The raw schedule options from the database |
||
33 | * |
||
34 | * @var array |
||
35 | * @access private |
||
36 | */ |
||
37 | private $options = array( |
||
38 | 'max_backups' => 3, |
||
39 | 'excludes' => array(), |
||
40 | 'type' => 'complete', |
||
41 | 'reoccurrence' => 'manually' |
||
42 | ); |
||
43 | |||
44 | /** |
||
45 | * Setup the schedule object |
||
46 | * Loads the options from the database and populates properties |
||
47 | * |
||
48 | * @param string $id |
||
49 | * |
||
50 | * @throws Exception |
||
51 | */ |
||
52 | |||
53 | public function __construct( $id ) { |
||
95 | |||
96 | /** |
||
97 | * Get the id for this schedule |
||
98 | */ |
||
99 | public function get_id() { |
||
102 | |||
103 | /** |
||
104 | * Get a slugified version of name |
||
105 | */ |
||
106 | public function get_slug() { |
||
116 | |||
117 | /** |
||
118 | * Returns the given option value |
||
119 | * |
||
120 | * @param $option_name |
||
121 | * @return mixed The option value |
||
122 | */ |
||
123 | public function get_schedule_option( $option_name ) { |
||
128 | |||
129 | /** |
||
130 | * Get the name of this backup schedule |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | public function get_name() { |
||
137 | |||
138 | /** |
||
139 | * Get the type of backup |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | public function get_type() { |
||
146 | |||
147 | /** |
||
148 | * Set the type of backup |
||
149 | * |
||
150 | * @param string $type |
||
151 | */ |
||
152 | public function set_type( $type ) { |
||
157 | |||
158 | /** |
||
159 | * Get the exclude rules |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | public function get_excludes() { |
||
166 | |||
167 | /** |
||
168 | * Set the exclude rules |
||
169 | * |
||
170 | * @param mixed $excludes A comma separated list or array of exclude rules |
||
171 | * @param bool $append Whether to replace or append to existing rules |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | public function set_excludes( $exclude_rules, $append = false ) { |
||
187 | |||
188 | /** |
||
189 | * Get the maximum number of backups to keep |
||
190 | * |
||
191 | * @return int |
||
192 | */ |
||
193 | public function get_max_backups() { |
||
196 | |||
197 | /** |
||
198 | * Set the maximum number of backups to keep |
||
199 | * |
||
200 | * @param int $max |
||
201 | * |
||
202 | * @return WP_Error|boolean |
||
203 | */ |
||
204 | public function set_max_backups( $max ) { |
||
207 | |||
208 | public function get_status() { |
||
211 | |||
212 | /** |
||
213 | * Get the array of services options for this schedule |
||
214 | * |
||
215 | * @param $service |
||
216 | * @param null $option |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | public function get_service_options( $service, $option = null ) { |
||
239 | |||
240 | /** |
||
241 | * Set the service options for this schedule |
||
242 | * |
||
243 | * @param $service |
||
244 | * @param array $options |
||
245 | */ |
||
246 | public function set_service_options( $service, Array $options ) { |
||
249 | |||
250 | /** |
||
251 | * Get the start time for the schedule |
||
252 | * |
||
253 | * @return int timestamp || 0 for manual only schedules |
||
254 | */ |
||
255 | public function get_schedule_start_time( $gmt = true ) { |
||
276 | |||
277 | /** |
||
278 | * Set the schedule start time. |
||
279 | * |
||
280 | * @param timestamp $time |
||
281 | */ |
||
282 | public function set_schedule_start_time( $time ) { |
||
289 | |||
290 | /** |
||
291 | * Get the schedule reoccurrence |
||
292 | * |
||
293 | */ |
||
294 | public function get_reoccurrence() { |
||
297 | |||
298 | /** |
||
299 | * Set the schedule reoccurrence |
||
300 | * |
||
301 | * @param string $reoccurrence |
||
302 | * |
||
303 | * @return \WP_Error|null|boolean |
||
304 | */ |
||
305 | public function set_reoccurrence( $reoccurrence ) { |
||
332 | |||
333 | /** |
||
334 | * Get the interval between backups |
||
335 | * |
||
336 | * @return int |
||
337 | */ |
||
338 | public function get_interval() { |
||
349 | |||
350 | /** |
||
351 | * Get the next occurrence of this scheduled backup |
||
352 | * |
||
353 | */ |
||
354 | public function get_next_occurrence( $gmt = true ) { |
||
369 | |||
370 | public function is_cron_scheduled() { |
||
373 | |||
374 | /** |
||
375 | * Schedule the backup cron |
||
376 | * |
||
377 | */ |
||
378 | public function schedule() { |
||
388 | |||
389 | |||
390 | /** |
||
391 | * Unschedule the backup cron. |
||
392 | * |
||
393 | * @return void |
||
394 | */ |
||
395 | public function unschedule() { |
||
398 | |||
399 | /** |
||
400 | * Run the backup |
||
401 | * |
||
402 | */ |
||
403 | public function run() { |
||
446 | |||
447 | public function get_backup_filename() { |
||
455 | |||
456 | public function get_database_dump_filename() { |
||
459 | |||
460 | /** |
||
461 | * Hook into the actions fired in the Backup class and set the status |
||
462 | * |
||
463 | * @param $action |
||
464 | */ |
||
465 | public function do_action( $action, Site_Backup $backup ) { |
||
477 | |||
478 | /** |
||
479 | * Calculate schedule run time. |
||
480 | * |
||
481 | * @param int Timestamp $end |
||
482 | */ |
||
483 | public function update_average_schedule_run_time( $start, $end ) { |
||
506 | |||
507 | /** |
||
508 | * Calculates the average run time for this schedule. |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | public function get_schedule_average_duration() { |
||
550 | |||
551 | /** |
||
552 | * Get the backups created by this schedule |
||
553 | * |
||
554 | * @todo look into using recursiveDirectoryIterator and recursiveRegexIterator |
||
555 | * @return string[] - file paths of the backups |
||
556 | */ |
||
557 | public function get_backups() { |
||
580 | |||
581 | /** |
||
582 | * Delete old backups |
||
583 | * |
||
584 | * @access private |
||
585 | */ |
||
586 | public function delete_old_backups() { |
||
595 | |||
596 | /** |
||
597 | * Delete a specific back up file created by this schedule |
||
598 | * |
||
599 | * @param string $filepath |
||
600 | * |
||
601 | * @return \WP_Error|boolean |
||
602 | */ |
||
603 | public function delete_backup( $filepath ) { |
||
625 | |||
626 | /** |
||
627 | * Delete all back up files created by this schedule |
||
628 | * |
||
629 | */ |
||
630 | public function delete_backups() { |
||
633 | |||
634 | /** |
||
635 | * Save the schedules options. |
||
636 | * |
||
637 | */ |
||
638 | public function save() { |
||
646 | |||
647 | /** |
||
648 | * Cancel this schedule |
||
649 | * |
||
650 | * Cancels the cron job, removes the schedules options |
||
651 | * and optionally deletes all backups created by |
||
652 | * this schedule. |
||
653 | * |
||
654 | */ |
||
655 | public function cancel( $delete_backups = false ) { |
||
669 | |||
670 | } |
||
671 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: