Complex classes like Path 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 Path, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 11 | class Path { | 
            ||
| 12 | |||
| 13 | /**  | 
            ||
| 14 | * The path to the directory that backup files are stored in  | 
            ||
| 15 | *  | 
            ||
| 16 | * @var string $this->path  | 
            ||
| 17 | */  | 
            ||
| 18 | private $path;  | 
            ||
| 19 | |||
| 20 | /**  | 
            ||
| 21 | * The path to the directory that will be backed up  | 
            ||
| 22 | *  | 
            ||
| 23 | * @var string $this->root  | 
            ||
| 24 | */  | 
            ||
| 25 | private $root;  | 
            ||
| 26 | |||
| 27 | /**  | 
            ||
| 28 | * The path to the directory that backup files are stored in  | 
            ||
| 29 | *  | 
            ||
| 30 | * @var string $this->path  | 
            ||
| 31 | */  | 
            ||
| 32 | private $custom_path;  | 
            ||
| 33 | |||
| 34 | /**  | 
            ||
| 35 | * Contains the instantiated Path instance  | 
            ||
| 36 | *  | 
            ||
| 37 | * @var Path $this->instance  | 
            ||
| 38 | */  | 
            ||
| 39 | private static $instance;  | 
            ||
| 40 | |||
| 41 | /**  | 
            ||
| 42 | * Protected constructor to prevent creating a new instance of the  | 
            ||
| 43 | * *Singleton* via the `new` operator from outside of this class.  | 
            ||
| 44 | */  | 
            ||
| 45 | 	protected function __construct() {} | 
            ||
| 46 | |||
| 47 | /**  | 
            ||
| 48 | * Private clone method to prevent cloning of the instance of the  | 
            ||
| 49 | * *Singleton* instance.  | 
            ||
| 50 | */  | 
            ||
| 51 | 	private function __clone() {} | 
            ||
| 52 | |||
| 53 | /**  | 
            ||
| 54 | * Private unserialize method to prevent unserializing of the *Singleton*  | 
            ||
| 55 | * instance.  | 
            ||
| 56 | */  | 
            ||
| 57 | 	private function __wakeup() {} | 
            ||
| 58 | |||
| 59 | /**  | 
            ||
| 60 | * Returns the *Singleton* instance of this class.  | 
            ||
| 61 | *  | 
            ||
| 62 | * @staticvar Path $instance The *Singleton* instances of this class.  | 
            ||
| 63 | *  | 
            ||
| 64 | * @return Path The *Singleton* instance.  | 
            ||
| 65 | */  | 
            ||
| 66 | 	public static function get_instance() { | 
            ||
| 67 | |||
| 68 | 		if ( ! ( self::$instance instanceof Path ) ) { | 
            ||
| 69 | self::$instance = new Path();  | 
            ||
| 70 | }  | 
            ||
| 71 | |||
| 72 | return self::$instance;  | 
            ||
| 73 | }  | 
            ||
| 74 | |||
| 75 | /**  | 
            ||
| 76 | * Convenience method for quickly grabbing the path  | 
            ||
| 77 | */  | 
            ||
| 78 | 	public static function get_path() { | 
            ||
| 81 | |||
| 82 | /**  | 
            ||
| 83 | * Convenience method for quickly grabbing the root  | 
            ||
| 84 | */  | 
            ||
| 85 | 	public static function get_root() { | 
            ||
| 88 | |||
| 89 | /**  | 
            ||
| 90 | * Calculate the path to the site "home" directory.  | 
            ||
| 91 | *  | 
            ||
| 92 | * The home directory is the path equivalent to the home_url. That is,  | 
            ||
| 93 | * the path to the true root of the website. In situations where WordPress is  | 
            ||
| 94 | * installed in a subdirectory the home path is different to ABSPATH  | 
            ||
| 95 | *  | 
            ||
| 96 | * @param string $site_path The site_path to use when calculating the home path, defaults to ABSPATH  | 
            ||
| 97 | */  | 
            ||
| 98 | 	public static function get_home_path( $site_path = ABSPATH ) { | 
            ||
| 
                                                                                                                                                        
                         1 ignored issue 
                            –
                            show
                         | 
                |||
| 99 | |||
| 100 | 		if ( defined( 'HMBKP_ROOT' ) && HMBKP_ROOT ) { | 
            ||
| 101 | return wp_normalize_path( HMBKP_ROOT );  | 
            ||
| 102 | }  | 
            ||
| 103 | |||
| 104 | $home_path = wp_normalize_path( $site_path );  | 
            ||
| 105 | |||
| 106 | 		if ( path_in_php_open_basedir( dirname( $site_path ) ) ) { | 
            ||
| 107 | |||
| 108 | $home = set_url_scheme( get_option( 'home' ), 'http' );  | 
            ||
| 109 | $siteurl = set_url_scheme( get_option( 'siteurl' ), 'http' );  | 
            ||
| 110 | 			if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) { | 
            ||
| 111 | $wp_path_rel_to_home = str_ireplace( $home, '', $siteurl ); /* $siteurl - $home */  | 
            ||
| 112 | $pos = strripos( wp_normalize_path( $_SERVER['SCRIPT_FILENAME'] ), trailingslashit( $wp_path_rel_to_home ) );  | 
            ||
| 113 | $home_path = substr( wp_normalize_path( $_SERVER['SCRIPT_FILENAME'] ), 0, $pos );  | 
            ||
| 114 | $home_path = trailingslashit( $home_path );  | 
            ||
| 115 | }  | 
            ||
| 116 | |||
| 117 | 			if ( is_multisite() ) { | 
            ||
| 118 | $slashed_home = trailingslashit( get_option( 'home' ) );  | 
            ||
| 119 | $base = parse_url( $slashed_home, PHP_URL_PATH );  | 
            ||
| 120 | $document_root_fix = wp_normalize_path( realpath( $_SERVER['DOCUMENT_ROOT'] ) );  | 
            ||
| 121 | $abspath_fix = wp_normalize_path( ABSPATH );  | 
            ||
| 122 | $home_path = strpos( $abspath_fix, $document_root_fix ) === 0 ? $document_root_fix . $base : $home_path;  | 
            ||
| 123 | }  | 
            ||
| 124 | }  | 
            ||
| 125 | |||
| 126 | return wp_normalize_path( untrailingslashit( $home_path ) );  | 
            ||
| 127 | |||
| 128 | }  | 
            ||
| 129 | |||
| 130 | /**  | 
            ||
| 131 | * get the calculated path to the directory where backups will be stored  | 
            ||
| 132 | */  | 
            ||
| 133 | 	private function get_calculated_path() { | 
            ||
| 146 | |||
| 147 | /**  | 
            ||
| 148 | * Set the path directly, overriding the default  | 
            ||
| 149 | *  | 
            ||
| 150 | * @param $path  | 
            ||
| 151 | */  | 
            ||
| 152 | 	public function set_path( $path ) { | 
            ||
| 160 | |||
| 161 | /**  | 
            ||
| 162 | * get the calculated path to the directory that will be backed up  | 
            ||
| 163 | */  | 
            ||
| 164 | 	private function get_calculated_root() { | 
            ||
| 179 | |||
| 180 | /**  | 
            ||
| 181 | * Set the root path directly, overriding the default  | 
            ||
| 182 | *  | 
            ||
| 183 | * @param $root  | 
            ||
| 184 | */  | 
            ||
| 185 | 	public function set_root( $root ) { | 
            ||
| 188 | |||
| 189 | 	public function reset_path() { | 
            ||
| 192 | |||
| 193 | /**  | 
            ||
| 194 | * Get the path to the default backup location in wp-content  | 
            ||
| 195 | */  | 
            ||
| 196 | 	public function get_default_path() { | 
            ||
| 199 | |||
| 200 | /**  | 
            ||
| 201 | * Get the path to the fallback backup location in uploads  | 
            ||
| 202 | */  | 
            ||
| 203 | 	public function get_fallback_path() { | 
            ||
| 210 | |||
| 211 | /**  | 
            ||
| 212 | * Get the path to the custom backup location if it's been set  | 
            ||
| 213 | */  | 
            ||
| 214 | 	public function get_custom_path() { | 
            ||
| 227 | |||
| 228 | /**  | 
            ||
| 229 | * Builds an array containing existing backups folders.  | 
            ||
| 230 | *  | 
            ||
| 231 | * @return array  | 
            ||
| 232 | */  | 
            ||
| 233 | 	public function get_existing_paths() { | 
            ||
| 252 | |||
| 253 | /**  | 
            ||
| 254 | * Returns the first existing path if there is one  | 
            ||
| 255 | *  | 
            ||
| 256 | * @return string Backup path if found empty string if not  | 
            ||
| 257 | */  | 
            ||
| 258 | 	public function get_existing_path() { | 
            ||
| 269 | |||
| 270 | /**  | 
            ||
| 271 | * Calculate the backup path and create the directory if it doesn't exist.  | 
            ||
| 272 | *  | 
            ||
| 273 | * Tries all possible locations and uses the first one possible  | 
            ||
| 274 | *  | 
            ||
| 275 | * @return  | 
            ||
| 276 | */  | 
            ||
| 277 | 	public function calculate_path() { | 
            ||
| 298 | |||
| 299 | 	public function get_possible_paths() { | 
            ||
| 322 | |||
| 323 | /**  | 
            ||
| 324 | * Protect the directory that backups are stored in  | 
            ||
| 325 | *  | 
            ||
| 326 | * - Adds an index.html file in an attempt to disable directory browsing  | 
            ||
| 327 | * - Adds a .httaccess file to deny direct access if on Apache  | 
            ||
| 328 | *  | 
            ||
| 329 | * @param string $reset  | 
            ||
| 330 | */  | 
            ||
| 331 | 	public function protect_path( $reset = 'no' ) { | 
            ||
| 371 | |||
| 372 | /**  | 
            ||
| 373 | * If we have more than one path then move any existing backups to the current path and remove them  | 
            ||
| 374 | */  | 
            ||
| 375 | 	public function merge_existing_paths() { | 
            ||
| 386 | |||
| 387 | /**  | 
            ||
| 388 | * Move backup files from an existing directory and the new  | 
            ||
| 389 | * location  | 
            ||
| 390 | *  | 
            ||
| 391 | * @param string $path The path to move the backups from  | 
            ||
| 392 | */  | 
            ||
| 393 | 	public function move_old_backups( $from ) { | 
            ||
| 431 | |||
| 432 | /**  | 
            ||
| 433 | * Clean any temporary / incomplete backups from the backups directory  | 
            ||
| 434 | */  | 
            ||
| 435 | 	public function cleanup() { | 
            ||
| 452 | }  | 
            ||
| 453 | |||
| 479 | 
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.