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 | public function Menu() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Return the current object from $this->Menu() |
||
| 236 | * Good for making titles and things |
||
| 237 | */ |
||
| 238 | public function CurrentMenu() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Return a name for this environment. |
||
| 244 | * |
||
| 245 | * @param string $separator The string used when concatenating project with env name |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | public function getFullName($separator = ':') { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * URL for the environment that can be used if no explicit URL is set. |
||
| 254 | */ |
||
| 255 | public function getDefaultURL() { |
||
| 258 | |||
| 259 | public function getBareURL() { |
||
| 265 | |||
| 266 | public function getBareDefaultURL() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Environments are only viewable by people that can view the environment. |
||
| 275 | * |
||
| 276 | * @param Member|null $member |
||
| 277 | * @return boolean |
||
| 278 | */ |
||
| 279 | public function canView($member = null) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Allow deploy only to some people. |
||
| 303 | * |
||
| 304 | * @param Member|null $member |
||
| 305 | * @return boolean |
||
| 306 | */ |
||
| 307 | View Code Duplication | public function canDeploy($member = null) { |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Provide reason why the user cannot deploy. |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public function getCannotDeployMessage() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Allows only selected {@link Member} objects to restore {@link DNDataArchive} objects into this |
||
| 341 | * {@link DNEnvironment}. |
||
| 342 | * |
||
| 343 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 344 | * @return boolean true if $member can restore, and false if they can't. |
||
| 345 | */ |
||
| 346 | View Code Duplication | public function canRestore($member = null) { |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Allows only selected {@link Member} objects to backup this {@link DNEnvironment} to a {@link DNDataArchive} |
||
| 371 | * file. |
||
| 372 | * |
||
| 373 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 374 | * @return boolean true if $member can backup, and false if they can't. |
||
| 375 | */ |
||
| 376 | View Code Duplication | public function canBackup($member = null) { |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Allows only selected {@link Member} objects to upload {@link DNDataArchive} objects linked to this |
||
| 406 | * {@link DNEnvironment}. |
||
| 407 | * |
||
| 408 | * Note: This is not uploading them to the actual environment itself (e.g. uploading to the live site) - it is the |
||
| 409 | * process of uploading a *.sspak file into Deploynaut for later 'restoring' to an environment. See |
||
| 410 | * {@link self::canRestore()}. |
||
| 411 | * |
||
| 412 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 413 | * @return boolean true if $member can upload archives linked to this environment, false if they can't. |
||
| 414 | */ |
||
| 415 | View Code Duplication | public function canUploadArchive($member = null) { |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Allows only selected {@link Member} objects to download {@link DNDataArchive} objects from this |
||
| 445 | * {@link DNEnvironment}. |
||
| 446 | * |
||
| 447 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 448 | * @return boolean true if $member can download archives from this environment, false if they can't. |
||
| 449 | */ |
||
| 450 | View Code Duplication | public function canDownloadArchive($member = null) { |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Allows only selected {@link Member} objects to delete {@link DNDataArchive} objects from this |
||
| 475 | * {@link DNEnvironment}. |
||
| 476 | * |
||
| 477 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 478 | * @return boolean true if $member can delete archives from this environment, false if they can't. |
||
| 479 | */ |
||
| 480 | View Code Duplication | public function canDeleteArchive($member = null) { |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Get a string of groups/people that are allowed to deploy to this environment. |
||
| 505 | * Used in DNRoot_project.ss to list {@link Member}s who have permission to perform this action. |
||
| 506 | * |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | public function getDeployersList() { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Get a string of groups/people that are allowed to restore {@link DNDataArchive} objects into this environment. |
||
| 521 | * |
||
| 522 | * @return string |
||
| 523 | */ |
||
| 524 | public function getCanRestoreMembersList() { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Get a string of groups/people that are allowed to backup {@link DNDataArchive} objects from this environment. |
||
| 536 | * |
||
| 537 | * @return string |
||
| 538 | */ |
||
| 539 | public function getCanBackupMembersList() { |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Get a string of groups/people that are allowed to upload {@link DNDataArchive} |
||
| 551 | * objects linked to this environment. |
||
| 552 | * |
||
| 553 | * @return string |
||
| 554 | */ |
||
| 555 | public function getArchiveUploadersList() { |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Get a string of groups/people that are allowed to download {@link DNDataArchive} objects from this environment. |
||
| 567 | * |
||
| 568 | * @return string |
||
| 569 | */ |
||
| 570 | public function getArchiveDownloadersList() { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Get a string of groups/people that are allowed to delete {@link DNDataArchive} objects from this environment. |
||
| 582 | * |
||
| 583 | * @return string |
||
| 584 | */ |
||
| 585 | public function getArchiveDeletersList() { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @return DNData |
||
| 597 | */ |
||
| 598 | public function DNData() { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Get the current deployed build for this environment |
||
| 604 | * |
||
| 605 | * Dear people of the future: If you are looking to optimize this, simply create a CurrentBuildSHA(), which can be |
||
| 606 | * a lot faster. I presume you came here because of the Project display template, which only needs a SHA. |
||
| 607 | * |
||
| 608 | * @return false|DNDeployment |
||
| 609 | */ |
||
| 610 | public function CurrentBuild() { |
||
| 643 | |||
| 644 | /** |
||
| 645 | * This is a proxy call to gitonmy that caches the information per project and sha |
||
| 646 | * |
||
| 647 | * @param string $sha |
||
| 648 | * @return \Gitonomy\Git\Commit |
||
| 649 | */ |
||
| 650 | public function getCommit($sha) { |
||
| 653 | |||
| 654 | public function getCommitMessage(\Gitonomy\Git\Commit $commit) { |
||
| 657 | |||
| 658 | public function getCommitTags(\Gitonomy\Git\Commit $commit) { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * A list of past deployments. |
||
| 664 | * @param string $orderBy - the name of a DB column to sort in descending order |
||
| 665 | * @return \ArrayList |
||
| 666 | */ |
||
| 667 | public function DeployHistory($orderBy = '') { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * A list of upcoming or current deployments. |
||
| 687 | * @return ArrayList |
||
| 688 | */ |
||
| 689 | public function UpcomingDeployments() { |
||
| 703 | |||
| 704 | /** |
||
| 705 | * @param string $action |
||
| 706 | * |
||
| 707 | * @return string |
||
| 708 | */ |
||
| 709 | public function Link($action = '') { |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Is this environment currently at the root level of the controller that handles it? |
||
| 715 | * @return bool |
||
| 716 | */ |
||
| 717 | public function isCurrent() { |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Is this environment currently in a controller that is handling it or performing a sub-task? |
||
| 723 | * @return bool |
||
| 724 | */ |
||
| 725 | public function isSection() { |
||
| 730 | |||
| 731 | /** |
||
| 732 | * @return FieldList |
||
| 733 | */ |
||
| 734 | public function getCMSFields() { |
||
| 883 | |||
| 884 | /** |
||
| 885 | */ |
||
| 886 | public function onBeforeWrite() { |
||
| 894 | |||
| 895 | public function onAfterWrite() { |
||
| 910 | |||
| 911 | /** |
||
| 912 | * Delete any related config files |
||
| 913 | */ |
||
| 914 | public function onAfterDelete() { |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Returns the path to the ruby config file |
||
| 958 | * |
||
| 959 | * @return string |
||
| 960 | */ |
||
| 961 | public function getConfigFilename() { |
||
| 970 | |||
| 971 | /** |
||
| 972 | * Helper function to convert a multi-dimensional array (associative or indexed) to an {@link ArrayList} or |
||
| 973 | * {@link ArrayData} object structure, so that values can be used in templates. |
||
| 974 | * |
||
| 975 | * @param array $array The (single- or multi-dimensional) array to convert |
||
| 976 | * @return object Either an {@link ArrayList} or {@link ArrayData} object, or the original item ($array) if $array |
||
| 977 | * isn't an array. |
||
| 978 | */ |
||
| 979 | public static function array_to_viewabledata($array) { |
||
| 1004 | |||
| 1005 | /** |
||
| 1006 | * Fetchs all deployments in progress. Limits to 1 hour to prevent deployments |
||
| 1007 | * if an old deployment is stuck. |
||
| 1008 | * |
||
| 1009 | * @return DataList |
||
| 1010 | */ |
||
| 1011 | public function runningDeployments() { |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * @param string $sha |
||
| 1026 | * @return array |
||
| 1027 | */ |
||
| 1028 | protected function getCommitData($sha) { |
||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * Build a set of multi-select fields for assigning permissions to a pair of group and member many_many relations |
||
| 1055 | * |
||
| 1056 | * @param string $groupField Group field name |
||
| 1057 | * @param string $memberField Member field name |
||
| 1058 | * @param array $groups List of groups |
||
| 1059 | * @param array $members List of members |
||
| 1060 | * @return FieldGroup |
||
| 1061 | */ |
||
| 1062 | protected function buildPermissionField($groupField, $memberField, $groups, $members) { |
||
| 1077 | |||
| 1078 | /** |
||
| 1079 | * @param FieldList $fields |
||
| 1080 | */ |
||
| 1081 | protected function setDeployConfigurationFields(&$fields) { |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Ensure that environment paths are setup on the local filesystem |
||
| 1104 | */ |
||
| 1105 | protected function checkEnvironmentPath() { |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * Write the deployment config file to filesystem |
||
| 1115 | */ |
||
| 1116 | protected function writeConfigFile() { |
||
| 1132 | |||
| 1133 | /** |
||
| 1134 | * @return string |
||
| 1135 | */ |
||
| 1136 | protected function getEnvironmentConfig() { |
||
| 1142 | |||
| 1143 | /** |
||
| 1144 | * @return boolean |
||
| 1145 | */ |
||
| 1146 | protected function envFileExists() { |
||
| 1152 | |||
| 1153 | protected function validate() { |
||
| 1163 | |||
| 1164 | } |
||
| 1165 |
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.