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:
Complex classes like EEH_File 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 EEH_File, and based on these observations, apply Extract Interface, too.
| 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
||
| 14 | class EEH_File extends EEH_Base { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string $_credentials_form |
||
| 18 | */ |
||
| 19 | private static $_credentials_form; |
||
| 20 | |||
| 21 | protected static $_wp_filesystem_direct; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param string|null $filepath the filepath we want to work in. If its in the |
||
| 25 | * wp uploads directory, we'll want to just use the filesystem directly. |
||
| 26 | * If not provided, we have to assume its not in the uploads directory |
||
| 27 | * @throws EE_Error |
||
| 28 | * @return WP_Filesystem_Base |
||
| 29 | */ |
||
| 30 | private static function _get_wp_filesystem( $filepath = null) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * display_request_filesystem_credentials_form |
||
| 97 | */ |
||
| 98 | public static function display_request_filesystem_credentials_form() { |
||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * verify_filepath_and_permissions |
||
| 108 | * checks that a file is readable and has sufficient file permissions set to access |
||
| 109 | * |
||
| 110 | * @access public |
||
| 111 | * @param string $full_file_path - full server path to the folder or file |
||
| 112 | * @param string $file_name - name of file if checking a file |
||
| 113 | * @param string $file_ext - file extension (ie: "php") if checking a file |
||
| 114 | * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
||
| 115 | * @throws EE_Error |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | public static function verify_filepath_and_permissions( $full_file_path = '', $file_name = '', $file_ext = '', $type_of_file = '' ) { |
||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | /** |
||
| 150 | * _permissions_error_for_unreadable_filepath - attempts to determine why permissions are set incorrectly for a file or folder |
||
| 151 | * |
||
| 152 | * @access private |
||
| 153 | * @param string $full_file_path - full server path to the folder or file |
||
| 154 | * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | private static function _permissions_error_for_unreadable_filepath( $full_file_path = '', $type_of_file = '' ){ |
||
| 179 | |||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * ensure_folder_exists_and_is_writable |
||
| 184 | * ensures that a folder exists and is writable, will attempt to create folder if it does not exist |
||
| 185 | * Also ensures all the parent folders exist, and if not tries to create them. |
||
| 186 | * Also, if this function creates the folder, adds a .htaccess file and index.html file |
||
| 187 | * @param string $folder |
||
| 188 | * @throws EE_Error if the folder exists and is writeable, but for some reason we |
||
| 189 | * can't write to it |
||
| 190 | * @return bool false if folder isn't writable; true if it exists and is writeable, |
||
| 191 | */ |
||
| 192 | public static function ensure_folder_exists_and_is_writable( $folder = '' ){ |
||
| 225 | |||
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * verify_is_writable - checks if a file or folder is writable |
||
| 230 | * @param string $full_path - full server path to file or folder |
||
| 231 | * @param string $file_or_folder - whether checking a file or folder |
||
| 232 | * @throws EE_Error |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | public static function verify_is_writable( $full_path = '', $file_or_folder = 'folder' ){ |
||
| 249 | |||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * ensure_file_exists_and_is_writable |
||
| 254 | * ensures that a file exists and is writable, will attempt to create file if it does not exist. |
||
| 255 | * Also ensures all the parent folders exist, and if not tries to create them. |
||
| 256 | * @param string $full_file_path |
||
| 257 | * @throws EE_Error |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | public static function ensure_file_exists_and_is_writable( $full_file_path = '' ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Gets the parent folder. If provided with file, gets the folder that contains it. |
||
| 286 | * If provided a folder, gets its parent folder. |
||
| 287 | * @param string $file_or_folder_path |
||
| 288 | * @return string parent folder, ENDING with a directory separator |
||
| 289 | */ |
||
| 290 | public static function get_parent_folder( $file_or_folder_path ) { |
||
| 297 | |||
| 298 | public static function ensure_folder_exists_recursively( $folder ) { |
||
| 301 | |||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * get_file_contents |
||
| 306 | * @param string $full_file_path |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public static function get_file_contents( $full_file_path = '' ){ |
||
| 318 | |||
| 319 | |||
| 320 | |||
| 321 | /** |
||
| 322 | * write_file |
||
| 323 | * @param string $full_file_path |
||
| 324 | * @param string $file_contents - the content to be written to the file |
||
| 325 | * @param string $file_type |
||
| 326 | * @throws EE_Error |
||
| 327 | * @return bool |
||
| 328 | */ |
||
| 329 | public static function write_to_file( $full_file_path = '', $file_contents = '', $file_type = '' ){ |
||
| 354 | |||
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * exists |
||
| 359 | * checks if a file exists using the WP filesystem |
||
| 360 | * |
||
| 361 | * @param string $full_file_path |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | public static function exists( $full_file_path = '' ) { |
||
| 368 | |||
| 369 | |||
| 370 | |||
| 371 | /** |
||
| 372 | * is_readable |
||
| 373 | * checks if a file is_readable using the WP filesystem |
||
| 374 | * |
||
| 375 | * @param string $full_file_path |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | public static function is_readable( $full_file_path = '' ) { |
||
| 386 | |||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * remove_filename_from_filepath |
||
| 391 | * given a full path to a file including the filename itself, this removes the filename and returns the path, up to, but NOT including the filename OR slash |
||
| 392 | * |
||
| 393 | * @param string $full_file_path |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | public static function remove_filename_from_filepath( $full_file_path = '' ) { |
||
| 399 | |||
| 400 | |||
| 401 | /** |
||
| 402 | * get_filename_from_filepath. Arguably the same as basename() |
||
| 403 | * |
||
| 404 | * @param string $full_file_path |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | public static function get_filename_from_filepath( $full_file_path = '' ) { |
||
| 410 | |||
| 411 | |||
| 412 | /** |
||
| 413 | * get_file_extension |
||
| 414 | * |
||
| 415 | * @param string $full_file_path |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | public static function get_file_extension( $full_file_path = '' ) { |
||
| 421 | |||
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * add_htaccess_deny_from_all and an index.html file |
||
| 426 | * in order to prevent folks from exploring filesystem at their leisure |
||
| 427 | * @param string $folder |
||
| 428 | * @return bool |
||
| 429 | */ |
||
| 430 | public static function add_htaccess_deny_from_all( $folder = '' ) { |
||
| 444 | |||
| 445 | |||
| 446 | |||
| 447 | /** |
||
| 448 | * Given that the file in $file_path has the normal name, (ie, CLASSNAME.whatever.php), |
||
| 449 | * extract that classname. |
||
| 450 | * @param string $file_path |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | public static function get_classname_from_filepath_with_standard_filename( $file_path ){ |
||
| 460 | |||
| 461 | |||
| 462 | |||
| 463 | /** |
||
| 464 | * standardise_directory_separators |
||
| 465 | * convert all directory separators in a file path to whatever is set for DS |
||
| 466 | * @param string $file_path |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public static function standardise_directory_separators( $file_path ){ |
||
| 472 | |||
| 473 | |||
| 474 | |||
| 475 | /** |
||
| 476 | * end_with_directory_separator |
||
| 477 | * ensures that file path ends with DS |
||
| 478 | * @param string $file_path |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public static function end_with_directory_separator( $file_path ){ |
||
| 484 | |||
| 485 | |||
| 486 | |||
| 487 | /** |
||
| 488 | * shorthand for both EEH_FIle::end_with_directory_separator AND EEH_File::standardise_directory_separators |
||
| 489 | * @param $file_path |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | public static function standardise_and_end_with_directory_separator( $file_path ){ |
||
| 495 | |||
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * takes the folder name (with or without trailing slash) and finds the files it in, |
||
| 500 | * and what the class's name inside of each should be. |
||
| 501 | * @param array $folder_paths |
||
| 502 | * @param boolean $index_numerically if TRUE, the returned array will be indexed numerically; |
||
| 503 | * if FALSE (Default), returned array will be indexed by the filenames minus extensions. |
||
| 504 | * Set it TRUE if you know there are files in the directory with the same name but different extensions |
||
| 505 | * @throws \EE_Error |
||
| 506 | * @return array if $index_numerically == TRUE keys are numeric , |
||
| 507 | * if $index_numerically == FALSE (Default) keys are what the class names SHOULD be; |
||
| 508 | * and values are their filepaths |
||
| 509 | */ |
||
| 510 | public static function get_contents_of_folders( $folder_paths = array(), $index_numerically = FALSE ){ |
||
| 533 | |||
| 534 | |||
| 535 | |||
| 536 | /** |
||
| 537 | * Copies a file. Mostly a wrapper of WP_Filesystem::copy |
||
| 538 | * @param string $source_file |
||
| 539 | * @param string $destination_file |
||
| 540 | * @param boolean $overwrite |
||
| 541 | * @return boolean success |
||
| 542 | * @throws EE_Error |
||
| 543 | */ |
||
| 544 | public static function copy( $source_file, $destination_file, $overwrite = FALSE ){ |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Reports whether or not the filepath is in the EE uploads folder or not |
||
| 585 | * @param string $filepath |
||
| 586 | * @return boolean |
||
| 587 | */ |
||
| 588 | public static function is_in_uploads_folder( $filepath ) { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Given a "local" filepath (what you probably thought was the only filepath), |
||
| 595 | * converts it into a "remote" filepath (the filepath the currently-in-use |
||
| 596 | * $wp_filesystem needs to use access the folder or file). |
||
| 597 | * See http://wordpress.stackexchange.com/questions/124900/using-wp-filesystem-in-plugins |
||
| 598 | * @param WP_Filesystem_Base $wp_filesystem we aren't initially sure which one |
||
| 599 | * is in use, so you need to provide it |
||
| 600 | * @param string $local_filepath the filepath to the folder/file locally |
||
| 601 | * @return string the remote filepath (eg the filepath the filesystem method, eg |
||
| 602 | * ftp or ssh, will use to access the folder |
||
| 603 | */ |
||
| 604 | public static function convert_local_filepath_to_remote_filepath( $local_filepath ) { |
||
| 608 | } |
||
| 609 | // End of file EEH_File.helper.php |
||
| 610 | // Location: /helpers/EEH_File.helper.php |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: