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 | ]; |
||
62 | |||
63 | /** |
||
64 | * @var array |
||
65 | */ |
||
66 | public static $many_many = [ |
||
67 | "Viewers" => "Member", // Who can view this environment |
||
68 | "ViewerGroups" => "Group", |
||
69 | "Deployers" => "Member", // Who can deploy to this environment |
||
70 | "DeployerGroups" => "Group", |
||
71 | "CanRestoreMembers" => "Member", // Who can restore archive files to this environment |
||
72 | "CanRestoreGroups" => "Group", |
||
73 | "CanBackupMembers" => "Member", // Who can backup archive files from this environment |
||
74 | "CanBackupGroups" => "Group", |
||
75 | "ArchiveUploaders" => "Member", // Who can upload archive files linked to this environment |
||
76 | "ArchiveUploaderGroups" => "Group", |
||
77 | "ArchiveDownloaders" => "Member", // Who can download archive files from this environment |
||
78 | "ArchiveDownloaderGroups" => "Group", |
||
79 | "ArchiveDeleters" => "Member", // Who can delete archive files from this environment, |
||
80 | "ArchiveDeleterGroups" => "Group", |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * @var array |
||
85 | */ |
||
86 | public static $summary_fields = [ |
||
87 | "Name" => "Environment Name", |
||
88 | "Usage" => "Usage", |
||
89 | "URL" => "URL", |
||
90 | "DeployersList" => "Can Deploy List", |
||
91 | "CanRestoreMembersList" => "Can Restore List", |
||
92 | "CanBackupMembersList" => "Can Backup List", |
||
93 | "ArchiveUploadersList" => "Can Upload List", |
||
94 | "ArchiveDownloadersList" => "Can Download List", |
||
95 | "ArchiveDeletersList" => "Can Delete List", |
||
96 | ]; |
||
97 | |||
98 | /** |
||
99 | * @var array |
||
100 | */ |
||
101 | public static $searchable_fields = [ |
||
102 | "Name", |
||
103 | ]; |
||
104 | |||
105 | private static $singular_name = 'Capistrano Environment'; |
||
106 | |||
107 | private static $plural_name = 'Capistrano Environments'; |
||
108 | |||
109 | /** |
||
110 | * @var string |
||
111 | */ |
||
112 | private static $default_sort = 'Name'; |
||
113 | |||
114 | /** |
||
115 | * @var array |
||
116 | */ |
||
117 | public static $has_one = [ |
||
118 | "Project" => "DNProject", |
||
119 | "CreateEnvironment" => "DNCreateEnvironment" |
||
120 | ]; |
||
121 | |||
122 | /** |
||
123 | * If this is set to a full pathfile, it will be used as template |
||
124 | * file when creating a new capistrano environment config file. |
||
125 | * |
||
126 | * If not set, the default 'environment.template' from the module |
||
127 | * root is used |
||
128 | * |
||
129 | * @config |
||
130 | * @var string |
||
131 | */ |
||
132 | private static $template_file = ''; |
||
133 | |||
134 | /** |
||
135 | * Set this to true to allow editing of the environment files via the web admin |
||
136 | * |
||
137 | * @var bool |
||
138 | */ |
||
139 | private static $allow_web_editing = false; |
||
140 | |||
141 | /** |
||
142 | * @var array |
||
143 | */ |
||
144 | private static $casting = [ |
||
145 | 'DeployHistory' => 'Text' |
||
146 | ]; |
||
147 | |||
148 | /** |
||
149 | * Allowed backends. A map of Injector identifier to human-readable label. |
||
150 | * |
||
151 | * @config |
||
152 | * @var array |
||
153 | */ |
||
154 | private static $allowed_backends = []; |
||
155 | |||
156 | /** |
||
157 | * Used by the sync task |
||
158 | * |
||
159 | * @param string $path |
||
160 | * @return \DNEnvironment |
||
161 | */ |
||
162 | public static function create_from_path($path) { |
||
172 | |||
173 | /** |
||
174 | * Get the deployment backend used for this environment. |
||
175 | * |
||
176 | * Enforces compliance with the allowed_backends setting; if the DNEnvironment.BackendIdentifier value is |
||
177 | * illegal then that value is ignored. |
||
178 | * |
||
179 | * @return DeploymentBackend |
||
180 | */ |
||
181 | public function Backend() { |
||
182 | $backends = array_keys($this->config()->get('allowed_backends', Config::FIRST_SET)); |
||
183 | switch (sizeof($backends)) { |
||
184 | // Nothing allowed, use the default value "DeploymentBackend" |
||
185 | case 0: |
||
186 | $backend = "DeploymentBackend"; |
||
187 | break; |
||
188 | |||
189 | // Only 1 thing allowed, use that |
||
190 | case 1: |
||
191 | $backend = $backends[0]; |
||
192 | break; |
||
193 | |||
194 | // Multiple choices, use our choice if it's legal, otherwise default to the first item on the list |
||
195 | default: |
||
196 | $backend = $this->BackendIdentifier; |
||
197 | if (!in_array($backend, $backends)) { |
||
198 | $backend = $backends[0]; |
||
199 | } |
||
200 | } |
||
201 | |||
202 | return Injector::inst()->get($backend); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @param SS_HTTPRequest $request |
||
207 | * |
||
208 | * @return DeploymentStrategy |
||
209 | */ |
||
210 | public function getDeployStrategy(\SS_HTTPRequest $request) { |
||
213 | |||
214 | public function Menu() { |
||
215 | $list = new ArrayList(); |
||
216 | |||
217 | $controller = Controller::curr(); |
||
218 | $actionType = $controller->getField('CurrentActionType'); |
||
219 | |||
220 | $list->push(new ArrayData([ |
||
221 | 'Link' => sprintf('naut/project/%s/environment/%s', $this->Project()->Name, $this->Name), |
||
222 | 'Title' => 'Deployments', |
||
223 | 'IsCurrent' => $this->isCurrent(), |
||
224 | 'IsSection' => $this->isSection() && $actionType == DNRoot::ACTION_DEPLOY |
||
225 | ])); |
||
226 | |||
227 | $this->extend('updateMenu', $list); |
||
228 | |||
229 | return $list; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Return the current object from $this->Menu() |
||
234 | * Good for making titles and things |
||
235 | */ |
||
236 | public function CurrentMenu() { |
||
239 | |||
240 | /** |
||
241 | * Return a name for this environment. |
||
242 | * |
||
243 | * @param string $separator The string used when concatenating project with env name |
||
244 | * @return string |
||
245 | */ |
||
246 | public function getFullName($separator = ':') { |
||
249 | |||
250 | /** |
||
251 | * URL for the environment that can be used if no explicit URL is set. |
||
252 | */ |
||
253 | public function getDefaultURL() { |
||
256 | |||
257 | public function getBareURL() { |
||
263 | |||
264 | public function getBareDefaultURL() { |
||
270 | |||
271 | /** |
||
272 | * Environments are only viewable by people that can view the environment. |
||
273 | * |
||
274 | * @param Member|null $member |
||
275 | * @return boolean |
||
276 | */ |
||
277 | public function canView($member = null) { |
||
298 | |||
299 | /** |
||
300 | * Allow deploy only to some people. |
||
301 | * |
||
302 | * @param Member|null $member |
||
303 | * @return boolean |
||
304 | */ |
||
305 | View Code Duplication | public function canDeploy($member = null) { |
|
327 | |||
328 | /** |
||
329 | * Provide reason why the user cannot deploy. |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | public function getCannotDeployMessage() { |
||
336 | |||
337 | /** |
||
338 | * Allows only selected {@link Member} objects to restore {@link DNDataArchive} objects into this |
||
339 | * {@link DNEnvironment}. |
||
340 | * |
||
341 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
342 | * @return boolean true if $member can restore, and false if they can't. |
||
343 | */ |
||
344 | View Code Duplication | public function canRestore($member = null) { |
|
366 | |||
367 | /** |
||
368 | * Allows only selected {@link Member} objects to backup this {@link DNEnvironment} to a {@link DNDataArchive} |
||
369 | * file. |
||
370 | * |
||
371 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
372 | * @return boolean true if $member can backup, and false if they can't. |
||
373 | */ |
||
374 | View Code Duplication | public function canBackup($member = null) { |
|
401 | |||
402 | /** |
||
403 | * Allows only selected {@link Member} objects to upload {@link DNDataArchive} objects linked to this |
||
404 | * {@link DNEnvironment}. |
||
405 | * |
||
406 | * Note: This is not uploading them to the actual environment itself (e.g. uploading to the live site) - it is the |
||
407 | * process of uploading a *.sspak file into Deploynaut for later 'restoring' to an environment. See |
||
408 | * {@link self::canRestore()}. |
||
409 | * |
||
410 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
411 | * @return boolean true if $member can upload archives linked to this environment, false if they can't. |
||
412 | */ |
||
413 | View Code Duplication | public function canUploadArchive($member = null) { |
|
440 | |||
441 | /** |
||
442 | * Allows only selected {@link Member} objects to download {@link DNDataArchive} objects from this |
||
443 | * {@link DNEnvironment}. |
||
444 | * |
||
445 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
446 | * @return boolean true if $member can download archives from this environment, false if they can't. |
||
447 | */ |
||
448 | View Code Duplication | public function canDownloadArchive($member = null) { |
|
470 | |||
471 | /** |
||
472 | * Allows only selected {@link Member} objects to delete {@link DNDataArchive} objects from this |
||
473 | * {@link DNEnvironment}. |
||
474 | * |
||
475 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
476 | * @return boolean true if $member can delete archives from this environment, false if they can't. |
||
477 | */ |
||
478 | View Code Duplication | public function canDeleteArchive($member = null) { |
|
500 | |||
501 | /** |
||
502 | * Get a string of groups/people that are allowed to deploy to this environment. |
||
503 | * Used in DNRoot_project.ss to list {@link Member}s who have permission to perform this action. |
||
504 | * |
||
505 | * @return string |
||
506 | */ |
||
507 | public function getDeployersList() { |
||
516 | |||
517 | /** |
||
518 | * Get a string of groups/people that are allowed to restore {@link DNDataArchive} objects into this environment. |
||
519 | * |
||
520 | * @return string |
||
521 | */ |
||
522 | public function getCanRestoreMembersList() { |
||
531 | |||
532 | /** |
||
533 | * Get a string of groups/people that are allowed to backup {@link DNDataArchive} objects from this environment. |
||
534 | * |
||
535 | * @return string |
||
536 | */ |
||
537 | public function getCanBackupMembersList() { |
||
546 | |||
547 | /** |
||
548 | * Get a string of groups/people that are allowed to upload {@link DNDataArchive} |
||
549 | * objects linked to this environment. |
||
550 | * |
||
551 | * @return string |
||
552 | */ |
||
553 | public function getArchiveUploadersList() { |
||
562 | |||
563 | /** |
||
564 | * Get a string of groups/people that are allowed to download {@link DNDataArchive} objects from this environment. |
||
565 | * |
||
566 | * @return string |
||
567 | */ |
||
568 | public function getArchiveDownloadersList() { |
||
577 | |||
578 | /** |
||
579 | * Get a string of groups/people that are allowed to delete {@link DNDataArchive} objects from this environment. |
||
580 | * |
||
581 | * @return string |
||
582 | */ |
||
583 | public function getArchiveDeletersList() { |
||
592 | |||
593 | /** |
||
594 | * @return DNData |
||
595 | */ |
||
596 | public function DNData() { |
||
599 | |||
600 | /** |
||
601 | * Get the current deployed build for this environment |
||
602 | * |
||
603 | * Dear people of the future: If you are looking to optimize this, simply create a CurrentBuildSHA(), which can be |
||
604 | * a lot faster. I presume you came here because of the Project display template, which only needs a SHA. |
||
605 | * |
||
606 | * @return false|DNDeployment |
||
607 | */ |
||
608 | public function CurrentBuild() { |
||
641 | |||
642 | /** |
||
643 | * A history of all builds deployed to this environment |
||
644 | * |
||
645 | * @return ArrayList |
||
646 | */ |
||
647 | public function DeployHistory() { |
||
652 | |||
653 | /** |
||
654 | * @param string $action |
||
655 | * |
||
656 | * @return string |
||
657 | */ |
||
658 | public function Link($action = '') { |
||
661 | |||
662 | /** |
||
663 | * Is this environment currently at the root level of the controller that handles it? |
||
664 | * @return bool |
||
665 | */ |
||
666 | public function isCurrent() { |
||
669 | |||
670 | /** |
||
671 | * Is this environment currently in a controller that is handling it or performing a sub-task? |
||
672 | * @return bool |
||
673 | */ |
||
674 | public function isSection() { |
||
679 | |||
680 | /** |
||
681 | * @return FieldList |
||
682 | */ |
||
683 | public function getCMSFields() { |
||
832 | |||
833 | /** |
||
834 | */ |
||
835 | public function onBeforeWrite() { |
||
843 | |||
844 | public function onAfterWrite() { |
||
859 | |||
860 | /** |
||
861 | * Delete any related config files |
||
862 | */ |
||
863 | public function onAfterDelete() { |
||
875 | |||
876 | /** |
||
877 | * Returns the path to the ruby config file |
||
878 | * |
||
879 | * @return string |
||
880 | */ |
||
881 | public function getConfigFilename() { |
||
890 | |||
891 | /** |
||
892 | * Helper function to convert a multi-dimensional array (associative or indexed) to an {@link ArrayList} or |
||
893 | * {@link ArrayData} object structure, so that values can be used in templates. |
||
894 | * |
||
895 | * @param array $array The (single- or multi-dimensional) array to convert |
||
896 | * @return object Either an {@link ArrayList} or {@link ArrayData} object, or the original item ($array) if $array |
||
897 | * isn't an array. |
||
898 | */ |
||
899 | public static function array_to_viewabledata($array) { |
||
924 | |||
925 | /** |
||
926 | * Fetchs all deployments in progress. Limits to 1 hour to prevent deployments |
||
927 | * if an old deployment is stuck. |
||
928 | * |
||
929 | * @return DataList |
||
930 | */ |
||
931 | public function runningDeployments() { |
||
939 | |||
940 | /** |
||
941 | * @param string $sha |
||
942 | * @return array |
||
943 | */ |
||
944 | protected function getCommitData($sha) { |
||
968 | |||
969 | /** |
||
970 | * Build a set of multi-select fields for assigning permissions to a pair of group and member many_many relations |
||
971 | * |
||
972 | * @param string $groupField Group field name |
||
973 | * @param string $memberField Member field name |
||
974 | * @param array $groups List of groups |
||
975 | * @param array $members List of members |
||
976 | * @return FieldGroup |
||
977 | */ |
||
978 | protected function buildPermissionField($groupField, $memberField, $groups, $members) { |
||
993 | |||
994 | /** |
||
995 | * @param FieldList $fields |
||
996 | */ |
||
997 | protected function setDeployConfigurationFields(&$fields) { |
||
1017 | |||
1018 | /** |
||
1019 | * Ensure that environment paths are setup on the local filesystem |
||
1020 | */ |
||
1021 | protected function checkEnvironmentPath() { |
||
1028 | |||
1029 | /** |
||
1030 | * Write the deployment config file to filesystem |
||
1031 | */ |
||
1032 | protected function writeConfigFile() { |
||
1048 | |||
1049 | /** |
||
1050 | * @return string |
||
1051 | */ |
||
1052 | protected function getEnvironmentConfig() { |
||
1058 | |||
1059 | /** |
||
1060 | * @return boolean |
||
1061 | */ |
||
1062 | protected function envFileExists() { |
||
1068 | |||
1069 | protected function validate() { |
||
1079 | |||
1080 | } |
||
1081 | |||
1082 |
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
.