Complex classes like WC_Download_Handler 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 WC_Download_Handler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class WC_Download_Handler { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Hook in methods. |
||
| 22 | */ |
||
| 23 | public static function init() { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Check if we need to download a file and check validity. |
||
| 34 | */ |
||
| 35 | public static function download_product() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get a download from the database. |
||
| 59 | * |
||
| 60 | * @param array $args Contains email, order key, product id and download id |
||
| 61 | * @return object |
||
| 62 | * @access private |
||
| 63 | */ |
||
| 64 | private static function get_download_data( $args = array() ) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Perform checks to see if the current user can download the file. |
||
| 83 | * @param object $download_data |
||
| 84 | * @access private |
||
| 85 | */ |
||
| 86 | private static function check_current_user_can_download( $download_data ) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Check if an order is valid for downloading from. |
||
| 95 | * @param array $download_data |
||
| 96 | * @access private |
||
| 97 | */ |
||
| 98 | private static function check_order_is_valid( $download_data ) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Check if there are downloads remaining. |
||
| 106 | * @param array $download_data |
||
| 107 | * @access private |
||
| 108 | */ |
||
| 109 | private static function check_downloads_remaining( $download_data ) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Check if the download has expired. |
||
| 117 | * @param array $download_data |
||
| 118 | * @access private |
||
| 119 | */ |
||
| 120 | private static function check_download_expiry( $download_data ) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Check if a download requires the user to login first. |
||
| 128 | * @param array $download_data |
||
| 129 | * @access private |
||
| 130 | */ |
||
| 131 | private static function check_download_login_required( $download_data ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Log the download + increase counts. |
||
| 148 | * @param object $download_data |
||
| 149 | */ |
||
| 150 | public static function count_download( $download_data ) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Download a file - hook into init function. |
||
| 169 | * @param string $file_path URL to file |
||
| 170 | * @param integer $product_id of the product being downloaded |
||
| 171 | */ |
||
| 172 | public static function download( $file_path, $product_id ) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Redirect to a file to start the download. |
||
| 195 | * @param string $file_path |
||
| 196 | * @param string $filename |
||
| 197 | */ |
||
| 198 | public static function download_file_redirect( $file_path, $filename = '' ) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Parse file path and see if its remote or local. |
||
| 205 | * @param string $file_path |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | public static function parse_file_path( $file_path ) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Download a file using X-Sendfile, X-Lighttpd-Sendfile, or X-Accel-Redirect if available. |
||
| 245 | * @param string $file_path |
||
| 246 | * @param string $filename |
||
| 247 | */ |
||
| 248 | public static function download_file_xsendfile( $file_path, $filename ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Force download - this is the default method. |
||
| 272 | * @param string $file_path |
||
| 273 | * @param string $filename |
||
| 274 | */ |
||
| 275 | public static function download_file_force( $file_path, $filename ) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get content type of a download. |
||
| 293 | * @param string $file_path |
||
| 294 | * @return string |
||
| 295 | * @access private |
||
| 296 | */ |
||
| 297 | private static function get_download_content_type( $file_path ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Set headers for the download. |
||
| 314 | * @param string $file_path |
||
| 315 | * @param string $filename |
||
| 316 | * @access private |
||
| 317 | */ |
||
| 318 | private static function download_headers( $file_path, $filename ) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Check and set certain server config variables to ensure downloads work as intended. |
||
| 336 | */ |
||
| 337 | private static function check_server_config() { |
||
| 338 | wc_set_time_limit( 0 ); |
||
| 339 | if ( function_exists( 'get_magic_quotes_runtime' ) && get_magic_quotes_runtime() && version_compare( phpversion(), '5.4', '<' ) ) { |
||
| 340 | set_magic_quotes_runtime( 0 ); |
||
| 341 | } |
||
| 342 | if ( function_exists( 'apache_setenv' ) ) { |
||
| 343 | @apache_setenv( 'no-gzip', 1 ); |
||
| 344 | } |
||
| 345 | @ini_set( 'zlib.output_compression', 'Off' ); |
||
| 346 | @session_write_close(); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Clean all output buffers. |
||
| 351 | * |
||
| 352 | * Can prevent errors, for example: transfer closed with 3 bytes remaining to read. |
||
| 353 | * |
||
| 354 | * @access private |
||
| 355 | */ |
||
| 356 | private static function clean_buffers() { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * readfile_chunked. |
||
| 369 | * |
||
| 370 | * Reads file in chunks so big downloads are possible without changing PHP.INI - http://codeigniter.com/wiki/Download_helper_for_large_files/. |
||
| 371 | * |
||
| 372 | * @param string $file |
||
| 373 | * @return bool Success or fail |
||
| 374 | */ |
||
| 375 | public static function readfile_chunked( $file ) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Filter headers for IE to fix issues over SSL. |
||
| 397 | * |
||
| 398 | * IE bug prevents download via SSL when Cache Control and Pragma no-cache headers set. |
||
| 399 | * |
||
| 400 | * @param array $headers |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | public static function ie_nocache_headers_fix( $headers ) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Die with an error message if the download fails. |
||
| 413 | * @param string $message |
||
| 414 | * @param string $title |
||
| 415 | * @param integer $status |
||
| 416 | * @access private |
||
| 417 | */ |
||
| 418 | private static function download_error( $message, $title = '', $status = 404 ) { |
||
| 424 | } |
||
| 425 | |||
| 427 |
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: