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) { |
||
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() { |
||
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) { |
|
323 | |||
324 | /** |
||
325 | * Provide reason why the user cannot deploy. |
||
326 | * |
||
327 | * @return string |
||
328 | */ |
||
329 | public function getCannotDeployMessage() { |
||
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) { |
|
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) { |
|
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() { |
||
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 history of all builds deployed to this environment |
||
618 | * |
||
619 | * @return ArrayList |
||
620 | */ |
||
621 | public function DeployHistory() { |
||
626 | |||
627 | /** |
||
628 | * @param string $sha |
||
629 | * @return array |
||
630 | */ |
||
631 | protected function getCommitData($sha) { |
||
655 | |||
656 | /** |
||
657 | * @return string |
||
658 | */ |
||
659 | public function Link() { |
||
662 | |||
663 | /** |
||
664 | * Is this environment currently at the root level of the controller that handles it? |
||
665 | * @return bool |
||
666 | */ |
||
667 | public function isCurrent() { |
||
670 | |||
671 | /** |
||
672 | * Is this environment currently in a controller that is handling it or performing a sub-task? |
||
673 | * @return bool |
||
674 | */ |
||
675 | public function isSection() { |
||
680 | |||
681 | |||
682 | /** |
||
683 | * Build a set of multi-select fields for assigning permissions to a pair of group and member many_many relations |
||
684 | * |
||
685 | * @param string $groupField Group field name |
||
686 | * @param string $memberField Member field name |
||
687 | * @param array $groups List of groups |
||
688 | * @param array $members List of members |
||
689 | * @return FieldGroup |
||
690 | */ |
||
691 | protected function buildPermissionField($groupField, $memberField, $groups, $members) { |
||
706 | |||
707 | /** |
||
708 | * @return FieldList |
||
709 | */ |
||
710 | public function getCMSFields() { |
||
860 | |||
861 | /** |
||
862 | * @param FieldList $fields |
||
863 | */ |
||
864 | protected function setDeployConfigurationFields(&$fields) { |
||
884 | |||
885 | /** |
||
886 | */ |
||
887 | public function onBeforeWrite() { |
||
895 | |||
896 | public function onAfterWrite() { |
||
911 | |||
912 | |||
913 | /** |
||
914 | * Ensure that environment paths are setup on the local filesystem |
||
915 | */ |
||
916 | protected function checkEnvironmentPath() { |
||
923 | |||
924 | /** |
||
925 | * Write the deployment config file to filesystem |
||
926 | */ |
||
927 | protected function writeConfigFile() { |
||
943 | |||
944 | /** |
||
945 | * Delete any related config files |
||
946 | */ |
||
947 | public function onAfterDelete() { |
||
959 | |||
960 | /** |
||
961 | * @return string |
||
962 | */ |
||
963 | protected function getEnvironmentConfig() { |
||
969 | |||
970 | /** |
||
971 | * @return boolean |
||
972 | */ |
||
973 | protected function envFileExists() { |
||
979 | |||
980 | /** |
||
981 | * Returns the path to the ruby config file |
||
982 | * |
||
983 | * @return string |
||
984 | */ |
||
985 | public function getConfigFilename() { |
||
994 | |||
995 | /** |
||
996 | * Helper function to convert a multi-dimensional array (associative or indexed) to an {@link ArrayList} or |
||
997 | * {@link ArrayData} object structure, so that values can be used in templates. |
||
998 | * |
||
999 | * @param array $array The (single- or multi-dimensional) array to convert |
||
1000 | * @return object Either an {@link ArrayList} or {@link ArrayData} object, or the original item ($array) if $array |
||
1001 | * isn't an array. |
||
1002 | */ |
||
1003 | public static function array_to_viewabledata($array) { |
||
1028 | |||
1029 | protected function validate() { |
||
1039 | |||
1040 | /** |
||
1041 | * Fetchs all deployments in progress. Limits to 1 hour to prevent deployments |
||
1042 | * if an old deployment is stuck. |
||
1043 | * |
||
1044 | * @return DataList |
||
1045 | */ |
||
1046 | public function runningDeployments() { |
||
1054 | |||
1055 | } |
||
1056 | |||
1057 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.