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 | /** |
||
39 | * If this is set to a full pathfile, it will be used as template |
||
40 | * file when creating a new capistrano environment config file. |
||
41 | * |
||
42 | * If not set, the default 'environment.template' from the module |
||
43 | * root is used |
||
44 | * |
||
45 | * @config |
||
46 | * @var string |
||
47 | */ |
||
48 | private static $template_file = ''; |
||
49 | |||
50 | /** |
||
51 | * Set this to true to allow editing of the environment files via the web admin |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | private static $allow_web_editing = false; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | private static $casting = array( |
||
61 | 'DeployHistory' => 'Text' |
||
62 | ); |
||
63 | |||
64 | /** |
||
65 | * Allowed backends. A map of Injector identifier to human-readable label. |
||
66 | * |
||
67 | * @config |
||
68 | * @var array |
||
69 | */ |
||
70 | private static $allowed_backends = array(); |
||
71 | |||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | public static $db = array( |
||
76 | "Filename" => "Varchar(255)", |
||
77 | "Name" => "Varchar(255)", |
||
78 | "URL" => "Varchar(255)", |
||
79 | "BackendIdentifier" => "Varchar(255)", // Injector identifier of the DeploymentBackend |
||
80 | "Usage" => "Enum('Production, UAT, Test, Unspecified', 'Unspecified')" |
||
81 | ); |
||
82 | |||
83 | /** |
||
84 | * @var array |
||
85 | */ |
||
86 | public static $has_one = array( |
||
87 | "Project" => "DNProject", |
||
88 | "CreateEnvironment" => "DNCreateEnvironment" |
||
89 | ); |
||
90 | |||
91 | /** |
||
92 | * @var array |
||
93 | */ |
||
94 | public static $has_many = array( |
||
95 | "Deployments" => "DNDeployment", |
||
96 | "DataArchives" => "DNDataArchive", |
||
97 | ); |
||
98 | |||
99 | /** |
||
100 | * @var array |
||
101 | */ |
||
102 | public static $many_many = array( |
||
103 | "Viewers" => "Member", // Who can view this environment |
||
104 | "ViewerGroups" => "Group", |
||
105 | "Deployers" => "Member", // Who can deploy to this environment |
||
106 | "DeployerGroups" => "Group", |
||
107 | "CanRestoreMembers" => "Member", // Who can restore archive files to this environment |
||
108 | "CanRestoreGroups" => "Group", |
||
109 | "CanBackupMembers" => "Member", // Who can backup archive files from this environment |
||
110 | "CanBackupGroups" => "Group", |
||
111 | "ArchiveUploaders" => "Member", // Who can upload archive files linked to this environment |
||
112 | "ArchiveUploaderGroups" => "Group", |
||
113 | "ArchiveDownloaders" => "Member", // Who can download archive files from this environment |
||
114 | "ArchiveDownloaderGroups" => "Group", |
||
115 | "ArchiveDeleters" => "Member", // Who can delete archive files from this environment, |
||
116 | "ArchiveDeleterGroups" => "Group", |
||
117 | ); |
||
118 | |||
119 | /** |
||
120 | * @var array |
||
121 | */ |
||
122 | public static $summary_fields = array( |
||
123 | "Name" => "Environment Name", |
||
124 | "Usage" => "Usage", |
||
125 | "URL" => "URL", |
||
126 | "DeployersList" => "Can Deploy List", |
||
127 | "CanRestoreMembersList" => "Can Restore List", |
||
128 | "CanBackupMembersList" => "Can Backup List", |
||
129 | "ArchiveUploadersList" => "Can Upload List", |
||
130 | "ArchiveDownloadersList" => "Can Download List", |
||
131 | "ArchiveDeletersList" => "Can Delete List", |
||
132 | ); |
||
133 | |||
134 | private static $singular_name = 'Capistrano Environment'; |
||
135 | |||
136 | private static $plural_name = 'Capistrano Environments'; |
||
137 | |||
138 | /** |
||
139 | * @var array |
||
140 | */ |
||
141 | public static $searchable_fields = array( |
||
142 | "Name", |
||
143 | ); |
||
144 | |||
145 | /** |
||
146 | * @var string |
||
147 | */ |
||
148 | private static $default_sort = 'Name'; |
||
149 | |||
150 | const UAT = 'UAT'; |
||
151 | |||
152 | const PRODUCTION = 'Production'; |
||
153 | |||
154 | const UNSPECIFIED = 'Unspecified'; |
||
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() { |
||
204 | |||
205 | /** |
||
206 | * @param SS_HTTPRequest $request |
||
207 | * |
||
208 | * @return DeploymentStrategy |
||
209 | */ |
||
210 | public function getDeployStrategy(\SS_HTTPRequest $request) { |
||
211 | return $this->Backend()->planDeploy($this, $request->requestVars()); |
||
212 | } |
||
213 | |||
214 | public function Menu() { |
||
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() { |
||
254 | return null; |
||
255 | } |
||
256 | |||
257 | public function getBareURL() { |
||
263 | |||
264 | public function getBareDefaultURL() { |
||
265 | $url = parse_url($this->getDefaultURL()); |
||
266 | if(isset($url['host'])) { |
||
267 | return strtolower($url['host']); |
||
268 | } |
||
269 | } |
||
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) { |
||
278 | if(!$member) { |
||
279 | $member = Member::currentUser(); |
||
280 | } |
||
281 | if(!$member) { |
||
282 | return false; |
||
283 | } |
||
284 | // Must be logged in to check permissions |
||
285 | |||
286 | if(Permission::checkMember($member, 'ADMIN')) { |
||
287 | return true; |
||
288 | } |
||
289 | |||
290 | // if no Viewers or ViewerGroups defined, fallback to DNProject::canView permissions |
||
291 | if($this->Viewers()->exists() || $this->ViewerGroups()->exists()) { |
||
292 | return $this->Viewers()->byID($member->ID) |
||
293 | || $member->inGroups($this->ViewerGroups()); |
||
294 | } |
||
295 | |||
296 | return $this->Project()->canView($member); |
||
297 | } |
||
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) { |
|
306 | if(!$member) { |
||
307 | $member = Member::currentUser(); |
||
308 | } |
||
309 | if(!$member) { |
||
310 | return false; |
||
311 | } |
||
312 | // Must be logged in to check permissions |
||
313 | |||
314 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
315 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_DEPLOYMENT, $member)) return true; |
||
316 | } else { |
||
317 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_DEPLOYMENT, $member)) return true; |
||
318 | } |
||
319 | |||
320 | return $this->Deployers()->byID($member->ID) |
||
321 | || $member->inGroups($this->DeployerGroups()); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Provide reason why the user cannot deploy. |
||
326 | * |
||
327 | * @return string |
||
328 | */ |
||
329 | public function getCannotDeployMessage() { |
||
330 | return 'You cannot deploy to this environment.'; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Allows only selected {@link Member} objects to restore {@link DNDataArchive} objects into this |
||
335 | * {@link DNEnvironment}. |
||
336 | * |
||
337 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
338 | * @return boolean true if $member can restore, and false if they can't. |
||
339 | */ |
||
340 | View Code Duplication | public function canRestore($member = null) { |
|
341 | if(!$member) { |
||
342 | $member = Member::currentUser(); |
||
343 | } |
||
344 | if(!$member) { |
||
345 | return false; |
||
346 | } |
||
347 | // Must be logged in to check permissions |
||
348 | |||
349 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
350 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) return true; |
||
351 | } else { |
||
352 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) return true; |
||
353 | } |
||
354 | |||
355 | return $this->CanRestoreMembers()->byID($member->ID) |
||
356 | || $member->inGroups($this->CanRestoreGroups()); |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Allows only selected {@link Member} objects to backup this {@link DNEnvironment} to a {@link DNDataArchive} |
||
361 | * file. |
||
362 | * |
||
363 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
364 | * @return boolean true if $member can backup, and false if they can't. |
||
365 | */ |
||
366 | View Code Duplication | public function canBackup($member = null) { |
|
367 | $project = $this->Project(); |
||
368 | if($project->HasDiskQuota() && $project->HasExceededDiskQuota()) { |
||
369 | return false; |
||
370 | } |
||
371 | |||
372 | if(!$member) { |
||
373 | $member = Member::currentUser(); |
||
374 | } |
||
375 | // Must be logged in to check permissions |
||
376 | if(!$member) { |
||
377 | return false; |
||
378 | } |
||
379 | |||
380 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
381 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) return true; |
||
382 | } else { |
||
383 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) return true; |
||
384 | } |
||
385 | |||
386 | return $this->CanBackupMembers()->byID($member->ID) |
||
387 | || $member->inGroups($this->CanBackupGroups()); |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * Allows only selected {@link Member} objects to upload {@link DNDataArchive} objects linked to this |
||
392 | * {@link DNEnvironment}. |
||
393 | * |
||
394 | * Note: This is not uploading them to the actual environment itself (e.g. uploading to the live site) - it is the |
||
395 | * process of uploading a *.sspak file into Deploynaut for later 'restoring' to an environment. See |
||
396 | * {@link self::canRestore()}. |
||
397 | * |
||
398 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
399 | * @return boolean true if $member can upload archives linked to this environment, false if they can't. |
||
400 | */ |
||
401 | View Code Duplication | public function canUploadArchive($member = null) { |
|
424 | |||
425 | /** |
||
426 | * Allows only selected {@link Member} objects to download {@link DNDataArchive} objects from this |
||
427 | * {@link DNEnvironment}. |
||
428 | * |
||
429 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
430 | * @return boolean true if $member can download archives from this environment, false if they can't. |
||
431 | */ |
||
432 | View Code Duplication | public function canDownloadArchive($member = null) { |
|
450 | |||
451 | /** |
||
452 | * Allows only selected {@link Member} objects to delete {@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 delete archives from this environment, false if they can't. |
||
457 | */ |
||
458 | View Code Duplication | public function canDeleteArchive($member = null) { |
|
476 | /** |
||
477 | * Get a string of groups/people that are allowed to deploy to this environment. |
||
478 | * Used in DNRoot_project.ss to list {@link Member}s who have permission to perform this action. |
||
479 | * |
||
480 | * @return string |
||
481 | */ |
||
482 | public function getDeployersList() { |
||
491 | |||
492 | /** |
||
493 | * Get a string of groups/people that are allowed to restore {@link DNDataArchive} objects into this environment. |
||
494 | * |
||
495 | * @return string |
||
496 | */ |
||
497 | public function getCanRestoreMembersList() { |
||
506 | |||
507 | /** |
||
508 | * Get a string of groups/people that are allowed to backup {@link DNDataArchive} objects from this environment. |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | public function getCanBackupMembersList() { |
||
521 | |||
522 | /** |
||
523 | * Get a string of groups/people that are allowed to upload {@link DNDataArchive} |
||
524 | * objects linked to this environment. |
||
525 | * |
||
526 | * @return string |
||
527 | */ |
||
528 | public function getArchiveUploadersList() { |
||
537 | |||
538 | /** |
||
539 | * Get a string of groups/people that are allowed to download {@link DNDataArchive} objects from this environment. |
||
540 | * |
||
541 | * @return string |
||
542 | */ |
||
543 | public function getArchiveDownloadersList() { |
||
552 | |||
553 | /** |
||
554 | * Get a string of groups/people that are allowed to delete {@link DNDataArchive} objects from this environment. |
||
555 | * |
||
556 | * @return string |
||
557 | */ |
||
558 | public function getArchiveDeletersList() { |
||
559 | return implode( |
||
560 | ", ", |
||
567 | |||
568 | /** |
||
569 | * @return DNData |
||
570 | */ |
||
571 | public function DNData() { |
||
574 | |||
575 | /** |
||
576 | * Get the current deployed build for this environment |
||
577 | * |
||
578 | * Dear people of the future: If you are looking to optimize this, simply create a CurrentBuildSHA(), which can be |
||
579 | * a lot faster. I presume you came here because of the Project display template, which only needs a SHA. |
||
580 | * |
||
581 | * @return false|DNDeployment |
||
582 | */ |
||
583 | public function CurrentBuild() { |
||
615 | |||
616 | /** |
||
617 | * A list of past deployments. |
||
618 | * @return ArrayList |
||
619 | */ |
||
620 | public function DeployHistory() { |
||
626 | |||
627 | /** |
||
628 | * A list of upcoming or current deployments. |
||
629 | * @return ArrayList |
||
630 | */ |
||
631 | public function UpcomingDeployments() { |
||
637 | |||
638 | /** |
||
639 | * @param string $sha |
||
640 | * @return array |
||
641 | */ |
||
642 | protected function getCommitData($sha) { |
||
666 | |||
667 | /** |
||
668 | * @param string $action |
||
669 | * |
||
670 | * @return string |
||
671 | */ |
||
672 | public function Link($action = '') { |
||
675 | |||
676 | /** |
||
677 | * Is this environment currently at the root level of the controller that handles it? |
||
678 | * @return bool |
||
679 | */ |
||
680 | public function isCurrent() { |
||
683 | |||
684 | /** |
||
685 | * Is this environment currently in a controller that is handling it or performing a sub-task? |
||
686 | * @return bool |
||
687 | */ |
||
688 | public function isSection() { |
||
693 | |||
694 | |||
695 | /** |
||
696 | * Build a set of multi-select fields for assigning permissions to a pair of group and member many_many relations |
||
697 | * |
||
698 | * @param string $groupField Group field name |
||
699 | * @param string $memberField Member field name |
||
700 | * @param array $groups List of groups |
||
701 | * @param array $members List of members |
||
702 | * @return FieldGroup |
||
703 | */ |
||
704 | protected function buildPermissionField($groupField, $memberField, $groups, $members) { |
||
719 | |||
720 | /** |
||
721 | * @return FieldList |
||
722 | */ |
||
723 | public function getCMSFields() { |
||
873 | |||
874 | /** |
||
875 | * @param FieldList $fields |
||
876 | */ |
||
877 | protected function setDeployConfigurationFields(&$fields) { |
||
897 | |||
898 | /** |
||
899 | */ |
||
900 | public function onBeforeWrite() { |
||
908 | |||
909 | public function onAfterWrite() { |
||
924 | |||
925 | |||
926 | /** |
||
927 | * Ensure that environment paths are setup on the local filesystem |
||
928 | */ |
||
929 | protected function checkEnvironmentPath() { |
||
936 | |||
937 | /** |
||
938 | * Write the deployment config file to filesystem |
||
939 | */ |
||
940 | protected function writeConfigFile() { |
||
956 | |||
957 | /** |
||
958 | * Delete any related config files |
||
959 | */ |
||
960 | public function onAfterDelete() { |
||
972 | |||
973 | /** |
||
974 | * @return string |
||
975 | */ |
||
976 | protected function getEnvironmentConfig() { |
||
982 | |||
983 | /** |
||
984 | * @return boolean |
||
985 | */ |
||
986 | protected function envFileExists() { |
||
992 | |||
993 | /** |
||
994 | * Returns the path to the ruby config file |
||
995 | * |
||
996 | * @return string |
||
997 | */ |
||
998 | public function getConfigFilename() { |
||
1007 | |||
1008 | /** |
||
1009 | * Helper function to convert a multi-dimensional array (associative or indexed) to an {@link ArrayList} or |
||
1010 | * {@link ArrayData} object structure, so that values can be used in templates. |
||
1011 | * |
||
1012 | * @param array $array The (single- or multi-dimensional) array to convert |
||
1013 | * @return object Either an {@link ArrayList} or {@link ArrayData} object, or the original item ($array) if $array |
||
1014 | * isn't an array. |
||
1015 | */ |
||
1016 | public static function array_to_viewabledata($array) { |
||
1041 | |||
1042 | protected function validate() { |
||
1052 | |||
1053 | /** |
||
1054 | * Fetchs all deployments in progress. Limits to 1 hour to prevent deployments |
||
1055 | * if an old deployment is stuck. |
||
1056 | * |
||
1057 | * @return DataList |
||
1058 | */ |
||
1059 | public function runningDeployments() { |
||
1067 | |||
1068 | } |
||
1069 | |||
1070 |
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
.