Complex classes like Helper_Script_Manager 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 Helper_Script_Manager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Helper_Script_Manager { |
||
15 | |||
16 | const TEMP_DIRECTORY = 'jetpack-temp'; |
||
17 | const HELPER_HEADER = "<?php /* Jetpack Backup Helper Script */\n"; |
||
18 | const EXPIRY_TIME = 8 * 3600; // 8 hours |
||
19 | const MAX_FILESIZE = 1024 * 1024; // 1 MiB |
||
20 | |||
21 | const README_LINES = array( |
||
22 | 'These files have been put on your server by Jetpack to assist with backups and restores of your site content. They are cleaned up automatically when we no longer need them.', |
||
23 | 'If you no longer have Jetpack connected to your site, you can delete them manually.', |
||
24 | 'If you have questions or need assistance, please contact Jetpack Support at https://jetpack.com/support/', |
||
25 | 'If you like to build amazing things with WordPress, you should visit automattic.com/jobs and apply to join the fun – mention this file when you apply!;', |
||
26 | ); |
||
27 | |||
28 | const INDEX_FILE = '<?php // Silence is golden'; |
||
29 | |||
30 | /** |
||
31 | * Installs a Helper Script, and returns its filesystem path and access url. |
||
32 | * |
||
33 | * @access public |
||
34 | * @static |
||
35 | * |
||
36 | * @param string $script_body Helper Script file contents. |
||
37 | * @return array|WP_Error Either an array containing the path and url of the helper script, or an error. |
||
38 | */ |
||
39 | public static function install_helper_script( $script_body ) { |
||
89 | |||
90 | /** |
||
91 | * Given a path, verify it looks like a helper script and then delete it if so. |
||
92 | * |
||
93 | * @access public |
||
94 | * @static |
||
95 | * |
||
96 | * @param string $path Path to Helper Script to delete. |
||
97 | * @return boolean True if the file is deleted (or does not exist). |
||
98 | */ |
||
99 | public static function delete_helper_script( $path ) { |
||
111 | |||
112 | /** |
||
113 | * Search for Helper Scripts that are suspiciously old, and clean them out. |
||
114 | * |
||
115 | * @access public |
||
116 | * @static |
||
117 | */ |
||
118 | public static function cleanup_expired_helper_scripts() { |
||
121 | |||
122 | /** |
||
123 | * Search for and delete all Helper Scripts. Used during uninstallation. |
||
124 | * |
||
125 | * @access public |
||
126 | * @static |
||
127 | */ |
||
128 | public static function delete_all_helper_scripts() { |
||
131 | |||
132 | /** |
||
133 | * Search for and delete Helper Scripts. If an $expiry_time is specified, only delete Helper Scripts |
||
134 | * with an mtime older than $expiry_time. Otherwise, delete them all. |
||
135 | * |
||
136 | * @access public |
||
137 | * @static |
||
138 | * |
||
139 | * @param int|null $expiry_time If specified, only delete scripts older than $expiry_time. |
||
140 | */ |
||
141 | public static function cleanup_helper_scripts( $expiry_time = null ) { |
||
161 | |||
162 | /** |
||
163 | * Delete a helper script directory if it's empty |
||
164 | * |
||
165 | * @access public |
||
166 | * @static |
||
167 | * |
||
168 | * @param string $dir Path to Helper Script directory. |
||
169 | * @return boolean True if the directory is deleted |
||
170 | */ |
||
171 | private static function delete_empty_helper_directory( $dir ) { |
||
212 | |||
213 | /** |
||
214 | * Find an appropriate location for a jetpack-temp folder, and create one |
||
215 | * |
||
216 | * @access public |
||
217 | * @static |
||
218 | * |
||
219 | * @return WP_Error|array Array containing the url and path of the temp directory if successful, WP_Error if not. |
||
220 | */ |
||
221 | private static function create_temp_directory() { |
||
247 | |||
248 | /** |
||
249 | * Write out an index.php file and a README file for a new jetpack-temp directory. |
||
250 | * |
||
251 | * @access public |
||
252 | * @static |
||
253 | * |
||
254 | * @param string $dir Path to Helper Script directory. |
||
255 | */ |
||
256 | private static function write_supplementary_temp_files( $dir ) { |
||
263 | |||
264 | /** |
||
265 | * Write a file to the specified location with the specified contents. |
||
266 | * |
||
267 | * @access private |
||
268 | * @static |
||
269 | * |
||
270 | * @param string $file_path Path to write to. |
||
271 | * @param string $contents File contents to write. |
||
272 | * @return boolean True if successfully written. |
||
273 | */ |
||
274 | private static function put_contents( $file_path, $contents ) { |
||
287 | |||
288 | /** |
||
289 | * Checks that a file exists, is readable, and has the expected header. |
||
290 | * |
||
291 | * @access private |
||
292 | * @static |
||
293 | * |
||
294 | * @param string $file_path File to verify. |
||
295 | * @param string $expected_header Header that the file should have. |
||
296 | * @return boolean True if the file exists, is readable, and the header matches. |
||
297 | */ |
||
298 | private static function verify_file_header( $file_path, $expected_header ) { |
||
324 | |||
325 | /** |
||
326 | * Gets an associative array of possible places to install a jetpack-temp directory, along with the URL to access each. |
||
327 | * |
||
328 | * @access private |
||
329 | * @static |
||
330 | * |
||
331 | * @return array Array, with keys specifying the full path of install locations, and values with the equivalent URL. |
||
332 | */ |
||
333 | public static function get_install_locations() { |
||
346 | |||
347 | } |
||
348 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.