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'); |
||
| 29 | class EEH_File extends EEH_Base implements EEHI_File { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string $_credentials_form |
||
| 33 | */ |
||
| 34 | private static $_credentials_form; |
||
| 35 | |||
| 36 | protected static $_wp_filesystem_direct; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param string|null $filepath the filepath we want to work in. If its in the |
||
| 40 | * wp uploads directory, we'll want to just use the filesystem directly. |
||
| 41 | * If not provided, we have to assume its not in the uploads directory |
||
| 42 | * @throws EE_Error |
||
| 43 | * @return WP_Filesystem_Base |
||
| 44 | */ |
||
| 45 | private static function _get_wp_filesystem( $filepath = null) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * display_request_filesystem_credentials_form |
||
| 119 | */ |
||
| 120 | public static function display_request_filesystem_credentials_form() { |
||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * verify_filepath_and_permissions |
||
| 130 | * checks that a file is readable and has sufficient file permissions set to access |
||
| 131 | * |
||
| 132 | * @access public |
||
| 133 | * @param string $full_file_path - full server path to the folder or file |
||
| 134 | * @param string $file_name - name of file if checking a file |
||
| 135 | * @param string $file_ext - file extension (ie: "php") if checking a file |
||
| 136 | * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
||
| 137 | * @throws EE_Error |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | public static function verify_filepath_and_permissions( $full_file_path = '', $file_name = '', $file_ext = '', $type_of_file = '' ) { |
||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * _permissions_error_for_unreadable_filepath - attempts to determine why permissions are set incorrectly for a file or folder |
||
| 173 | * |
||
| 174 | * @access private |
||
| 175 | * @param string $full_file_path - full server path to the folder or file |
||
| 176 | * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | private static function _permissions_error_for_unreadable_filepath( $full_file_path = '', $type_of_file = '' ){ |
||
| 201 | |||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * ensure_folder_exists_and_is_writable |
||
| 206 | * ensures that a folder exists and is writable, will attempt to create folder if it does not exist |
||
| 207 | * Also ensures all the parent folders exist, and if not tries to create them. |
||
| 208 | * Also, if this function creates the folder, adds a .htaccess file and index.html file |
||
| 209 | * @param string $folder |
||
| 210 | * @throws EE_Error if the folder exists and is writeable, but for some reason we |
||
| 211 | * can't write to it |
||
| 212 | * @return bool false if folder isn't writable; true if it exists and is writeable, |
||
| 213 | */ |
||
| 214 | public static function ensure_folder_exists_and_is_writable( $folder = '' ){ |
||
| 247 | |||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * verify_is_writable - checks if a file or folder is writable |
||
| 252 | * @param string $full_path - full server path to file or folder |
||
| 253 | * @param string $file_or_folder - whether checking a file or folder |
||
| 254 | * @throws EE_Error |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public static function verify_is_writable( $full_path = '', $file_or_folder = 'folder' ){ |
||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * ensure_file_exists_and_is_writable |
||
| 276 | * ensures that a file exists and is writable, will attempt to create file if it does not exist. |
||
| 277 | * Also ensures all the parent folders exist, and if not tries to create them. |
||
| 278 | * @param string $full_file_path |
||
| 279 | * @throws EE_Error |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | public static function ensure_file_exists_and_is_writable( $full_file_path = '' ) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Gets the parent folder. If provided with file, gets the folder that contains it. |
||
| 308 | * If provided a folder, gets its parent folder. |
||
| 309 | * @param string $file_or_folder_path |
||
| 310 | * @return string parent folder, ENDING with a directory separator |
||
| 311 | */ |
||
| 312 | public static function get_parent_folder( $file_or_folder_path ) { |
||
| 319 | |||
| 320 | public static function ensure_folder_exists_recursively( $folder ) { |
||
| 323 | |||
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * get_file_contents |
||
| 328 | * @param string $full_file_path |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public static function get_file_contents( $full_file_path = '' ){ |
||
| 340 | |||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * write_file |
||
| 345 | * @param string $full_file_path |
||
| 346 | * @param string $file_contents - the content to be written to the file |
||
| 347 | * @param string $file_type |
||
| 348 | * @throws EE_Error |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | public static function write_to_file( $full_file_path = '', $file_contents = '', $file_type = '' ){ |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Wrapper for WP_Filesystem_Base::delete |
||
| 379 | * |
||
| 380 | * @param string $filepath |
||
| 381 | * @param boolean $recursive |
||
| 382 | * @param boolean|string $type 'd' for directory, 'f' for file |
||
| 383 | * @return boolean |
||
| 384 | */ |
||
| 385 | public static function delete( $filepath, $recursive = false, $type = false ) { |
||
| 389 | |||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * exists |
||
| 394 | * checks if a file exists using the WP filesystem |
||
| 395 | * |
||
| 396 | * @param string $full_file_path |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | public static function exists( $full_file_path = '' ) { |
||
| 403 | |||
| 404 | |||
| 405 | |||
| 406 | /** |
||
| 407 | * is_readable |
||
| 408 | * checks if a file is_readable using the WP filesystem |
||
| 409 | * |
||
| 410 | * @param string $full_file_path |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | public static function is_readable( $full_file_path = '' ) { |
||
| 421 | |||
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * remove_filename_from_filepath |
||
| 426 | * 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 |
||
| 427 | * |
||
| 428 | * @param string $full_file_path |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | public static function remove_filename_from_filepath( $full_file_path = '' ) { |
||
| 434 | |||
| 435 | |||
| 436 | /** |
||
| 437 | * get_filename_from_filepath. Arguably the same as basename() |
||
| 438 | * |
||
| 439 | * @param string $full_file_path |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public static function get_filename_from_filepath( $full_file_path = '' ) { |
||
| 445 | |||
| 446 | |||
| 447 | /** |
||
| 448 | * get_file_extension |
||
| 449 | * |
||
| 450 | * @param string $full_file_path |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | public static function get_file_extension( $full_file_path = '' ) { |
||
| 456 | |||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * add_htaccess_deny_from_all so the webserver cannot access this folder |
||
| 461 | * @param string $folder |
||
| 462 | * @return bool |
||
| 463 | */ |
||
| 464 | View Code Duplication | public static function add_htaccess_deny_from_all( $folder = '' ) { |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Adds an index file to this folder, so folks can't list all the file's contents |
||
| 477 | * @param string $folder |
||
| 478 | * @return boolean |
||
| 479 | */ |
||
| 480 | View Code Duplication | public static function add_index_file( $folder ) { |
|
| 489 | |||
| 490 | |||
| 491 | |||
| 492 | /** |
||
| 493 | * Given that the file in $file_path has the normal name, (ie, CLASSNAME.whatever.php), |
||
| 494 | * extract that classname. |
||
| 495 | * @param string $file_path |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | public static function get_classname_from_filepath_with_standard_filename( $file_path ){ |
||
| 505 | |||
| 506 | |||
| 507 | |||
| 508 | /** |
||
| 509 | * standardise_directory_separators |
||
| 510 | * convert all directory separators in a file path to whatever is set for DS |
||
| 511 | * @param string $file_path |
||
| 512 | * @return string |
||
| 513 | */ |
||
| 514 | public static function standardise_directory_separators( $file_path ){ |
||
| 517 | |||
| 518 | |||
| 519 | |||
| 520 | /** |
||
| 521 | * end_with_directory_separator |
||
| 522 | * ensures that file path ends with DS |
||
| 523 | * @param string $file_path |
||
| 524 | * @return string |
||
| 525 | */ |
||
| 526 | public static function end_with_directory_separator( $file_path ){ |
||
| 529 | |||
| 530 | |||
| 531 | |||
| 532 | /** |
||
| 533 | * shorthand for both EEH_FIle::end_with_directory_separator AND EEH_File::standardise_directory_separators |
||
| 534 | * @param $file_path |
||
| 535 | * @return string |
||
| 536 | */ |
||
| 537 | public static function standardise_and_end_with_directory_separator( $file_path ){ |
||
| 540 | |||
| 541 | |||
| 542 | |||
| 543 | /** |
||
| 544 | * takes the folder name (with or without trailing slash) and finds the files it in, |
||
| 545 | * and what the class's name inside of each should be. |
||
| 546 | * @param array $folder_paths |
||
| 547 | * @param boolean $index_numerically if TRUE, the returned array will be indexed numerically; |
||
| 548 | * if FALSE (Default), returned array will be indexed by the filenames minus extensions. |
||
| 549 | * Set it TRUE if you know there are files in the directory with the same name but different extensions |
||
| 550 | * @throws \EE_Error |
||
| 551 | * @return array if $index_numerically == TRUE keys are numeric , |
||
| 552 | * if $index_numerically == FALSE (Default) keys are what the class names SHOULD be; |
||
| 553 | * and values are their filepaths |
||
| 554 | */ |
||
| 555 | public static function get_contents_of_folders( $folder_paths = array(), $index_numerically = FALSE ){ |
||
| 578 | |||
| 579 | |||
| 580 | |||
| 581 | /** |
||
| 582 | * Copies a file. Mostly a wrapper of WP_Filesystem::copy |
||
| 583 | * @param string $source_file |
||
| 584 | * @param string $destination_file |
||
| 585 | * @param boolean $overwrite |
||
| 586 | * @return boolean success |
||
| 587 | * @throws EE_Error |
||
| 588 | */ |
||
| 589 | public static function copy( $source_file, $destination_file, $overwrite = FALSE ){ |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Reports whether or not the filepath is in the EE uploads folder or not |
||
| 630 | * @param string $filepath |
||
| 631 | * @return boolean |
||
| 632 | */ |
||
| 633 | public static function is_in_uploads_folder( $filepath ) { |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Given a "local" filepath (what you probably thought was the only filepath), |
||
| 640 | * converts it into a "remote" filepath (the filepath the currently-in-use |
||
| 641 | * $wp_filesystem needs to use access the folder or file). |
||
| 642 | * See http://wordpress.stackexchange.com/questions/124900/using-wp-filesystem-in-plugins |
||
| 643 | * @param WP_Filesystem_Base $wp_filesystem we aren't initially sure which one |
||
| 644 | * is in use, so you need to provide it |
||
| 645 | * @param string $local_filepath the filepath to the folder/file locally |
||
| 646 | * @return string the remote filepath (eg the filepath the filesystem method, eg |
||
| 647 | * ftp or ssh, will use to access the folder |
||
| 648 | */ |
||
| 649 | public static function convert_local_filepath_to_remote_filepath( $local_filepath ) { |
||
| 653 | } |
||
| 654 | // End of file EEH_File.helper.php |
||
| 655 | // 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: