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 DNProject 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 DNProject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class DNProject extends DataObject { |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | private static $db = [ |
||
21 | "Name" => "Varchar", |
||
22 | "IsNewDeployEnabled" => "Boolean", |
||
23 | "CVSPath" => "Varchar(255)", |
||
24 | "DiskQuotaMB" => "Int", |
||
25 | "AllowedEnvironmentType" => "Varchar(255)" |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private static $has_many = [ |
||
32 | "Environments" => "DNEnvironment", |
||
33 | "CreateEnvironments" => "DNCreateEnvironment", |
||
34 | "Fetches" => "DNGitFetch" |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private static $many_many = [ |
||
41 | "Viewers" => "Group", |
||
42 | "StarredBy" => "Member" |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | private static $summary_fields = [ |
||
49 | "Name", |
||
50 | "ViewersList", |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private static $searchable_fields = [ |
||
57 | "Name", |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | private static $singular_name = 'Project'; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | private static $plural_name = 'Projects'; |
||
69 | |||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | private static $default_sort = 'Name'; |
||
74 | |||
75 | /** |
||
76 | * In-memory cache for currentBuilds per environment since fetching them from |
||
77 | * disk is pretty resource hungry. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected static $relation_cache = []; |
||
82 | |||
83 | /** |
||
84 | * @var bool|Member |
||
85 | */ |
||
86 | protected static $_current_member_cache = null; |
||
87 | |||
88 | /** |
||
89 | * Display the repository URL on the project page. |
||
90 | * |
||
91 | * @var bool |
||
92 | */ |
||
93 | private static $show_repository_url = false; |
||
94 | |||
95 | /** |
||
96 | * In-memory cache to determine whether clone repo was called. |
||
97 | * @var array |
||
98 | */ |
||
99 | private static $has_cloned_cache = []; |
||
100 | |||
101 | /** |
||
102 | * Whitelist configuration that describes how to convert a repository URL into a link |
||
103 | * to a web user interface for that URL |
||
104 | * |
||
105 | * Consists of a hash of "full.lower.case.domain" => {configuration} key/value pairs |
||
106 | * |
||
107 | * {configuration} can either be boolean true to auto-detect both the host and the |
||
108 | * name of the UI provider, or a nested array that overrides either one or both |
||
109 | * of the auto-detected values |
||
110 | * |
||
111 | * @var array |
||
112 | */ |
||
113 | private static $repository_interfaces = [ |
||
114 | 'github.com' => [ |
||
115 | 'icon' => 'deploynaut/img/github.png', |
||
116 | 'name' => 'Github.com', |
||
117 | ], |
||
118 | 'bitbucket.org' => [ |
||
119 | 'commit' => 'commits', |
||
120 | 'name' => 'Bitbucket.org', |
||
121 | ], |
||
122 | 'repo.or.cz' => [ |
||
123 | 'scheme' => 'http', |
||
124 | 'name' => 'repo.or.cz', |
||
125 | 'regex' => ['^(.*)$' => '/w$1'], |
||
126 | ], |
||
127 | |||
128 | /* Example for adding your own gitlab repository and override all auto-detected values (with their defaults) |
||
129 | 'gitlab.mysite.com' => array( |
||
130 | 'icon' => 'deploynaut/img/git.png', |
||
131 | 'host' => 'gitlab.mysite.com', |
||
132 | 'name' => 'Gitlab', |
||
133 | 'regex' => array('.git$' => ''), |
||
134 | 'commit' => "commit" |
||
135 | ), |
||
136 | */ |
||
137 | ]; |
||
138 | |||
139 | /** |
||
140 | * Used by the sync task |
||
141 | * |
||
142 | * @param string $path |
||
143 | * @return \DNProject |
||
144 | */ |
||
145 | public static function create_from_path($path) { |
||
157 | |||
158 | /** |
||
159 | * This will clear the cache for the git getters and should be called when the local git repo is updated |
||
160 | */ |
||
161 | public function clearGitCache() { |
||
167 | |||
168 | /** |
||
169 | * @return \Zend_Cache_Frontend_Output |
||
170 | */ |
||
171 | public static function get_git_cache() { |
||
177 | |||
178 | /** |
||
179 | * Return the used quota in MB. |
||
180 | * |
||
181 | * @param int $round Number of decimal places to round to |
||
182 | * @return double The used quota size in MB |
||
183 | */ |
||
184 | public function getUsedQuotaMB($round = 2) { |
||
196 | |||
197 | /** |
||
198 | * Getter for DiskQuotaMB field to provide a default for existing |
||
199 | * records that have no quota field set, as it will need to default |
||
200 | * to a globally set size. |
||
201 | * |
||
202 | * @return string|int The quota size in MB |
||
203 | */ |
||
204 | public function getDiskQuotaMB() { |
||
214 | |||
215 | /** |
||
216 | * @return string |
||
217 | */ |
||
218 | public function getPrimaryEnvType() { |
||
223 | |||
224 | /** |
||
225 | * Has the disk quota been exceeded? |
||
226 | * |
||
227 | * @return boolean |
||
228 | */ |
||
229 | public function HasExceededDiskQuota() { |
||
232 | |||
233 | /** |
||
234 | * Is there a disk quota set for this project? |
||
235 | * |
||
236 | * @return boolean |
||
237 | */ |
||
238 | public function HasDiskQuota() { |
||
241 | |||
242 | /** |
||
243 | * Returns the current disk quota usage as a percentage |
||
244 | * |
||
245 | * @return int |
||
246 | */ |
||
247 | public function DiskQuotaUsagePercent() { |
||
254 | |||
255 | /** |
||
256 | * Get the menu to be shown on projects |
||
257 | * |
||
258 | * @return ArrayList |
||
259 | */ |
||
260 | public function Menu() { |
||
279 | |||
280 | /** |
||
281 | * Is this project currently at the root level of the controller that handles it? |
||
282 | * |
||
283 | * @return bool |
||
284 | */ |
||
285 | public function isCurrent() { |
||
288 | |||
289 | /** |
||
290 | * Return the current object from $this->Menu() |
||
291 | * Good for making titles and things |
||
292 | * |
||
293 | * @return DataObject |
||
294 | */ |
||
295 | public function CurrentMenu() { |
||
298 | |||
299 | /** |
||
300 | * Is this project currently in a controller that is handling it or performing a sub-task? |
||
301 | * |
||
302 | * @return bool |
||
303 | */ |
||
304 | public function isSection() { |
||
309 | |||
310 | /** |
||
311 | * Restrict access to viewing this project |
||
312 | * |
||
313 | * @param Member|null $member |
||
314 | * @return boolean |
||
315 | */ |
||
316 | public function canView($member = null) { |
||
327 | |||
328 | /** |
||
329 | * @param Member|null $member |
||
330 | * |
||
331 | * @return bool |
||
332 | */ |
||
333 | View Code Duplication | public function canRestore($member = null) { |
|
349 | |||
350 | /** |
||
351 | * @param Member|null $member |
||
352 | * @return bool |
||
353 | */ |
||
354 | View Code Duplication | public function canBackup($member = null) { |
|
370 | |||
371 | /** |
||
372 | * @param Member|null $member |
||
373 | * @return bool |
||
374 | */ |
||
375 | View Code Duplication | public function canUploadArchive($member = null) { |
|
391 | |||
392 | /** |
||
393 | * @param Member|null $member |
||
394 | * @return bool |
||
395 | */ |
||
396 | View Code Duplication | public function canDownloadArchive($member = null) { |
|
412 | |||
413 | /** |
||
414 | * This is a permission check for the front-end only. |
||
415 | * |
||
416 | * Only admins can create environments for now. Also, we need to check the value |
||
417 | * of AllowedEnvironmentType which dictates which backend to use to render the form. |
||
418 | * |
||
419 | * @param Member|null $member |
||
420 | * |
||
421 | * @return bool |
||
422 | */ |
||
423 | public function canCreateEnvironments($member = null) { |
||
433 | |||
434 | /** |
||
435 | * @return DataList |
||
436 | */ |
||
437 | public function DataArchives() { |
||
441 | |||
442 | /** |
||
443 | * Return all archives which are "manual upload requests", |
||
444 | * meaning they don't have a file attached to them (yet). |
||
445 | * |
||
446 | * @return DataList |
||
447 | */ |
||
448 | public function PendingManualUploadDataArchives() { |
||
451 | |||
452 | /** |
||
453 | * Build an environment variable array to be used with this project. |
||
454 | * |
||
455 | * This is relevant if every project needs to use an individual SSH pubkey. |
||
456 | * |
||
457 | * Include this with all Gitonomy\Git\Repository, and |
||
458 | * \Symfony\Component\Process\Processes. |
||
459 | * |
||
460 | * @return array |
||
461 | */ |
||
462 | public function getProcessEnv() { |
||
476 | |||
477 | /** |
||
478 | * Get a string of people allowed to view this project |
||
479 | * |
||
480 | * @return string |
||
481 | */ |
||
482 | public function getViewersList() { |
||
485 | |||
486 | /** |
||
487 | * @return DNData |
||
488 | */ |
||
489 | public function DNData() { |
||
492 | |||
493 | /** |
||
494 | * Provides a DNBuildList of builds found in this project. |
||
495 | * |
||
496 | * @return DNReferenceList |
||
497 | */ |
||
498 | public function DNBuildList() { |
||
501 | |||
502 | /** |
||
503 | * Provides a list of the branches in this project. |
||
504 | * |
||
505 | * @return DNBranchList |
||
506 | */ |
||
507 | public function DNBranchList() { |
||
513 | |||
514 | /** |
||
515 | * Provides a list of the tags in this project. |
||
516 | * |
||
517 | * @return DNReferenceList |
||
518 | */ |
||
519 | public function DNTagList() { |
||
525 | |||
526 | /** |
||
527 | * @return false|Gitonomy\Git\Repository |
||
528 | */ |
||
529 | public function getRepository() { |
||
536 | |||
537 | /** |
||
538 | * Resolve a git reference like a branch or tag into a SHA. |
||
539 | * @return bool|string |
||
540 | */ |
||
541 | public function resolveRevision($value) { |
||
554 | |||
555 | /** |
||
556 | * Provides a list of environments found in this project. |
||
557 | * CAUTION: filterByCallback will change this into an ArrayList! |
||
558 | * |
||
559 | * @return ArrayList |
||
560 | */ |
||
561 | public function DNEnvironmentList() { |
||
577 | |||
578 | /** |
||
579 | * @param string $usage |
||
580 | * @return ArrayList |
||
581 | */ |
||
582 | public function EnvironmentsByUsage($usage) { |
||
585 | |||
586 | /** |
||
587 | * Returns a map of envrionment name to build name |
||
588 | * |
||
589 | * @return false|DNDeployment |
||
590 | */ |
||
591 | public function currentBuilds() { |
||
601 | |||
602 | /** |
||
603 | * @param string |
||
604 | * @return string |
||
605 | */ |
||
606 | public function Link($action = '') { |
||
609 | |||
610 | /** |
||
611 | * @return string |
||
612 | */ |
||
613 | public function getCMSEditLink() { |
||
616 | /** |
||
617 | * @return string|null |
||
618 | */ |
||
619 | public function CreateEnvironmentLink() { |
||
625 | |||
626 | /** |
||
627 | * @return string |
||
628 | */ |
||
629 | public function ToggleStarLink() { |
||
632 | |||
633 | /** |
||
634 | * @return bool |
||
635 | */ |
||
636 | public function IsStarred() { |
||
647 | |||
648 | /** |
||
649 | * @param string $action |
||
650 | * @return string |
||
651 | */ |
||
652 | public function APILink($action) { |
||
655 | |||
656 | /** |
||
657 | * @return FieldList |
||
658 | */ |
||
659 | public function getCMSFields() { |
||
717 | |||
718 | /** |
||
719 | * If there isn't a capistrano env project folder, show options to create one |
||
720 | * |
||
721 | * @param FieldList $fields |
||
722 | */ |
||
723 | public function setCreateProjectFolderField(&$fields) { |
||
740 | |||
741 | /** |
||
742 | * @return boolean |
||
743 | */ |
||
744 | public function projectFolderExists() { |
||
747 | |||
748 | /** |
||
749 | * @return bool |
||
750 | */ |
||
751 | public function repoExists() { |
||
754 | |||
755 | /** |
||
756 | * Setup a job to clone a git repository. |
||
757 | * @return string resque token |
||
758 | */ |
||
759 | public function cloneRepo() { |
||
774 | |||
775 | /** |
||
776 | * @return string |
||
777 | */ |
||
778 | public function getLocalCVSPath() { |
||
781 | |||
782 | public function onBeforeWrite() { |
||
789 | |||
790 | public function onAfterWrite() { |
||
802 | |||
803 | /** |
||
804 | * Delete related environments and folders |
||
805 | */ |
||
806 | public function onAfterDelete() { |
||
838 | |||
839 | /** |
||
840 | * Fetch the public key for this project. |
||
841 | * |
||
842 | * @return string|void |
||
843 | */ |
||
844 | public function getPublicKey() { |
||
851 | |||
852 | /** |
||
853 | * This returns that path of the public key if a key directory is set. It doesn't check whether the file exists. |
||
854 | * |
||
855 | * @return string|null |
||
856 | */ |
||
857 | public function getPublicKeyPath() { |
||
863 | |||
864 | /** |
||
865 | * This returns that path of the private key if a key directory is set. It doesn't check whether the file exists. |
||
866 | * |
||
867 | * @return string|null |
||
868 | */ |
||
869 | public function getPrivateKeyPath() { |
||
878 | |||
879 | /** |
||
880 | * Returns the location of the projects key dir if one exists. |
||
881 | * |
||
882 | * @return string|null |
||
883 | */ |
||
884 | public function getKeyDir() { |
||
895 | |||
896 | /** |
||
897 | * Provide current repository URL to the users. |
||
898 | * |
||
899 | * @return void|string |
||
900 | */ |
||
901 | public function getRepositoryURL() { |
||
907 | |||
908 | /** |
||
909 | * Get a ViewableData structure describing the UI tool that lets the user view the repository code |
||
910 | * |
||
911 | * @return ArrayData |
||
912 | */ |
||
913 | public function getRepositoryInterface() { |
||
953 | |||
954 | /** |
||
955 | * Convenience wrapper for a single permission code. |
||
956 | * |
||
957 | * @param string $code |
||
958 | * @return SS_List |
||
959 | */ |
||
960 | public function whoIsAllowed($code) { |
||
963 | |||
964 | /** |
||
965 | * List members who have $codes on this project. |
||
966 | * Does not support Permission::DENY_PERMISSION malarky, same as Permission::get_groups_by_permission anyway... |
||
967 | * |
||
968 | * @param array|string $codes |
||
969 | * @return SS_List |
||
970 | */ |
||
971 | public function whoIsAllowedAny($codes) { |
||
990 | |||
991 | /** |
||
992 | * Convenience wrapper for a single permission code. |
||
993 | * |
||
994 | * @param string $code |
||
995 | * @param Member|null $member |
||
996 | * |
||
997 | * @return bool |
||
998 | */ |
||
999 | public function allowed($code, $member = null) { |
||
1002 | |||
1003 | /** |
||
1004 | * Checks if a group is allowed to the project and the permission code |
||
1005 | * |
||
1006 | * @param string $permissionCode |
||
1007 | * @param Group $group |
||
1008 | * |
||
1009 | * @return bool |
||
1010 | */ |
||
1011 | public function groupAllowed($permissionCode, Group $group) { |
||
1022 | |||
1023 | /** |
||
1024 | * Check if member has a permission code in this project. |
||
1025 | * |
||
1026 | * @param array|string $codes |
||
1027 | * @param Member|null $member |
||
1028 | * |
||
1029 | * @return bool |
||
1030 | */ |
||
1031 | public function allowedAny($codes, $member = null) { |
||
1043 | |||
1044 | /** |
||
1045 | * Checks if the environment has been fully built. |
||
1046 | * |
||
1047 | * @return bool |
||
1048 | */ |
||
1049 | public function isProjectReady() { |
||
1072 | |||
1073 | /** |
||
1074 | * Returns a list of environments still being created. |
||
1075 | * |
||
1076 | * @return SS_List |
||
1077 | */ |
||
1078 | public function getRunningEnvironmentCreations() { |
||
1082 | |||
1083 | /** |
||
1084 | * Returns a list of initial environments created for this project. |
||
1085 | * |
||
1086 | * @return DataList |
||
1087 | */ |
||
1088 | public function getInitialEnvironmentCreations() { |
||
1091 | |||
1092 | /** |
||
1093 | * Only returns initial environments that are being created. |
||
1094 | * |
||
1095 | * @return DataList |
||
1096 | */ |
||
1097 | public function getRunningInitialEnvironmentCreations() { |
||
1101 | |||
1102 | /** |
||
1103 | * Returns a list of completed initial environment creations. This includes failed tasks. |
||
1104 | * |
||
1105 | * @return DataList |
||
1106 | */ |
||
1107 | public function getCompleteInitialEnvironmentCreations() { |
||
1111 | |||
1112 | /** |
||
1113 | * @param Member $member |
||
1114 | * |
||
1115 | * @return bool |
||
1116 | */ |
||
1117 | public function canCreate($member = null) { |
||
1132 | |||
1133 | /** |
||
1134 | * This is a proxy call to gitonmy that caches the information per project and sha |
||
1135 | * |
||
1136 | * @param string $sha |
||
1137 | * @return false|\Gitonomy\Git\Commit |
||
1138 | */ |
||
1139 | public function getCommit($sha) { |
||
1157 | |||
1158 | /** |
||
1159 | * @param \Gitonomy\Git\Commit $commit |
||
1160 | * @return string |
||
1161 | */ |
||
1162 | View Code Duplication | public function getCommitMessage(\Gitonomy\Git\Commit $commit) { |
|
1171 | |||
1172 | /** |
||
1173 | * get the commit "subject", getCommitMessage get the full message |
||
1174 | * |
||
1175 | * @param \Gitonomy\Git\Commit $commit |
||
1176 | * @return string |
||
1177 | */ |
||
1178 | View Code Duplication | public function getCommitSubjectMessage(\Gitonomy\Git\Commit $commit) { |
|
1187 | |||
1188 | /** |
||
1189 | * @param \Gitonomy\Git\Commit $commit |
||
1190 | * @return mixed |
||
1191 | */ |
||
1192 | public function getCommitTags(\Gitonomy\Git\Commit $commit) { |
||
1204 | |||
1205 | /** |
||
1206 | * Setup a gridfield for the environment configs |
||
1207 | * |
||
1208 | * @param FieldList $fields |
||
1209 | * @param GridField $environments |
||
1210 | */ |
||
1211 | protected function setEnvironmentFields(&$fields, $environments) { |
||
1229 | |||
1230 | /** |
||
1231 | * @return string |
||
1232 | */ |
||
1233 | protected function getProjectFolderPath() { |
||
1236 | |||
1237 | /** |
||
1238 | * @return ValidationResult |
||
1239 | */ |
||
1240 | protected function validate() { |
||
1266 | |||
1267 | } |
||
1268 | |||
1269 |
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
.