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 DNEnvironment 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 DNEnvironment, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 36 | class DNEnvironment extends DataObject { | 
            ||
| 37 | |||
| 38 | const UAT = 'UAT';  | 
            ||
| 39 | |||
| 40 | const PRODUCTION = 'Production';  | 
            ||
| 41 | |||
| 42 | const UNSPECIFIED = 'Unspecified';  | 
            ||
| 43 | |||
| 44 | /**  | 
            ||
| 45 | * @var array  | 
            ||
| 46 | */  | 
            ||
| 47 | public static $db = [  | 
            ||
| 48 | "Filename" => "Varchar(255)",  | 
            ||
| 49 | "Name" => "Varchar(255)",  | 
            ||
| 50 | "URL" => "Varchar(255)",  | 
            ||
| 51 | "BackendIdentifier" => "Varchar(255)", // Injector identifier of the DeploymentBackend  | 
            ||
| 52 | 		"Usage" => "Enum('Production, UAT, Test, Unspecified', 'Unspecified')", | 
            ||
| 53 | ];  | 
            ||
| 54 | |||
| 55 | /**  | 
            ||
| 56 | * @var array  | 
            ||
| 57 | */  | 
            ||
| 58 | public static $has_many = [  | 
            ||
| 59 | "Deployments" => "DNDeployment",  | 
            ||
| 60 | "DataArchives" => "DNDataArchive",  | 
            ||
| 61 | "DataTransfers" => "DNDataTransfer",  | 
            ||
| 62 | "Pings" => "DNPing"  | 
            ||
| 63 | ];  | 
            ||
| 64 | |||
| 65 | /**  | 
            ||
| 66 | * @var array  | 
            ||
| 67 | */  | 
            ||
| 68 | public static $many_many = [  | 
            ||
| 69 | "Viewers" => "Member", // Who can view this environment  | 
            ||
| 70 | "ViewerGroups" => "Group",  | 
            ||
| 71 | "Deployers" => "Member", // Who can deploy to this environment  | 
            ||
| 72 | "DeployerGroups" => "Group",  | 
            ||
| 73 | "CanRestoreMembers" => "Member", // Who can restore archive files to this environment  | 
            ||
| 74 | "CanRestoreGroups" => "Group",  | 
            ||
| 75 | "CanBackupMembers" => "Member", // Who can backup archive files from this environment  | 
            ||
| 76 | "CanBackupGroups" => "Group",  | 
            ||
| 77 | "ArchiveUploaders" => "Member", // Who can upload archive files linked to this environment  | 
            ||
| 78 | "ArchiveUploaderGroups" => "Group",  | 
            ||
| 79 | "ArchiveDownloaders" => "Member", // Who can download archive files from this environment  | 
            ||
| 80 | "ArchiveDownloaderGroups" => "Group",  | 
            ||
| 81 | "ArchiveDeleters" => "Member", // Who can delete archive files from this environment,  | 
            ||
| 82 | "ArchiveDeleterGroups" => "Group",  | 
            ||
| 83 | ];  | 
            ||
| 84 | |||
| 85 | /**  | 
            ||
| 86 | * @var array  | 
            ||
| 87 | */  | 
            ||
| 88 | public static $summary_fields = [  | 
            ||
| 89 | "Name" => "Environment Name",  | 
            ||
| 90 | "Usage" => "Usage",  | 
            ||
| 91 | "URL" => "URL",  | 
            ||
| 92 | "DeployersList" => "Can Deploy List",  | 
            ||
| 93 | "CanRestoreMembersList" => "Can Restore List",  | 
            ||
| 94 | "CanBackupMembersList" => "Can Backup List",  | 
            ||
| 95 | "ArchiveUploadersList" => "Can Upload List",  | 
            ||
| 96 | "ArchiveDownloadersList" => "Can Download List",  | 
            ||
| 97 | "ArchiveDeletersList" => "Can Delete List",  | 
            ||
| 98 | ];  | 
            ||
| 99 | |||
| 100 | /**  | 
            ||
| 101 | * @var array  | 
            ||
| 102 | */  | 
            ||
| 103 | public static $searchable_fields = [  | 
            ||
| 104 | "Name",  | 
            ||
| 105 | ];  | 
            ||
| 106 | |||
| 107 | private static $singular_name = 'Capistrano Environment';  | 
            ||
| 108 | |||
| 109 | private static $plural_name = 'Capistrano Environments';  | 
            ||
| 110 | |||
| 111 | /**  | 
            ||
| 112 | * @var string  | 
            ||
| 113 | */  | 
            ||
| 114 | private static $default_sort = 'Name';  | 
            ||
| 115 | |||
| 116 | /**  | 
            ||
| 117 | * @var array  | 
            ||
| 118 | */  | 
            ||
| 119 | public static $has_one = [  | 
            ||
| 120 | "Project" => "DNProject",  | 
            ||
| 121 | "CreateEnvironment" => "DNCreateEnvironment"  | 
            ||
| 122 | ];  | 
            ||
| 123 | |||
| 124 | /**  | 
            ||
| 125 | * If this is set to a full pathfile, it will be used as template  | 
            ||
| 126 | * file when creating a new capistrano environment config file.  | 
            ||
| 127 | *  | 
            ||
| 128 | * If not set, the default 'environment.template' from the module  | 
            ||
| 129 | * root is used  | 
            ||
| 130 | *  | 
            ||
| 131 | * @config  | 
            ||
| 132 | * @var string  | 
            ||
| 133 | */  | 
            ||
| 134 | private static $template_file = '';  | 
            ||
| 135 | |||
| 136 | /**  | 
            ||
| 137 | * Set this to true to allow editing of the environment files via the web admin  | 
            ||
| 138 | *  | 
            ||
| 139 | * @var bool  | 
            ||
| 140 | */  | 
            ||
| 141 | private static $allow_web_editing = false;  | 
            ||
| 142 | |||
| 143 | /**  | 
            ||
| 144 | * @var array  | 
            ||
| 145 | */  | 
            ||
| 146 | private static $casting = [  | 
            ||
| 147 | 'DeployHistory' => 'Text'  | 
            ||
| 148 | ];  | 
            ||
| 149 | |||
| 150 | /**  | 
            ||
| 151 | * Allowed backends. A map of Injector identifier to human-readable label.  | 
            ||
| 152 | *  | 
            ||
| 153 | * @config  | 
            ||
| 154 | * @var array  | 
            ||
| 155 | */  | 
            ||
| 156 | private static $allowed_backends = [];  | 
            ||
| 157 | |||
| 158 | /**  | 
            ||
| 159 | * Used by the sync task  | 
            ||
| 160 | *  | 
            ||
| 161 | * @param string $path  | 
            ||
| 162 | * @return \DNEnvironment  | 
            ||
| 163 | */  | 
            ||
| 164 | 	public static function create_from_path($path) { | 
            ||
| 174 | |||
| 175 | /**  | 
            ||
| 176 | * Get the deployment backend used for this environment.  | 
            ||
| 177 | *  | 
            ||
| 178 | * Enforces compliance with the allowed_backends setting; if the DNEnvironment.BackendIdentifier value is  | 
            ||
| 179 | * illegal then that value is ignored.  | 
            ||
| 180 | *  | 
            ||
| 181 | * @return DeploymentBackend  | 
            ||
| 182 | */  | 
            ||
| 183 | 	public function Backend() { | 
            ||
| 206 | |||
| 207 | /**  | 
            ||
| 208 | * @param SS_HTTPRequest $request  | 
            ||
| 209 | *  | 
            ||
| 210 | * @return DeploymentStrategy  | 
            ||
| 211 | */  | 
            ||
| 212 | 	public function getDeployStrategy(\SS_HTTPRequest $request) { | 
            ||
| 215 | |||
| 216 | /**  | 
            ||
| 217 | * Return the supported options for this environment.  | 
            ||
| 218 | * @return ArrayList  | 
            ||
| 219 | */  | 
            ||
| 220 | 	public function getSupportedOptions() { | 
            ||
| 223 | |||
| 224 | 	public function Menu() { | 
            ||
| 225 | $list = new ArrayList();  | 
            ||
| 226 | |||
| 227 | $controller = Controller::curr();  | 
            ||
| 228 | 		$actionType = $controller->getField('CurrentActionType'); | 
            ||
| 229 | |||
| 230 | $list->push(new ArrayData([  | 
            ||
| 231 | 'Link' => $this->DeploymentsLink(),  | 
            ||
| 232 | 'Title' => 'Deployments',  | 
            ||
| 233 | 'IsCurrent' => $this->isCurrent(),  | 
            ||
| 234 | 'IsSection' => $this->isSection() && ($actionType == DNRoot::ACTION_DEPLOY || $actionType == \EnvironmentOverview::ACTION_OVERVIEW)  | 
            ||
| 235 | ]));  | 
            ||
| 236 | |||
| 237 | 		$this->extend('updateMenu', $list); | 
            ||
| 238 | |||
| 239 | return $list;  | 
            ||
| 240 | }  | 
            ||
| 241 | |||
| 242 | /**  | 
            ||
| 243 | * Return the current object from $this->Menu()  | 
            ||
| 244 | * Good for making titles and things  | 
            ||
| 245 | */  | 
            ||
| 246 | 	public function CurrentMenu() { | 
            ||
| 249 | |||
| 250 | /**  | 
            ||
| 251 | * Return a name for this environment.  | 
            ||
| 252 | *  | 
            ||
| 253 | * @param string $separator The string used when concatenating project with env name  | 
            ||
| 254 | * @return string  | 
            ||
| 255 | */  | 
            ||
| 256 | 	public function getFullName($separator = ':') { | 
            ||
| 259 | |||
| 260 | /**  | 
            ||
| 261 | * URL for the environment that can be used if no explicit URL is set.  | 
            ||
| 262 | */  | 
            ||
| 263 | 	public function getDefaultURL() { | 
            ||
| 266 | |||
| 267 | 	public function getBareURL() { | 
            ||
| 273 | |||
| 274 | 	public function getBareDefaultURL() { | 
            ||
| 280 | |||
| 281 | /**  | 
            ||
| 282 | * Environments are only viewable by people that can view the environment.  | 
            ||
| 283 | *  | 
            ||
| 284 | * @param Member|null $member  | 
            ||
| 285 | * @return boolean  | 
            ||
| 286 | */  | 
            ||
| 287 | 	public function canView($member = null) { | 
            ||
| 308 | |||
| 309 | /**  | 
            ||
| 310 | * Allow deploy only to some people.  | 
            ||
| 311 | *  | 
            ||
| 312 | * @param Member|null $member  | 
            ||
| 313 | * @return boolean  | 
            ||
| 314 | */  | 
            ||
| 315 | View Code Duplication | 	public function canDeploy($member = null) { | 
            |
| 337 | |||
| 338 | /**  | 
            ||
| 339 | * Provide reason why the user cannot deploy.  | 
            ||
| 340 | *  | 
            ||
| 341 | * @return string  | 
            ||
| 342 | */  | 
            ||
| 343 | 	public function getCannotDeployMessage() { | 
            ||
| 346 | |||
| 347 | /**  | 
            ||
| 348 | 	 * Allows only selected {@link Member} objects to restore {@link DNDataArchive} objects into this | 
            ||
| 349 | 	 * {@link DNEnvironment}. | 
            ||
| 350 | *  | 
            ||
| 351 | 	 * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); | 
            ||
| 352 | * @return boolean true if $member can restore, and false if they can't.  | 
            ||
| 353 | */  | 
            ||
| 354 | View Code Duplication | 	public function canRestore($member = null) { | 
            |
| 376 | |||
| 377 | /**  | 
            ||
| 378 | 	 * Allows only selected {@link Member} objects to backup this {@link DNEnvironment} to a {@link DNDataArchive} | 
            ||
| 379 | * file.  | 
            ||
| 380 | *  | 
            ||
| 381 | 	 * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); | 
            ||
| 382 | * @return boolean true if $member can backup, and false if they can't.  | 
            ||
| 383 | */  | 
            ||
| 384 | View Code Duplication | 	public function canBackup($member = null) { | 
            |
| 411 | |||
| 412 | /**  | 
            ||
| 413 | 	 * Allows only selected {@link Member} objects to upload {@link DNDataArchive} objects linked to this | 
            ||
| 414 | 	 * {@link DNEnvironment}. | 
            ||
| 415 | *  | 
            ||
| 416 | * Note: This is not uploading them to the actual environment itself (e.g. uploading to the live site) - it is the  | 
            ||
| 417 | * process of uploading a *.sspak file into Deploynaut for later 'restoring' to an environment. See  | 
            ||
| 418 | 	 * {@link self::canRestore()}. | 
            ||
| 419 | *  | 
            ||
| 420 | 	 * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); | 
            ||
| 421 | * @return boolean true if $member can upload archives linked to this environment, false if they can't.  | 
            ||
| 422 | */  | 
            ||
| 423 | View Code Duplication | 	public function canUploadArchive($member = null) { | 
            |
| 450 | |||
| 451 | /**  | 
            ||
| 452 | 	 * Allows only selected {@link Member} objects to download {@link DNDataArchive} objects from this | 
            ||
| 453 | 	 * {@link DNEnvironment}. | 
            ||
| 454 | *  | 
            ||
| 455 | 	 * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); | 
            ||
| 456 | * @return boolean true if $member can download archives from this environment, false if they can't.  | 
            ||
| 457 | */  | 
            ||
| 458 | View Code Duplication | 	public function canDownloadArchive($member = null) { | 
            |
| 480 | |||
| 481 | /**  | 
            ||
| 482 | 	 * Allows only selected {@link Member} objects to delete {@link DNDataArchive} objects from this | 
            ||
| 483 | 	 * {@link DNEnvironment}. | 
            ||
| 484 | *  | 
            ||
| 485 | 	 * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); | 
            ||
| 486 | * @return boolean true if $member can delete archives from this environment, false if they can't.  | 
            ||
| 487 | */  | 
            ||
| 488 | View Code Duplication | 	public function canDeleteArchive($member = null) { | 
            |
| 510 | |||
| 511 | /**  | 
            ||
| 512 | * Get a string of groups/people that are allowed to deploy to this environment.  | 
            ||
| 513 | 	 * Used in DNRoot_project.ss to list {@link Member}s who have permission to perform this action. | 
            ||
| 514 | *  | 
            ||
| 515 | * @return string  | 
            ||
| 516 | */  | 
            ||
| 517 | 	public function getDeployersList() { | 
            ||
| 526 | |||
| 527 | /**  | 
            ||
| 528 | 	 * Get a string of groups/people that are allowed to restore {@link DNDataArchive} objects into this environment. | 
            ||
| 529 | *  | 
            ||
| 530 | * @return string  | 
            ||
| 531 | */  | 
            ||
| 532 | 	public function getCanRestoreMembersList() { | 
            ||
| 541 | |||
| 542 | /**  | 
            ||
| 543 | 	 * Get a string of groups/people that are allowed to backup {@link DNDataArchive} objects from this environment. | 
            ||
| 544 | *  | 
            ||
| 545 | * @return string  | 
            ||
| 546 | */  | 
            ||
| 547 | 	public function getCanBackupMembersList() { | 
            ||
| 556 | |||
| 557 | /**  | 
            ||
| 558 | 	 * Get a string of groups/people that are allowed to upload {@link DNDataArchive} | 
            ||
| 559 | * objects linked to this environment.  | 
            ||
| 560 | *  | 
            ||
| 561 | * @return string  | 
            ||
| 562 | */  | 
            ||
| 563 | 	public function getArchiveUploadersList() { | 
            ||
| 572 | |||
| 573 | /**  | 
            ||
| 574 | 	 * Get a string of groups/people that are allowed to download {@link DNDataArchive} objects from this environment. | 
            ||
| 575 | *  | 
            ||
| 576 | * @return string  | 
            ||
| 577 | */  | 
            ||
| 578 | 	public function getArchiveDownloadersList() { | 
            ||
| 587 | |||
| 588 | /**  | 
            ||
| 589 | 	 * Get a string of groups/people that are allowed to delete {@link DNDataArchive} objects from this environment. | 
            ||
| 590 | *  | 
            ||
| 591 | * @return string  | 
            ||
| 592 | */  | 
            ||
| 593 | 	public function getArchiveDeletersList() { | 
            ||
| 602 | |||
| 603 | /**  | 
            ||
| 604 | * @return DNData  | 
            ||
| 605 | */  | 
            ||
| 606 | 	public function DNData() { | 
            ||
| 609 | |||
| 610 | /**  | 
            ||
| 611 | * Get the current deployed build for this environment  | 
            ||
| 612 | *  | 
            ||
| 613 | * Dear people of the future: If you are looking to optimize this, simply create a CurrentBuildSHA(), which can be  | 
            ||
| 614 | * a lot faster. I presume you came here because of the Project display template, which only needs a SHA.  | 
            ||
| 615 | *  | 
            ||
| 616 | * @return false|DNDeployment  | 
            ||
| 617 | */  | 
            ||
| 618 | 	public function CurrentBuild() { | 
            ||
| 651 | |||
| 652 | /**  | 
            ||
| 653 | * This is a proxy call to gitonmy that caches the information per project and sha  | 
            ||
| 654 | *  | 
            ||
| 655 | * @param string $sha  | 
            ||
| 656 | * @return \Gitonomy\Git\Commit  | 
            ||
| 657 | */  | 
            ||
| 658 | 	public function getCommit($sha) { | 
            ||
| 661 | |||
| 662 | 	public function getCommitMessage(\Gitonomy\Git\Commit $commit) { | 
            ||
| 665 | |||
| 666 | 	public function getCommitTags(\Gitonomy\Git\Commit $commit) { | 
            ||
| 669 | |||
| 670 | /**  | 
            ||
| 671 | * A list of past deployments.  | 
            ||
| 672 | * @param string $orderBy - the name of a DB column to sort in descending order  | 
            ||
| 673 | * @return \ArrayList  | 
            ||
| 674 | */  | 
            ||
| 675 | 	public function DeployHistory($orderBy = '') { | 
            ||
| 692 | |||
| 693 | /**  | 
            ||
| 694 | * A list of upcoming or current deployments.  | 
            ||
| 695 | * @return ArrayList  | 
            ||
| 696 | */  | 
            ||
| 697 | 	public function UpcomingDeployments() { | 
            ||
| 711 | |||
| 712 | /**  | 
            ||
| 713 | * This provides the link to the deployments depending on whether  | 
            ||
| 714 | * the feature flag for the new deployment is enabled.  | 
            ||
| 715 | *  | 
            ||
| 716 | * @return string  | 
            ||
| 717 | */  | 
            ||
| 718 | 	public function DeploymentsLink() { | 
            ||
| 724 | |||
| 725 | /**  | 
            ||
| 726 | * @param string $action  | 
            ||
| 727 | *  | 
            ||
| 728 | * @return string  | 
            ||
| 729 | */  | 
            ||
| 730 | 	public function Link($action = '') { | 
            ||
| 733 | |||
| 734 | /**  | 
            ||
| 735 | * Is this environment currently at the root level of the controller that handles it?  | 
            ||
| 736 | * @return bool  | 
            ||
| 737 | */  | 
            ||
| 738 | 	public function isCurrent() { | 
            ||
| 741 | |||
| 742 | /**  | 
            ||
| 743 | * Is this environment currently in a controller that is handling it or performing a sub-task?  | 
            ||
| 744 | * @return bool  | 
            ||
| 745 | */  | 
            ||
| 746 | 	public function isSection() { | 
            ||
| 751 | |||
| 752 | /**  | 
            ||
| 753 | * @return FieldList  | 
            ||
| 754 | */  | 
            ||
| 755 | 	public function getCMSFields() { | 
            ||
| 904 | |||
| 905 | /**  | 
            ||
| 906 | */  | 
            ||
| 907 | 	public function onBeforeWrite() { | 
            ||
| 915 | |||
| 916 | 	public function onAfterWrite() { | 
            ||
| 931 | |||
| 932 | /**  | 
            ||
| 933 | * Delete any related config files  | 
            ||
| 934 | */  | 
            ||
| 935 | 	public function onAfterDelete() { | 
            ||
| 976 | |||
| 977 | /**  | 
            ||
| 978 | * Returns the path to the ruby config file  | 
            ||
| 979 | *  | 
            ||
| 980 | * @return string  | 
            ||
| 981 | */  | 
            ||
| 982 | 	public function getConfigFilename() { | 
            ||
| 991 | |||
| 992 | /**  | 
            ||
| 993 | 	 * Helper function to convert a multi-dimensional array (associative or indexed) to an {@link ArrayList} or | 
            ||
| 994 | 	 * {@link ArrayData} object structure, so that values can be used in templates. | 
            ||
| 995 | *  | 
            ||
| 996 | * @param array $array The (single- or multi-dimensional) array to convert  | 
            ||
| 997 | 	 * @return object Either an {@link ArrayList} or {@link ArrayData} object, or the original item ($array) if $array | 
            ||
| 998 | * isn't an array.  | 
            ||
| 999 | */  | 
            ||
| 1000 | 	public static function array_to_viewabledata($array) { | 
            ||
| 1025 | |||
| 1026 | /**  | 
            ||
| 1027 | * Fetchs all deployments in progress. Limits to 1 hour to prevent deployments  | 
            ||
| 1028 | * if an old deployment is stuck.  | 
            ||
| 1029 | *  | 
            ||
| 1030 | * @return DataList  | 
            ||
| 1031 | */  | 
            ||
| 1032 | 	public function runningDeployments() { | 
            ||
| 1044 | |||
| 1045 | /**  | 
            ||
| 1046 | * @param string $sha  | 
            ||
| 1047 | * @return array  | 
            ||
| 1048 | */  | 
            ||
| 1049 | 	protected function getCommitData($sha) { | 
            ||
| 1073 | |||
| 1074 | /**  | 
            ||
| 1075 | * Build a set of multi-select fields for assigning permissions to a pair of group and member many_many relations  | 
            ||
| 1076 | *  | 
            ||
| 1077 | * @param string $groupField Group field name  | 
            ||
| 1078 | * @param string $memberField Member field name  | 
            ||
| 1079 | * @param array $groups List of groups  | 
            ||
| 1080 | * @param array $members List of members  | 
            ||
| 1081 | * @return FieldGroup  | 
            ||
| 1082 | */  | 
            ||
| 1083 | 	protected function buildPermissionField($groupField, $memberField, $groups, $members) { | 
            ||
| 1098 | |||
| 1099 | /**  | 
            ||
| 1100 | * @param FieldList $fields  | 
            ||
| 1101 | */  | 
            ||
| 1102 | 	protected function setDeployConfigurationFields(&$fields) { | 
            ||
| 1122 | |||
| 1123 | /**  | 
            ||
| 1124 | * Ensure that environment paths are setup on the local filesystem  | 
            ||
| 1125 | */  | 
            ||
| 1126 | 	protected function checkEnvironmentPath() { | 
            ||
| 1133 | |||
| 1134 | /**  | 
            ||
| 1135 | * Write the deployment config file to filesystem  | 
            ||
| 1136 | */  | 
            ||
| 1137 | 	protected function writeConfigFile() { | 
            ||
| 1153 | |||
| 1154 | /**  | 
            ||
| 1155 | * @return string  | 
            ||
| 1156 | */  | 
            ||
| 1157 | 	protected function getEnvironmentConfig() { | 
            ||
| 1163 | |||
| 1164 | /**  | 
            ||
| 1165 | * @return boolean  | 
            ||
| 1166 | */  | 
            ||
| 1167 | 	protected function envFileExists() { | 
            ||
| 1173 | |||
| 1174 | 	protected function validate() { | 
            ||
| 1184 | |||
| 1185 | }  | 
            ||
| 1186 | 
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.