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() { |
||
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)) { |
||
316 | return true; |
||
317 | } |
||
318 | } else { |
||
319 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_DEPLOYMENT, $member)) { |
||
320 | return true; |
||
321 | } |
||
322 | } |
||
323 | |||
324 | return $this->Deployers()->byID($member->ID) |
||
325 | || $member->inGroups($this->DeployerGroups()); |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Provide reason why the user cannot deploy. |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | public function getCannotDeployMessage() { |
||
334 | return 'You cannot deploy to this environment.'; |
||
335 | } |
||
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) { |
|
345 | if (!$member) { |
||
346 | $member = Member::currentUser(); |
||
347 | } |
||
348 | if (!$member) { |
||
349 | return false; |
||
350 | } |
||
351 | // Must be logged in to check permissions |
||
352 | |||
353 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
354 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
||
355 | return true; |
||
356 | } |
||
357 | } else { |
||
358 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
||
359 | return true; |
||
360 | } |
||
361 | } |
||
362 | |||
363 | return $this->CanRestoreMembers()->byID($member->ID) |
||
364 | || $member->inGroups($this->CanRestoreGroups()); |
||
365 | } |
||
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) { |
|
375 | $project = $this->Project(); |
||
376 | if ($project->HasDiskQuota() && $project->HasExceededDiskQuota()) { |
||
377 | return false; |
||
378 | } |
||
379 | |||
380 | if (!$member) { |
||
381 | $member = Member::currentUser(); |
||
382 | } |
||
383 | // Must be logged in to check permissions |
||
384 | if (!$member) { |
||
385 | return false; |
||
386 | } |
||
387 | |||
388 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
389 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
||
390 | return true; |
||
391 | } |
||
392 | } else { |
||
393 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
||
394 | return true; |
||
395 | } |
||
396 | } |
||
397 | |||
398 | return $this->CanBackupMembers()->byID($member->ID) |
||
399 | || $member->inGroups($this->CanBackupGroups()); |
||
400 | } |
||
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) { |
|
414 | $project = $this->Project(); |
||
415 | if ($project->HasDiskQuota() && $project->HasExceededDiskQuota()) { |
||
416 | return false; |
||
417 | } |
||
418 | |||
419 | if (!$member) { |
||
420 | $member = Member::currentUser(); |
||
421 | } |
||
422 | if (!$member) { |
||
423 | return false; |
||
424 | } |
||
425 | // Must be logged in to check permissions |
||
426 | |||
427 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
428 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
||
429 | return true; |
||
430 | } |
||
431 | } else { |
||
432 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
||
433 | return true; |
||
434 | } |
||
435 | } |
||
436 | |||
437 | return $this->ArchiveUploaders()->byID($member->ID) |
||
438 | || $member->inGroups($this->ArchiveUploaderGroups()); |
||
439 | } |
||
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) { |
|
449 | if (!$member) { |
||
450 | $member = Member::currentUser(); |
||
451 | } |
||
452 | if (!$member) { |
||
453 | return false; |
||
454 | } |
||
455 | // Must be logged in to check permissions |
||
456 | |||
457 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
458 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
||
459 | return true; |
||
460 | } |
||
461 | } else { |
||
462 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
||
463 | return true; |
||
464 | } |
||
465 | } |
||
466 | |||
467 | return $this->ArchiveDownloaders()->byID($member->ID) |
||
468 | || $member->inGroups($this->ArchiveDownloaderGroups()); |
||
469 | } |
||
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) { |
|
479 | if (!$member) { |
||
480 | $member = Member::currentUser(); |
||
481 | } |
||
482 | if (!$member) { |
||
483 | return false; |
||
484 | } |
||
485 | // Must be logged in to check permissions |
||
486 | |||
487 | if ($this->Usage === self::PRODUCTION || $this->Usage === self::UNSPECIFIED) { |
||
488 | if ($this->Project()->allowed(DNRoot::ALLOW_PROD_SNAPSHOT, $member)) { |
||
489 | return true; |
||
490 | } |
||
491 | } else { |
||
492 | if ($this->Project()->allowed(DNRoot::ALLOW_NON_PROD_SNAPSHOT, $member)) { |
||
493 | return true; |
||
494 | } |
||
495 | } |
||
496 | |||
497 | return $this->ArchiveDeleters()->byID($member->ID) |
||
498 | || $member->inGroups($this->ArchiveDeleterGroups()); |
||
499 | } |
||
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() { |
||
584 | return implode( |
||
585 | ", ", |
||
586 | array_merge( |
||
587 | $this->ArchiveDeleterGroups()->column("Title"), |
||
588 | $this->ArchiveDeleters()->column("FirstName") |
||
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 | * This is a proxy call to gitonmy that caches the information per project and sha |
||
644 | * |
||
645 | * @param string $sha |
||
646 | * @return \Gitonomy\Git\Commit |
||
647 | */ |
||
648 | public function getCommit($sha) { |
||
651 | |||
652 | public function getCommitMessage(\Gitonomy\Git\Commit $commit) { |
||
655 | |||
656 | public function getCommitTags(\Gitonomy\Git\Commit $commit) { |
||
659 | |||
660 | /** |
||
661 | * A list of past deployments. |
||
662 | * @param string $orderBy - the name of a DB column to sort in descending order |
||
663 | * @return \ArrayList |
||
664 | */ |
||
665 | public function DeployHistory($orderBy = '') { |
||
682 | |||
683 | /** |
||
684 | * A list of upcoming or current deployments. |
||
685 | * @return ArrayList |
||
686 | */ |
||
687 | public function UpcomingDeployments() { |
||
701 | |||
702 | /** |
||
703 | * @param string $action |
||
704 | * |
||
705 | * @return string |
||
706 | */ |
||
707 | public function Link($action = '') { |
||
710 | |||
711 | /** |
||
712 | * Is this environment currently at the root level of the controller that handles it? |
||
713 | * @return bool |
||
714 | */ |
||
715 | public function isCurrent() { |
||
718 | |||
719 | /** |
||
720 | * Is this environment currently in a controller that is handling it or performing a sub-task? |
||
721 | * @return bool |
||
722 | */ |
||
723 | public function isSection() { |
||
728 | |||
729 | /** |
||
730 | * @return FieldList |
||
731 | */ |
||
732 | public function getCMSFields() { |
||
881 | |||
882 | /** |
||
883 | */ |
||
884 | public function onBeforeWrite() { |
||
892 | |||
893 | public function onAfterWrite() { |
||
908 | |||
909 | /** |
||
910 | * Delete any related config files |
||
911 | */ |
||
912 | public function onAfterDelete() { |
||
939 | |||
940 | /** |
||
941 | * Returns the path to the ruby config file |
||
942 | * |
||
943 | * @return string |
||
944 | */ |
||
945 | public function getConfigFilename() { |
||
954 | |||
955 | /** |
||
956 | * Helper function to convert a multi-dimensional array (associative or indexed) to an {@link ArrayList} or |
||
957 | * {@link ArrayData} object structure, so that values can be used in templates. |
||
958 | * |
||
959 | * @param array $array The (single- or multi-dimensional) array to convert |
||
960 | * @return object Either an {@link ArrayList} or {@link ArrayData} object, or the original item ($array) if $array |
||
961 | * isn't an array. |
||
962 | */ |
||
963 | public static function array_to_viewabledata($array) { |
||
988 | |||
989 | /** |
||
990 | * Fetchs all deployments in progress. Limits to 1 hour to prevent deployments |
||
991 | * if an old deployment is stuck. |
||
992 | * |
||
993 | * @return DataList |
||
994 | */ |
||
995 | public function runningDeployments() { |
||
1007 | |||
1008 | /** |
||
1009 | * @param string $sha |
||
1010 | * @return array |
||
1011 | */ |
||
1012 | protected function getCommitData($sha) { |
||
1036 | |||
1037 | /** |
||
1038 | * Build a set of multi-select fields for assigning permissions to a pair of group and member many_many relations |
||
1039 | * |
||
1040 | * @param string $groupField Group field name |
||
1041 | * @param string $memberField Member field name |
||
1042 | * @param array $groups List of groups |
||
1043 | * @param array $members List of members |
||
1044 | * @return FieldGroup |
||
1045 | */ |
||
1046 | protected function buildPermissionField($groupField, $memberField, $groups, $members) { |
||
1061 | |||
1062 | /** |
||
1063 | * @param FieldList $fields |
||
1064 | */ |
||
1065 | protected function setDeployConfigurationFields(&$fields) { |
||
1085 | |||
1086 | /** |
||
1087 | * Ensure that environment paths are setup on the local filesystem |
||
1088 | */ |
||
1089 | protected function checkEnvironmentPath() { |
||
1096 | |||
1097 | /** |
||
1098 | * Write the deployment config file to filesystem |
||
1099 | */ |
||
1100 | protected function writeConfigFile() { |
||
1116 | |||
1117 | /** |
||
1118 | * @return string |
||
1119 | */ |
||
1120 | protected function getEnvironmentConfig() { |
||
1126 | |||
1127 | /** |
||
1128 | * @return boolean |
||
1129 | */ |
||
1130 | protected function envFileExists() { |
||
1136 | |||
1137 | protected function validate() { |
||
1147 | |||
1148 | } |
||
1149 |
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
.