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 DNDataArchive 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 DNDataArchive, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class DNDataArchive extends DataObject { |
||
| 50 | |||
| 51 | private static $db = array( |
||
| 52 | 'UploadToken' => 'Varchar(8)', |
||
| 53 | "Mode" => "Enum('all, assets, db', '')", |
||
| 54 | "IsBackup" => "Boolean", |
||
| 55 | "IsManualUpload" => "Boolean", |
||
| 56 | ); |
||
| 57 | |||
| 58 | private static $has_one = array( |
||
| 59 | 'Author' => 'Member', |
||
| 60 | 'OriginalEnvironment' => 'DNEnvironment', |
||
| 61 | 'Environment' => 'DNEnvironment', |
||
| 62 | 'ArchiveFile' => 'File' |
||
| 63 | ); |
||
| 64 | |||
| 65 | private static $has_many = array( |
||
| 66 | 'DataTransfers' => 'DNDataTransfer', |
||
| 67 | ); |
||
| 68 | |||
| 69 | private static $singular_name = 'Data Archive'; |
||
| 70 | |||
| 71 | private static $plural_name = 'Data Archives'; |
||
| 72 | |||
| 73 | private static $summary_fields = array( |
||
| 74 | 'Created' => 'Created', |
||
| 75 | 'Author.Title' => 'Author', |
||
| 76 | 'Environment.Project.Name' => 'Project', |
||
| 77 | 'OriginalEnvironment.Name' => 'Origin', |
||
| 78 | 'Environment.Name' => 'Environment', |
||
| 79 | 'ArchiveFile.Name' => 'File', |
||
| 80 | ); |
||
| 81 | |||
| 82 | private static $searchable_fields = array( |
||
| 83 | 'Environment.Project.Name' => array( |
||
| 84 | 'title' => 'Project', |
||
| 85 | ), |
||
| 86 | 'OriginalEnvironment.Name' => array( |
||
| 87 | 'title' => 'Origin', |
||
| 88 | ), |
||
| 89 | 'Environment.Name' => array( |
||
| 90 | 'title' => 'Environment', |
||
| 91 | ), |
||
| 92 | 'UploadToken' => array( |
||
| 93 | 'title' => 'Upload Token', |
||
| 94 | ), |
||
| 95 | 'Mode' => array( |
||
| 96 | 'title' => 'Mode', |
||
| 97 | ), |
||
| 98 | ); |
||
| 99 | |||
| 100 | private static $_cache_can_restore = array(); |
||
| 101 | |||
| 102 | private static $_cache_can_download = array(); |
||
| 103 | |||
| 104 | public static function get_mode_map() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Returns a unique token to correlate an offline item (posted DVD) |
||
| 114 | * with a specific archive placeholder. |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public static function generate_upload_token($chars = 8) { |
||
| 122 | |||
| 123 | public function onBeforeWrite() { |
||
| 130 | |||
| 131 | public function onAfterDelete() { |
||
| 137 | |||
| 138 | public function getCMSFields() { |
||
| 170 | |||
| 171 | public function getDefaultSearchContext() { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Calculates and returns a human-readable size of this archive file. If the file exists, it will determine |
||
| 180 | * whether to display the output in bytes, kilobytes, megabytes, or gigabytes. |
||
| 181 | * |
||
| 182 | * @return string The human-readable size of this archive file |
||
| 183 | */ |
||
| 184 | public function FileSize() { |
||
| 191 | |||
| 192 | public function getModeNice() { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Some archives don't have files attached to them yet, |
||
| 202 | * because a file has been posted offline and is waiting to be uploaded |
||
| 203 | * against this "archive placeholder". |
||
| 204 | * |
||
| 205 | * @return boolean |
||
| 206 | */ |
||
| 207 | public function isPending() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Inferred from both restore and backup permissions. |
||
| 213 | * |
||
| 214 | * @param Member|null $member The {@link Member} object to test against. |
||
| 215 | */ |
||
| 216 | public function canView($member = null) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Whether a {@link Member} can restore this archive to an environment. |
||
| 222 | * This only needs to be checked *once* per member and environment. |
||
| 223 | * |
||
| 224 | * @param Member|null $member The {@link Member} object to test against. |
||
| 225 | * @return true if $member (or the currently logged in member if null) can upload this archive |
||
| 226 | */ |
||
| 227 | View Code Duplication | public function canRestore($member = null) { |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Whether a {@link Member} can download this archive to their PC. |
||
| 243 | * This only needs to be checked *once* per member and environment. |
||
| 244 | * |
||
| 245 | * @param Member|null $member The {@link Member} object to test against. |
||
| 246 | * @return true if $member (or the currently logged in member if null) can download this archive |
||
| 247 | */ |
||
| 248 | View Code Duplication | public function canDownload($member = null) { |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Whether a {@link Member} can delete this archive from staging area. |
||
| 263 | * |
||
| 264 | * @param Member|null $member The {@link Member} object to test against. |
||
| 265 | * @return boolean if $member (or the currently logged in member if null) can delete this archive |
||
| 266 | */ |
||
| 267 | public function canDelete($member = null) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Check if this member can move archive into the environment. |
||
| 273 | * |
||
| 274 | * @param DNEnvironment $targetEnv Environment to check. |
||
| 275 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 276 | * |
||
| 277 | * @return boolean true if $member can upload archives linked to this environment, false if they can't. |
||
| 278 | */ |
||
| 279 | public function canMoveTo($targetEnv, $member = null) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Finds all environments within this project where the archive can be moved to. |
||
| 312 | * Excludes current environment automatically. |
||
| 313 | * |
||
| 314 | * @return ArrayList List of valid environments. |
||
| 315 | */ |
||
| 316 | public function validTargetEnvironments() { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Returns a unique filename, including project/environment/timestamp details. |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public function generateFilename(\DNDataTransfer $dataTransfer) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Returns a path unique to a specific transfer, including project/environment details. |
||
| 346 | * Does not create the path on the filesystem. Can be used to store files related to this transfer. |
||
| 347 | * |
||
| 348 | * @param DNDataTransfer |
||
| 349 | * @return string Absolute file path |
||
| 350 | */ |
||
| 351 | public function generateFilepath(\DNDataTransfer $dataTransfer) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Attach an sspak file path to this archive and associate the transfer. |
||
| 366 | * Does the job of creating a {@link File} record, and setting correct paths into the assets directory. |
||
| 367 | * |
||
| 368 | * @param string $sspakFilepath |
||
| 369 | * @param DNDataTransfer $dataTransfer |
||
| 370 | * @return bool |
||
| 371 | */ |
||
| 372 | public function attachFile($sspakFilepath, DNDataTransfer $dataTransfer) { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Extract the current sspak contents into the given working directory. |
||
| 401 | * This also extracts the assets and database and puts them into |
||
| 402 | * <workingdir>/database.sql and <workingdir>/assets, respectively. |
||
| 403 | * |
||
| 404 | * @param string|null $workingDir The path to extract to |
||
| 405 | * @throws RuntimeException |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | public function extractArchive($workingDir = null) { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Validate that an sspak contains the correct content. |
||
| 460 | * |
||
| 461 | * For example, if the user uploaded an sspak containing just the db, but declared in the form |
||
| 462 | * that it contained db+assets, then the archive is not valid. |
||
| 463 | * |
||
| 464 | * @param string|null $mode "db", "assets", or "all". This is the content we're checking for. Default to the archive setting |
||
| 465 | * @return ValidationResult |
||
| 466 | */ |
||
| 467 | public function validateArchiveContents($mode = null) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Given a path that already exists and contains an extracted sspak, including |
||
| 503 | * the assets, fix all of the file permissions so they're in a state ready to |
||
| 504 | * be pushed to remote servers. |
||
| 505 | * |
||
| 506 | * Normally, command line tar will use permissions found in the archive, but will |
||
| 507 | * substract the user's umask from them. This has a potential to create unreadable |
||
| 508 | * files, e.g. cygwin on Windows will pack files with mode 000, hence why this fix |
||
| 509 | * is necessary. |
||
| 510 | * |
||
| 511 | * @param string|null $workingDir The path of where the sspak has been extracted to |
||
| 512 | * @throws RuntimeException |
||
| 513 | * @return bool |
||
| 514 | */ |
||
| 515 | public function fixArchivePermissions($workingDir) { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Given extracted sspak contents, create an sspak from it |
||
| 537 | * and overwrite the current ArchiveFile with it's contents. |
||
| 538 | * Use GZIP=-1 for less compression on assets, which are already |
||
| 539 | * heavily compressed to begin with. |
||
| 540 | * |
||
| 541 | * @param string|null $workingDir The path of where the sspak has been extracted to |
||
| 542 | * @return bool |
||
| 543 | */ |
||
| 544 | public function setArchiveFromFiles($workingDir) { |
||
| 576 | |||
| 577 | } |
||
| 578 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.