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) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * display_request_filesystem_credentials_form |
||
| 104 | */ |
||
| 105 | public static function display_request_filesystem_credentials_form() { |
||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * verify_filepath_and_permissions |
||
| 115 | * checks that a file is readable and has sufficient file permissions set to access |
||
| 116 | * |
||
| 117 | * @access public |
||
| 118 | * @param string $full_file_path - full server path to the folder or file |
||
| 119 | * @param string $file_name - name of file if checking a file |
||
| 120 | * @param string $file_ext - file extension (ie: "php") if checking a file |
||
| 121 | * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
||
| 122 | * @throws EE_Error |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | public static function verify_filepath_and_permissions( $full_file_path = '', $file_name = '', $file_ext = '', $type_of_file = '' ) { |
||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * _permissions_error_for_unreadable_filepath - attempts to determine why permissions are set incorrectly for a file or folder |
||
| 158 | * |
||
| 159 | * @access private |
||
| 160 | * @param string $full_file_path - full server path to the folder or file |
||
| 161 | * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | private static function _permissions_error_for_unreadable_filepath( $full_file_path = '', $type_of_file = '' ){ |
||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * ensure_folder_exists_and_is_writable |
||
| 191 | * ensures that a folder exists and is writable, will attempt to create folder if it does not exist |
||
| 192 | * Also ensures all the parent folders exist, and if not tries to create them. |
||
| 193 | * Also, if this function creates the folder, adds a .htaccess file and index.html file |
||
| 194 | * @param string $folder |
||
| 195 | * @throws EE_Error if the folder exists and is writeable, but for some reason we |
||
| 196 | * can't write to it |
||
| 197 | * @return bool false if folder isn't writable; true if it exists and is writeable, |
||
| 198 | */ |
||
| 199 | public static function ensure_folder_exists_and_is_writable( $folder = '' ){ |
||
| 232 | |||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * verify_is_writable - checks if a file or folder is writable |
||
| 237 | * @param string $full_path - full server path to file or folder |
||
| 238 | * @param string $file_or_folder - whether checking a file or folder |
||
| 239 | * @throws EE_Error |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | public static function verify_is_writable( $full_path = '', $file_or_folder = 'folder' ){ |
||
| 256 | |||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * ensure_file_exists_and_is_writable |
||
| 261 | * ensures that a file exists and is writable, will attempt to create file if it does not exist. |
||
| 262 | * Also ensures all the parent folders exist, and if not tries to create them. |
||
| 263 | * @param string $full_file_path |
||
| 264 | * @throws EE_Error |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | public static function ensure_file_exists_and_is_writable( $full_file_path = '' ) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Gets the parent folder. If provided with file, gets the folder that contains it. |
||
| 293 | * If provided a folder, gets its parent folder. |
||
| 294 | * @param string $file_or_folder_path |
||
| 295 | * @return string parent folder, ENDING with a directory separator |
||
| 296 | */ |
||
| 297 | public static function get_parent_folder( $file_or_folder_path ) { |
||
| 304 | |||
| 305 | public static function ensure_folder_exists_recursively( $folder ) { |
||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * get_file_contents |
||
| 313 | * @param string $full_file_path |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public static function get_file_contents( $full_file_path = '' ){ |
||
| 325 | |||
| 326 | |||
| 327 | |||
| 328 | /** |
||
| 329 | * write_file |
||
| 330 | * @param string $full_file_path |
||
| 331 | * @param string $file_contents - the content to be written to the file |
||
| 332 | * @param string $file_type |
||
| 333 | * @throws EE_Error |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | public static function write_to_file( $full_file_path = '', $file_contents = '', $file_type = '' ){ |
||
| 361 | |||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * exists |
||
| 366 | * checks if a file exists using the WP filesystem |
||
| 367 | * |
||
| 368 | * @param string $full_file_path |
||
| 369 | * @return bool |
||
| 370 | */ |
||
| 371 | public static function exists( $full_file_path = '' ) { |
||
| 375 | |||
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * is_readable |
||
| 380 | * checks if a file is_readable using the WP filesystem |
||
| 381 | * |
||
| 382 | * @param string $full_file_path |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | public static function is_readable( $full_file_path = '' ) { |
||
| 393 | |||
| 394 | |||
| 395 | |||
| 396 | /** |
||
| 397 | * remove_filename_from_filepath |
||
| 398 | * 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 |
||
| 399 | * |
||
| 400 | * @param string $full_file_path |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public static function remove_filename_from_filepath( $full_file_path = '' ) { |
||
| 406 | |||
| 407 | |||
| 408 | /** |
||
| 409 | * get_filename_from_filepath. Arguably the same as basename() |
||
| 410 | * |
||
| 411 | * @param string $full_file_path |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | public static function get_filename_from_filepath( $full_file_path = '' ) { |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * get_file_extension |
||
| 421 | * |
||
| 422 | * @param string $full_file_path |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | public static function get_file_extension( $full_file_path = '' ) { |
||
| 428 | |||
| 429 | |||
| 430 | |||
| 431 | /** |
||
| 432 | * add_htaccess_deny_from_all and an index.html file |
||
| 433 | * in order to prevent folks from exploring filesystem at their leisure |
||
| 434 | * @param string $folder |
||
| 435 | * @return bool |
||
| 436 | */ |
||
| 437 | public static function add_htaccess_deny_from_all( $folder = '' ) { |
||
| 451 | |||
| 452 | |||
| 453 | |||
| 454 | /** |
||
| 455 | * Given that the file in $file_path has the normal name, (ie, CLASSNAME.whatever.php), |
||
| 456 | * extract that classname. |
||
| 457 | * @param string $file_path |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | public static function get_classname_from_filepath_with_standard_filename( $file_path ){ |
||
| 467 | |||
| 468 | |||
| 469 | |||
| 470 | /** |
||
| 471 | * standardise_directory_separators |
||
| 472 | * convert all directory separators in a file path to whatever is set for DS |
||
| 473 | * @param string $file_path |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | public static function standardise_directory_separators( $file_path ){ |
||
| 479 | |||
| 480 | |||
| 481 | |||
| 482 | /** |
||
| 483 | * end_with_directory_separator |
||
| 484 | * ensures that file path ends with DS |
||
| 485 | * @param string $file_path |
||
| 486 | * @return string |
||
| 487 | */ |
||
| 488 | public static function end_with_directory_separator( $file_path ){ |
||
| 491 | |||
| 492 | |||
| 493 | |||
| 494 | /** |
||
| 495 | * shorthand for both EEH_FIle::end_with_directory_separator AND EEH_File::standardise_directory_separators |
||
| 496 | * @param $file_path |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | public static function standardise_and_end_with_directory_separator( $file_path ){ |
||
| 502 | |||
| 503 | |||
| 504 | |||
| 505 | /** |
||
| 506 | * takes the folder name (with or without trailing slash) and finds the files it in, |
||
| 507 | * and what the class's name inside of each should be. |
||
| 508 | * @param array $folder_paths |
||
| 509 | * @param boolean $index_numerically if TRUE, the returned array will be indexed numerically; |
||
| 510 | * if FALSE (Default), returned array will be indexed by the filenames minus extensions. |
||
| 511 | * Set it TRUE if you know there are files in the directory with the same name but different extensions |
||
| 512 | * @throws \EE_Error |
||
| 513 | * @return array if $index_numerically == TRUE keys are numeric , |
||
| 514 | * if $index_numerically == FALSE (Default) keys are what the class names SHOULD be; |
||
| 515 | * and values are their filepaths |
||
| 516 | */ |
||
| 517 | public static function get_contents_of_folders( $folder_paths = array(), $index_numerically = FALSE ){ |
||
| 540 | |||
| 541 | |||
| 542 | |||
| 543 | /** |
||
| 544 | * Copies a file. Mostly a wrapper of WP_Filesystem::copy |
||
| 545 | * @param string $source_file |
||
| 546 | * @param string $destination_file |
||
| 547 | * @param boolean $overwrite |
||
| 548 | * @return boolean success |
||
| 549 | * @throws EE_Error |
||
| 550 | */ |
||
| 551 | public static function copy( $source_file, $destination_file, $overwrite = FALSE ){ |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Reports whether or not the filepath is in the EE uploads folder or not |
||
| 592 | * @param string $filepath |
||
| 593 | * @return boolean |
||
| 594 | */ |
||
| 595 | public static function is_in_uploads_folder( $filepath ) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Given a "local" filepath (what you probably thought was the only filepath), |
||
| 602 | * converts it into a "remote" filepath (the filepath the currently-in-use |
||
| 603 | * $wp_filesystem needs to use access the folder or file). |
||
| 604 | * See http://wordpress.stackexchange.com/questions/124900/using-wp-filesystem-in-plugins |
||
| 605 | * @param WP_Filesystem_Base $wp_filesystem we aren't initially sure which one |
||
| 606 | * is in use, so you need to provide it |
||
| 607 | * @param string $local_filepath the filepath to the folder/file locally |
||
| 608 | * @return string the remote filepath (eg the filepath the filesystem method, eg |
||
| 609 | * ftp or ssh, will use to access the folder |
||
| 610 | */ |
||
| 611 | public static function convert_local_filepath_to_remote_filepath( $local_filepath ) { |
||
| 615 | } |
||
| 616 | // End of file EEH_File.helper.php |
||
| 617 | // 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: