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 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() { |
||
| 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 ) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * get the calculated path to the directory where backups will be stored |
||
| 122 | */ |
||
| 123 | private function get_calculated_path() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Set the path directly, overriding the default |
||
| 139 | * |
||
| 140 | * @param $path |
||
| 141 | */ |
||
| 142 | public function set_path( $path ) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * get the calculated path to the directory that will be backed up |
||
| 153 | */ |
||
| 154 | private function get_calculated_root() { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set the root path directly, overriding the default |
||
| 172 | * |
||
| 173 | * @param $root |
||
| 174 | */ |
||
| 175 | public function set_root( $root ) { |
||
| 178 | |||
| 179 | public function reset_path() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get the path to the default backup location in wp-content |
||
| 185 | */ |
||
| 186 | public function get_default_path() { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get the path to the fallback backup location in uploads |
||
| 192 | */ |
||
| 193 | public function get_fallback_path() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the path to the custom backup location if it's been set |
||
| 203 | */ |
||
| 204 | public function get_custom_path() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Builds an array containing existing backups folders. |
||
| 220 | * |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | public function get_existing_paths() { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns the first existing path if there is one |
||
| 243 | * |
||
| 244 | * @return string Backup path if found empty string if not |
||
| 245 | */ |
||
| 246 | public function get_existing_path() { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Calculate the backup path and create the directory if it doesn't exist. |
||
| 260 | * |
||
| 261 | * Tries all possible locations and uses the first one possible |
||
| 262 | * |
||
| 263 | * @return |
||
| 264 | */ |
||
| 265 | public function calculate_path() { |
||
| 266 | |||
| 267 | $paths = array(); |
||
| 268 | |||
| 269 | // If we have a custom path then try to use it |
||
| 270 | if ( $this->get_custom_path() ) { |
||
| 271 | $paths[] = $this->get_custom_path(); |
||
| 272 | } |
||
| 273 | |||
| 274 | // If there is already a backups directory then try to use that |
||
| 275 | if ( $this->get_existing_path() ) { |
||
| 276 | $paths[] = $this->get_existing_path(); |
||
| 277 | } |
||
| 278 | |||
| 279 | // If not then default to a new directory in wp-content |
||
| 280 | $paths[] = $this->get_default_path(); |
||
| 281 | |||
| 282 | // If that didn't work then fallback to a new directory in uploads |
||
| 283 | $paths[] = $this->get_fallback_path(); |
||
| 284 | |||
| 285 | // Loop through possible paths, use the first one that exists/can be created and is writable |
||
| 286 | foreach ( $paths as $path ) { |
||
| 287 | if ( wp_mkdir_p( $path ) && wp_is_writable( $path ) ) { // Also handles fixing perms / directory already exists |
||
| 288 | break; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | if ( isset( $path ) ) { |
||
| 293 | $this->path = $path; |
||
| 294 | } |
||
| 295 | |||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Protect the directory that backups are stored in |
||
| 300 | * |
||
| 301 | * - Adds an index.html file in an attempt to disable directory browsing |
||
| 302 | * - Adds a .httaccess file to deny direct access if on Apache |
||
| 303 | * |
||
| 304 | * @param string $reset |
||
| 305 | */ |
||
| 306 | public function protect_path( $reset = 'no' ) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * If we have more than one path then move any existing backups to the current path and remove them |
||
| 349 | */ |
||
| 350 | public function merge_existing_paths() { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Move backup files from an existing directory and the new |
||
| 364 | * location |
||
| 365 | * |
||
| 366 | * @param string $path The path to move the backups from |
||
| 367 | */ |
||
| 368 | public function move_old_backups( $from ) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Clean any temporary / incomplete backups from the backups directory |
||
| 412 | */ |
||
| 413 | public function cleanup() { |
||
| 431 | |||
| 432 | } |
||
| 433 | |||
| 441 |
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.