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 ) { |
||
54 | |||
55 | // Verify the schedule id |
||
56 | if ( ! is_string( $id ) || ! trim( $id ) ) { |
||
57 | throw new \Exception( 'Argument 1 for ' . __METHOD__ . ' must be a non-empty string' ); |
||
58 | } |
||
59 | |||
60 | // Store id for later |
||
61 | $this->id = $id; |
||
62 | |||
63 | // Load the options |
||
64 | $this->options = array_merge( $this->options, array_filter( (array) get_option( 'hmbkp_schedule_' . $this->get_id() ) ) ); |
||
65 | |||
66 | if ( defined( 'HMBKP_SCHEDULE_START_TIME' ) && strtotime( 'HMBKP_SCHEDULE_START_TIME' ) ) { |
||
67 | $this->set_schedule_start_time( strtotime( 'HMBKP_SCHEDULE_START_TIME' ) ); |
||
|
|||
68 | } |
||
69 | |||
70 | // Setup the schedule if it isn't set |
||
71 | if ( ( ! $this->is_cron_scheduled() && $this->get_reoccurrence() !== 'manually' ) ) { |
||
72 | $this->schedule(); |
||
73 | } |
||
74 | |||
75 | $this->backup_filename = implode( '-', array( |
||
76 | sanitize_title( str_ireplace( array( |
||
77 | 'http://', |
||
78 | 'https://', |
||
79 | 'www' |
||
80 | ), '', home_url() ) ), |
||
81 | $this->get_id(), |
||
82 | $this->get_type(), |
||
83 | current_time( 'Y-m-d-H-i-s' ) |
||
84 | ) ) . '.zip'; |
||
85 | |||
86 | $this->database_dump_filename = implode( '-', array( |
||
87 | 'database', |
||
88 | sanitize_title( str_ireplace( array( 'http://', 'https://', 'www' ), '', home_url() ) ), |
||
89 | $this->get_id() |
||
90 | ) ) . '.sql'; |
||
91 | |||
92 | $this->status = new Backup_Status( $this->get_id() ); |
||
93 | |||
94 | } |
||
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() { |
||
107 | |||
108 | // We cache slug in $this to save expensive calls to sanitize_title |
||
109 | if ( ! empty( $this->slug ) ) { |
||
110 | return $this->slug; |
||
111 | } |
||
112 | |||
113 | return $this->slug = sanitize_title( $this->get_name() ); |
||
114 | |||
115 | } |
||
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 ) { |
||
124 | if ( isset( $this->options[ $option_name ] ) ) { |
||
125 | return $this->options[ $option_name ]; |
||
126 | } |
||
127 | } |
||
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 ) { |
||
153 | if ( ! isset( $this->options['type'] ) || $this->options['type'] !== $type ) { |
||
154 | $this->options['type'] = $type; |
||
155 | } |
||
156 | } |
||
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 ) { |
||
176 | |||
177 | // Normalize the exclude rules before we save them |
||
178 | $excludes = new Excludes; |
||
179 | $excludes = $excludes->normalize( (array) $exclude_rules ); |
||
180 | |||
181 | // If these are valid excludes and they are different save them |
||
182 | if ( empty( $this->options['excludes'] ) || $this->options['excludes'] !== $excludes ) { |
||
183 | $this->options['excludes'] = $append && ! empty( $this->options['excludes'] ) ? array_merge( (array) $this->options['excludes'], (array) $excludes ) : (array) $excludes; |
||
184 | } |
||
185 | |||
186 | } |
||
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 | * Back compat with old set_status mathod |
||
214 | * |
||
215 | * @deprecated 3.4 Backup->status->set_status() |
||
216 | */ |
||
217 | public function set_status( $message ) { |
||
221 | |||
222 | /** |
||
223 | * Get the array of services options for this schedule |
||
224 | * |
||
225 | * @param $service |
||
226 | * @param null $option |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | public function get_service_options( $service, $option = null ) { |
||
249 | |||
250 | /** |
||
251 | * Set the service options for this schedule |
||
252 | * |
||
253 | * @param $service |
||
254 | * @param array $options |
||
255 | */ |
||
256 | public function set_service_options( $service, Array $options ) { |
||
259 | |||
260 | /** |
||
261 | * Get the start time for the schedule |
||
262 | * |
||
263 | * @return int timestamp || 0 for manual only schedules |
||
264 | */ |
||
265 | public function get_schedule_start_time( $gmt = true ) { |
||
286 | |||
287 | /** |
||
288 | * Set the schedule start time. |
||
289 | * |
||
290 | * @param timestamp $time |
||
291 | */ |
||
292 | public function set_schedule_start_time( $time ) { |
||
299 | |||
300 | /** |
||
301 | * Get the schedule reoccurrence |
||
302 | * |
||
303 | */ |
||
304 | public function get_reoccurrence() { |
||
307 | |||
308 | /** |
||
309 | * Set the schedule reoccurrence |
||
310 | * |
||
311 | * @param string $reoccurrence |
||
312 | * |
||
313 | * @return \WP_Error|null|boolean |
||
314 | */ |
||
315 | public function set_reoccurrence( $reoccurrence ) { |
||
342 | |||
343 | /** |
||
344 | * Get the interval between backups |
||
345 | * |
||
346 | * @return int |
||
347 | */ |
||
348 | public function get_interval() { |
||
359 | |||
360 | /** |
||
361 | * Get the next occurrence of this scheduled backup |
||
362 | * |
||
363 | */ |
||
364 | public function get_next_occurrence( $gmt = true ) { |
||
379 | |||
380 | public function is_cron_scheduled() { |
||
383 | |||
384 | /** |
||
385 | * Schedule the backup cron |
||
386 | * |
||
387 | */ |
||
388 | public function schedule() { |
||
398 | |||
399 | |||
400 | /** |
||
401 | * Unschedule the backup cron. |
||
402 | * |
||
403 | * @return void |
||
404 | */ |
||
405 | public function unschedule() { |
||
408 | |||
409 | /** |
||
410 | * Run the backup |
||
411 | * |
||
412 | */ |
||
413 | public function run() { |
||
456 | |||
457 | public function get_backup_filename() { |
||
465 | |||
466 | public function get_database_dump_filename() { |
||
469 | |||
470 | /** |
||
471 | * Hook into the actions fired in the Backup class and set the status |
||
472 | * |
||
473 | * @param $action |
||
474 | */ |
||
475 | public function do_action( $action, Backup $backup ) { |
||
487 | |||
488 | /** |
||
489 | * Calculate schedule run time. |
||
490 | * |
||
491 | * @param int Timestamp $end |
||
492 | */ |
||
493 | public function update_average_schedule_run_time( $start, $end ) { |
||
516 | |||
517 | /** |
||
518 | * Calculates the average run time for this schedule. |
||
519 | * |
||
520 | * @return string |
||
521 | */ |
||
522 | public function get_schedule_average_duration() { |
||
560 | |||
561 | /** |
||
562 | * Get the backups created by this schedule |
||
563 | * |
||
564 | * @todo look into using recursiveDirectoryIterator and recursiveRegexIterator |
||
565 | * @return string[] - file paths of the backups |
||
566 | */ |
||
567 | public function get_backups() { |
||
590 | |||
591 | /** |
||
592 | * Delete old backups |
||
593 | * |
||
594 | * @access private |
||
595 | */ |
||
596 | public function delete_old_backups() { |
||
605 | |||
606 | /** |
||
607 | * Delete a specific back up file created by this schedule |
||
608 | * |
||
609 | * @param string $filepath |
||
610 | * |
||
611 | * @return \WP_Error|boolean |
||
612 | */ |
||
613 | public function delete_backup( $filepath ) { |
||
635 | |||
636 | /** |
||
637 | * Delete all back up files created by this schedule |
||
638 | * |
||
639 | */ |
||
640 | public function delete_backups() { |
||
643 | |||
644 | /** |
||
645 | * Save the schedules options. |
||
646 | * |
||
647 | */ |
||
648 | public function save() { |
||
656 | |||
657 | /** |
||
658 | * Cancel this schedule |
||
659 | * |
||
660 | * Cancels the cron job, removes the schedules options |
||
661 | * and optionally deletes all backups created by |
||
662 | * this schedule. |
||
663 | * |
||
664 | */ |
||
665 | public function cancel( $delete_backups = false ) { |
||
679 | |||
680 | } |
||
681 |
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: