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 |
||
| 25 | class EEH_File extends EEH_Base implements EEHI_File |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string $_credentials_form |
||
| 30 | */ |
||
| 31 | private static $_credentials_form; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var WP_Filesystem_Base $_wp_filesystem |
||
| 35 | */ |
||
| 36 | protected static $_wp_filesystem; |
||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * @param string|null $filepath the filepath we want to work in. If its in the |
||
| 41 | * wp uploads directory, we'll want to just use the filesystem directly. |
||
| 42 | * If not provided, we have to assume its not in the uploads directory |
||
| 43 | * @return WP_Filesystem_Base |
||
| 44 | */ |
||
| 45 | private static function _get_wp_filesystem($filepath = null) |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * @return WP_Filesystem_Base |
||
| 60 | */ |
||
| 61 | private static function loadAlternateWpFileSystem() |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * @return WP_Filesystem_Base |
||
| 121 | */ |
||
| 122 | private static function loadWpFileSystem() |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * display_request_filesystem_credentials_form |
||
| 199 | */ |
||
| 200 | public static function display_request_filesystem_credentials_form() |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * verify_filepath_and_permissions |
||
| 210 | * checks that a file is readable and has sufficient file permissions set to access |
||
| 211 | * |
||
| 212 | * @access public |
||
| 213 | * @param string $full_file_path - full server path to the folder or file |
||
| 214 | * @param string $file_name - name of file if checking a file |
||
| 215 | * @param string $file_ext - file extension (ie: "php") if checking a file |
||
| 216 | * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | public static function verify_filepath_and_permissions( |
||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * _permissions_error_for_unreadable_filepath - attempts to determine why permissions are set incorrectly for a |
||
| 259 | * file or folder |
||
| 260 | * |
||
| 261 | * @access private |
||
| 262 | * @param string $full_file_path - full server path to the folder or file |
||
| 263 | * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | private static function _permissions_error_for_unreadable_filepath($full_file_path = '', $type_of_file = '') |
||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * ensure_folder_exists_and_is_writable |
||
| 299 | * ensures that a folder exists and is writable, will attempt to create folder if it does not exist |
||
| 300 | * Also ensures all the parent folders exist, and if not tries to create them. |
||
| 301 | * Also, if this function creates the folder, adds a .htaccess file and index.html file |
||
| 302 | * |
||
| 303 | * @param string $folder |
||
| 304 | * @return bool false if folder isn't writable; true if it exists and is writeable, |
||
| 305 | */ |
||
| 306 | public static function ensure_folder_exists_and_is_writable($folder = '') |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * verify_is_writable - checks if a file or folder is writable |
||
| 344 | * |
||
| 345 | * @param string $full_path - full server path to file or folder |
||
| 346 | * @param string $file_or_folder - whether checking a file or folder |
||
| 347 | * @return bool |
||
| 348 | */ |
||
| 349 | public static function verify_is_writable($full_path = '', $file_or_folder = 'folder') |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * ensure_file_exists_and_is_writable |
||
| 370 | * ensures that a file exists and is writable, will attempt to create file if it does not exist. |
||
| 371 | * Also ensures all the parent folders exist, and if not tries to create them. |
||
| 372 | * |
||
| 373 | * @param string $full_file_path |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | public static function ensure_file_exists_and_is_writable($full_file_path = '') |
||
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * Gets the parent folder. If provided with file, gets the folder that contains it. |
||
| 404 | * If provided a folder, gets its parent folder. |
||
| 405 | * |
||
| 406 | * @param string $file_or_folder_path |
||
| 407 | * @return string parent folder, ENDING with a directory separator |
||
| 408 | */ |
||
| 409 | public static function get_parent_folder($file_or_folder_path) |
||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * get_file_contents |
||
| 423 | * |
||
| 424 | * @param string $full_file_path |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | public static function get_file_contents($full_file_path = '') |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * write_file |
||
| 445 | * |
||
| 446 | * @param string $full_file_path |
||
| 447 | * @param string $file_contents - the content to be written to the file |
||
| 448 | * @param string $file_type |
||
| 449 | * @return bool |
||
| 450 | */ |
||
| 451 | public static function write_to_file($full_file_path = '', $file_contents = '', $file_type = '') |
||
| 490 | |||
| 491 | |||
| 492 | /** |
||
| 493 | * Wrapper for WP_Filesystem_Base::delete |
||
| 494 | * |
||
| 495 | * @param string $filepath |
||
| 496 | * @param boolean $recursive |
||
| 497 | * @param boolean|string $type 'd' for directory, 'f' for file |
||
| 498 | * @return boolean |
||
| 499 | */ |
||
| 500 | public static function delete($filepath, $recursive = false, $type = false) |
||
| 505 | |||
| 506 | |||
| 507 | /** |
||
| 508 | * exists |
||
| 509 | * checks if a file exists using the WP filesystem |
||
| 510 | * |
||
| 511 | * @param string $full_file_path |
||
| 512 | * @return bool |
||
| 513 | */ |
||
| 514 | public static function exists($full_file_path = '') |
||
| 519 | |||
| 520 | |||
| 521 | /** |
||
| 522 | * is_readable |
||
| 523 | * checks if a file is_readable using the WP filesystem |
||
| 524 | * |
||
| 525 | * @param string $full_file_path |
||
| 526 | * @return bool |
||
| 527 | */ |
||
| 528 | public static function is_readable($full_file_path = '') |
||
| 533 | |||
| 534 | |||
| 535 | /** |
||
| 536 | * remove_filename_from_filepath |
||
| 537 | * given a full path to a file including the filename itself, this removes the filename and returns the path, up |
||
| 538 | * to, but NOT including the filename OR slash |
||
| 539 | * |
||
| 540 | * @param string $full_file_path |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | public static function remove_filename_from_filepath($full_file_path = '') |
||
| 547 | |||
| 548 | |||
| 549 | /** |
||
| 550 | * get_filename_from_filepath. Arguably the same as basename() |
||
| 551 | * |
||
| 552 | * @param string $full_file_path |
||
| 553 | * @return string |
||
| 554 | */ |
||
| 555 | public static function get_filename_from_filepath($full_file_path = '') |
||
| 559 | |||
| 560 | |||
| 561 | /** |
||
| 562 | * get_file_extension |
||
| 563 | * |
||
| 564 | * @param string $full_file_path |
||
| 565 | * @return string |
||
| 566 | */ |
||
| 567 | public static function get_file_extension($full_file_path = '') |
||
| 571 | |||
| 572 | |||
| 573 | /** |
||
| 574 | * add_htaccess_deny_from_all so the web server cannot access this folder |
||
| 575 | * |
||
| 576 | * @param string $folder |
||
| 577 | * @return bool |
||
| 578 | */ |
||
| 579 | View Code Duplication | public static function add_htaccess_deny_from_all($folder = '') |
|
| 590 | |||
| 591 | |||
| 592 | /** |
||
| 593 | * Adds an index file to this folder, so folks can't list all the file's contents |
||
| 594 | * |
||
| 595 | * @param string $folder |
||
| 596 | * @return boolean |
||
| 597 | */ |
||
| 598 | View Code Duplication | public static function add_index_file($folder) |
|
| 612 | |||
| 613 | |||
| 614 | /** |
||
| 615 | * Given that the file in $file_path has the normal name, (ie, CLASSNAME.whatever.php), |
||
| 616 | * extract that classname. |
||
| 617 | * |
||
| 618 | * @param string $file_path |
||
| 619 | * @return string |
||
| 620 | */ |
||
| 621 | public static function get_classname_from_filepath_with_standard_filename($file_path) |
||
| 629 | |||
| 630 | |||
| 631 | /** |
||
| 632 | * standardise_directory_separators |
||
| 633 | * convert all directory separators in a file path. |
||
| 634 | * |
||
| 635 | * @param string $file_path |
||
| 636 | * @param bool $rtrim will remove trailing backslash |
||
| 637 | * @return string |
||
| 638 | */ |
||
| 639 | public static function standardise_directory_separators($file_path, $rtrim = false) |
||
| 644 | |||
| 645 | |||
| 646 | /** |
||
| 647 | * end_with_directory_separator |
||
| 648 | * ensures that file path ends with '/' |
||
| 649 | * |
||
| 650 | * @param string $file_path |
||
| 651 | * @return string |
||
| 652 | */ |
||
| 653 | public static function end_with_directory_separator($file_path) |
||
| 657 | |||
| 658 | |||
| 659 | /** |
||
| 660 | * shorthand for both EEH_FIle::end_with_directory_separator AND EEH_File::standardise_directory_separators |
||
| 661 | * |
||
| 662 | * @param $file_path |
||
| 663 | * @return string |
||
| 664 | */ |
||
| 665 | public static function standardise_and_end_with_directory_separator($file_path) |
||
| 669 | |||
| 670 | |||
| 671 | /** |
||
| 672 | * takes the folder name (with or without trailing slash) and finds the files it in, |
||
| 673 | * and what the class's name inside of each should be. |
||
| 674 | * |
||
| 675 | * @param array $folder_paths |
||
| 676 | * @param boolean $index_numerically if TRUE, the returned array will be indexed numerically; |
||
| 677 | * if FALSE (Default), returned array will be indexed by the filenames minus |
||
| 678 | * extensions. Set it TRUE if you know there are files in the directory with the |
||
| 679 | * same name but different extensions |
||
| 680 | * @return array if $index_numerically == TRUE keys are numeric , |
||
| 681 | * if $index_numerically == FALSE (Default) keys are what the class names SHOULD |
||
| 682 | * be; and values are their file paths |
||
| 683 | */ |
||
| 684 | public static function get_contents_of_folders($folder_paths = [], $index_numerically = false) |
||
| 709 | |||
| 710 | |||
| 711 | /** |
||
| 712 | * Copies a file. Mostly a wrapper of WP_Filesystem::copy |
||
| 713 | * |
||
| 714 | * @param string $source_file |
||
| 715 | * @param string $destination_file |
||
| 716 | * @param boolean $overwrite |
||
| 717 | * @return boolean success |
||
| 718 | */ |
||
| 719 | public static function copy($source_file, $destination_file, $overwrite = false) |
||
| 750 | |||
| 751 | |||
| 752 | /** |
||
| 753 | * Reports whether or not the filepath is in the EE uploads folder or not |
||
| 754 | * |
||
| 755 | * @param string $filepath |
||
| 756 | * @return boolean |
||
| 757 | */ |
||
| 758 | public static function is_in_uploads_folder($filepath) |
||
| 763 | |||
| 764 | |||
| 765 | /** |
||
| 766 | * Given a "local" filepath (what you probably thought was the only filepath), |
||
| 767 | * converts it into a "remote" filepath (the filepath the currently-in-use |
||
| 768 | * $wp_filesystem needs to use access the folder or file). |
||
| 769 | * See http://wordpress.stackexchange.com/questions/124900/using-wp-filesystem-in-plugins |
||
| 770 | * |
||
| 771 | * @param string $local_filepath the filepath to the folder/file locally |
||
| 772 | * @return string the remote filepath (eg the filepath the filesystem method, eg |
||
| 773 | * ftp or ssh, will use to access the folder |
||
| 774 | */ |
||
| 775 | public static function convert_local_filepath_to_remote_filepath($local_filepath) |
||
| 780 | |||
| 781 | |||
| 782 | /** |
||
| 783 | * wrapper for WP_Filesystem::chmod() |
||
| 784 | * |
||
| 785 | * @param string $file Path to the file. |
||
| 786 | * @param int|false $mode Optional. The permissions as octal number, usually 0644 for files, |
||
| 787 | * 0755 for directories. Default false. |
||
| 788 | * @param bool $recursive Optional. If set to true, changes file permissions recursively. |
||
| 789 | * Default false. |
||
| 790 | * @return bool True on success, false on failure. |
||
| 791 | */ |
||
| 792 | public static function chmod($file, $mode = false, $recursive = false) |
||
| 797 | |||
| 798 | |||
| 799 | /** |
||
| 800 | * wrapper for WP_Filesystem::getchmod() |
||
| 801 | * |
||
| 802 | * @param string $file Path to the file. |
||
| 803 | * @return string Mode of the file (the last 3 digits). |
||
| 804 | */ |
||
| 805 | public static function permissions($file) |
||
| 810 | |||
| 811 | |||
| 812 | /** |
||
| 813 | * wrapper for WP_Filesystem::owner() |
||
| 814 | * |
||
| 815 | * @param string $file Path to the file. |
||
| 816 | * @return string|false Username of the owner on success, false on failure. |
||
| 817 | */ |
||
| 818 | public static function owner($file) |
||
| 823 | |||
| 824 | |||
| 825 | /** |
||
| 826 | * wrapper for WP_Filesystem::group() |
||
| 827 | * |
||
| 828 | * @param string $file Path to the file. |
||
| 829 | * @return string|false The group on success, false on failure. |
||
| 830 | */ |
||
| 831 | public static function group($file) |
||
| 836 | |||
| 837 | |||
| 838 | /** |
||
| 839 | * wrapper for WP_Filesystem::move() |
||
| 840 | * |
||
| 841 | * @param string $source Path to the source file. |
||
| 842 | * @param string $destination Path to the destination file. |
||
| 843 | * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists. |
||
| 844 | * Default false. |
||
| 845 | * @return bool True on success, false on failure. |
||
| 846 | */ |
||
| 847 | public static function move($source, $destination, $overwrite = false) |
||
| 880 | |||
| 881 | |||
| 882 | /** |
||
| 883 | * @param string $source_file |
||
| 884 | * @return string |
||
| 885 | */ |
||
| 886 | private static function validateFileForCopyOrMove($source_file) |
||
| 904 | |||
| 905 | |||
| 906 | /** |
||
| 907 | * @param string $destination_file |
||
| 908 | * @return string |
||
| 909 | */ |
||
| 910 | private static function validateFolderForCopyOrMove($destination_file) |
||
| 929 | } |
||
| 930 |
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: