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 | * Has the disk quota been exceeded? |
||
| 217 | * |
||
| 218 | * @return boolean |
||
| 219 | */ |
||
| 220 | public function HasExceededDiskQuota() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Is there a disk quota set for this project? |
||
| 226 | * |
||
| 227 | * @return boolean |
||
| 228 | */ |
||
| 229 | public function HasDiskQuota() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Returns the current disk quota usage as a percentage |
||
| 235 | * |
||
| 236 | * @return int |
||
| 237 | */ |
||
| 238 | public function DiskQuotaUsagePercent() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get the menu to be shown on projects |
||
| 248 | * |
||
| 249 | * @return ArrayList |
||
| 250 | */ |
||
| 251 | public function Menu() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Is this project currently at the root level of the controller that handles it? |
||
| 273 | * |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | public function isCurrent() { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Return the current object from $this->Menu() |
||
| 282 | * Good for making titles and things |
||
| 283 | * |
||
| 284 | * @return DataObject |
||
| 285 | */ |
||
| 286 | public function CurrentMenu() { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Is this project currently in a controller that is handling it or performing a sub-task? |
||
| 292 | * |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | public function isSection() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Restrict access to viewing this project |
||
| 303 | * |
||
| 304 | * @param Member|null $member |
||
| 305 | * @return boolean |
||
| 306 | */ |
||
| 307 | public function canView($member = null) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param Member|null $member |
||
| 321 | * |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | View Code Duplication | public function canRestore($member = null) { |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @param Member|null $member |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | View Code Duplication | public function canBackup($member = null) { |
|
| 361 | |||
| 362 | /** |
||
| 363 | * @param Member|null $member |
||
| 364 | * @return bool |
||
| 365 | */ |
||
| 366 | View Code Duplication | public function canUploadArchive($member = null) { |
|
| 382 | |||
| 383 | /** |
||
| 384 | * @param Member|null $member |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | View Code Duplication | public function canDownloadArchive($member = null) { |
|
| 403 | |||
| 404 | /** |
||
| 405 | * This is a permission check for the front-end only. |
||
| 406 | * |
||
| 407 | * Only admins can create environments for now. Also, we need to check the value |
||
| 408 | * of AllowedEnvironmentType which dictates which backend to use to render the form. |
||
| 409 | * |
||
| 410 | * @param Member|null $member |
||
| 411 | * |
||
| 412 | * @return bool |
||
| 413 | */ |
||
| 414 | public function canCreateEnvironments($member = null) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @return DataList |
||
| 427 | */ |
||
| 428 | public function DataArchives() { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Return all archives which are "manual upload requests", |
||
| 435 | * meaning they don't have a file attached to them (yet). |
||
| 436 | * |
||
| 437 | * @return DataList |
||
| 438 | */ |
||
| 439 | public function PendingManualUploadDataArchives() { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Build an environment variable array to be used with this project. |
||
| 445 | * |
||
| 446 | * This is relevant if every project needs to use an individual SSH pubkey. |
||
| 447 | * |
||
| 448 | * Include this with all Gitonomy\Git\Repository, and |
||
| 449 | * \Symfony\Component\Process\Processes. |
||
| 450 | * |
||
| 451 | * @return array |
||
| 452 | */ |
||
| 453 | public function getProcessEnv() { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Get a string of people allowed to view this project |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | public function getViewersList() { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return DNData |
||
| 479 | */ |
||
| 480 | public function DNData() { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Provides a DNBuildList of builds found in this project. |
||
| 486 | * |
||
| 487 | * @return DNReferenceList |
||
| 488 | */ |
||
| 489 | public function DNBuildList() { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Provides a list of the branches in this project. |
||
| 495 | * |
||
| 496 | * @return DNBranchList |
||
| 497 | */ |
||
| 498 | public function DNBranchList() { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Provides a list of the tags in this project. |
||
| 507 | * |
||
| 508 | * @return DNReferenceList |
||
| 509 | */ |
||
| 510 | public function DNTagList() { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @return false|Gitonomy\Git\Repository |
||
| 519 | */ |
||
| 520 | public function getRepository() { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Resolve a git reference like a branch or tag into a SHA. |
||
| 530 | * @return bool|string |
||
| 531 | */ |
||
| 532 | public function resolveRevision($value) { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Provides a list of environments found in this project. |
||
| 548 | * CAUTION: filterByCallback will change this into an ArrayList! |
||
| 549 | * |
||
| 550 | * @return ArrayList |
||
| 551 | */ |
||
| 552 | public function DNEnvironmentList() { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * @param string $usage |
||
| 571 | * @return ArrayList |
||
| 572 | */ |
||
| 573 | public function EnvironmentsByUsage($usage) { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Returns a map of envrionment name to build name |
||
| 579 | * |
||
| 580 | * @return false|DNDeployment |
||
| 581 | */ |
||
| 582 | public function currentBuilds() { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @param string |
||
| 595 | * @return string |
||
| 596 | */ |
||
| 597 | public function Link($action = '') { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @return string|null |
||
| 603 | */ |
||
| 604 | public function CreateEnvironmentLink() { |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @return string |
||
| 613 | */ |
||
| 614 | public function ToggleStarLink() { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @return bool |
||
| 620 | */ |
||
| 621 | public function IsStarred() { |
||
| 632 | |||
| 633 | /** |
||
| 634 | * @param string $action |
||
| 635 | * @return string |
||
| 636 | */ |
||
| 637 | public function APILink($action) { |
||
| 640 | |||
| 641 | /** |
||
| 642 | * @return FieldList |
||
| 643 | */ |
||
| 644 | public function getCMSFields() { |
||
| 702 | |||
| 703 | /** |
||
| 704 | * If there isn't a capistrano env project folder, show options to create one |
||
| 705 | * |
||
| 706 | * @param FieldList $fields |
||
| 707 | */ |
||
| 708 | public function setCreateProjectFolderField(&$fields) { |
||
| 725 | |||
| 726 | /** |
||
| 727 | * @return boolean |
||
| 728 | */ |
||
| 729 | public function projectFolderExists() { |
||
| 732 | |||
| 733 | /** |
||
| 734 | * @return bool |
||
| 735 | */ |
||
| 736 | public function repoExists() { |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Setup a job to clone a git repository. |
||
| 742 | * @return string resque token |
||
| 743 | */ |
||
| 744 | public function cloneRepo() { |
||
| 759 | |||
| 760 | /** |
||
| 761 | * @return string |
||
| 762 | */ |
||
| 763 | public function getLocalCVSPath() { |
||
| 766 | |||
| 767 | public function onBeforeWrite() { |
||
| 774 | |||
| 775 | public function onAfterWrite() { |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Delete related environments and folders |
||
| 790 | */ |
||
| 791 | public function onAfterDelete() { |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Fetch the public key for this project. |
||
| 826 | * |
||
| 827 | * @return string|void |
||
| 828 | */ |
||
| 829 | public function getPublicKey() { |
||
| 836 | |||
| 837 | /** |
||
| 838 | * This returns that path of the public key if a key directory is set. It doesn't check whether the file exists. |
||
| 839 | * |
||
| 840 | * @return string|null |
||
| 841 | */ |
||
| 842 | public function getPublicKeyPath() { |
||
| 848 | |||
| 849 | /** |
||
| 850 | * This returns that path of the private key if a key directory is set. It doesn't check whether the file exists. |
||
| 851 | * |
||
| 852 | * @return string|null |
||
| 853 | */ |
||
| 854 | public function getPrivateKeyPath() { |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Returns the location of the projects key dir if one exists. |
||
| 866 | * |
||
| 867 | * @return string|null |
||
| 868 | */ |
||
| 869 | public function getKeyDir() { |
||
| 880 | |||
| 881 | /** |
||
| 882 | * Provide current repository URL to the users. |
||
| 883 | * |
||
| 884 | * @return void|string |
||
| 885 | */ |
||
| 886 | public function getRepositoryURL() { |
||
| 892 | |||
| 893 | /** |
||
| 894 | * Get a ViewableData structure describing the UI tool that lets the user view the repository code |
||
| 895 | * |
||
| 896 | * @return ArrayData |
||
| 897 | */ |
||
| 898 | public function getRepositoryInterface() { |
||
| 938 | |||
| 939 | /** |
||
| 940 | * Convenience wrapper for a single permission code. |
||
| 941 | * |
||
| 942 | * @param string $code |
||
| 943 | * @return SS_List |
||
| 944 | */ |
||
| 945 | public function whoIsAllowed($code) { |
||
| 948 | |||
| 949 | /** |
||
| 950 | * List members who have $codes on this project. |
||
| 951 | * Does not support Permission::DENY_PERMISSION malarky, same as Permission::get_groups_by_permission anyway... |
||
| 952 | * |
||
| 953 | * @param array|string $codes |
||
| 954 | * @return SS_List |
||
| 955 | */ |
||
| 956 | public function whoIsAllowedAny($codes) { |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Convenience wrapper for a single permission code. |
||
| 978 | * |
||
| 979 | * @param string $code |
||
| 980 | * @param Member|null $member |
||
| 981 | * |
||
| 982 | * @return bool |
||
| 983 | */ |
||
| 984 | public function allowed($code, $member = null) { |
||
| 987 | |||
| 988 | /** |
||
| 989 | * Checks if a group is allowed to the project and the permission code |
||
| 990 | * |
||
| 991 | * @param string $permissionCode |
||
| 992 | * @param Group $group |
||
| 993 | * |
||
| 994 | * @return bool |
||
| 995 | */ |
||
| 996 | public function groupAllowed($permissionCode, Group $group) { |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Check if member has a permission code in this project. |
||
| 1010 | * |
||
| 1011 | * @param array|string $codes |
||
| 1012 | * @param Member|null $member |
||
| 1013 | * |
||
| 1014 | * @return bool |
||
| 1015 | */ |
||
| 1016 | public function allowedAny($codes, $member = null) { |
||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Checks if the environment has been fully built. |
||
| 1031 | * |
||
| 1032 | * @return bool |
||
| 1033 | */ |
||
| 1034 | public function isProjectReady() { |
||
| 1057 | |||
| 1058 | /** |
||
| 1059 | * Returns a list of environments still being created. |
||
| 1060 | * |
||
| 1061 | * @return SS_List |
||
| 1062 | */ |
||
| 1063 | public function getRunningEnvironmentCreations() { |
||
| 1067 | |||
| 1068 | /** |
||
| 1069 | * Returns a list of initial environments created for this project. |
||
| 1070 | * |
||
| 1071 | * @return DataList |
||
| 1072 | */ |
||
| 1073 | public function getInitialEnvironmentCreations() { |
||
| 1076 | |||
| 1077 | /** |
||
| 1078 | * Only returns initial environments that are being created. |
||
| 1079 | * |
||
| 1080 | * @return DataList |
||
| 1081 | */ |
||
| 1082 | public function getRunningInitialEnvironmentCreations() { |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * Returns a list of completed initial environment creations. This includes failed tasks. |
||
| 1089 | * |
||
| 1090 | * @return DataList |
||
| 1091 | */ |
||
| 1092 | public function getCompleteInitialEnvironmentCreations() { |
||
| 1096 | |||
| 1097 | /** |
||
| 1098 | * @param Member $member |
||
| 1099 | * |
||
| 1100 | * @return bool |
||
| 1101 | */ |
||
| 1102 | public function canCreate($member = null) { |
||
| 1117 | |||
| 1118 | /** |
||
| 1119 | * This is a proxy call to gitonmy that caches the information per project and sha |
||
| 1120 | * |
||
| 1121 | * @param string $sha |
||
| 1122 | * @return false|\Gitonomy\Git\Commit |
||
| 1123 | */ |
||
| 1124 | public function getCommit($sha) { |
||
| 1142 | |||
| 1143 | /** |
||
| 1144 | * @param \Gitonomy\Git\Commit $commit |
||
| 1145 | * @return string |
||
| 1146 | */ |
||
| 1147 | View Code Duplication | public function getCommitMessage(\Gitonomy\Git\Commit $commit) { |
|
| 1156 | |||
| 1157 | /** |
||
| 1158 | * get the commit "subject", getCommitMessage get the full message |
||
| 1159 | * |
||
| 1160 | * @param \Gitonomy\Git\Commit $commit |
||
| 1161 | * @return string |
||
| 1162 | */ |
||
| 1163 | View Code Duplication | public function getCommitSubjectMessage(\Gitonomy\Git\Commit $commit) { |
|
| 1172 | |||
| 1173 | /** |
||
| 1174 | * @param \Gitonomy\Git\Commit $commit |
||
| 1175 | * @return mixed |
||
| 1176 | */ |
||
| 1177 | public function getCommitTags(\Gitonomy\Git\Commit $commit) { |
||
| 1189 | |||
| 1190 | /** |
||
| 1191 | * Setup a gridfield for the environment configs |
||
| 1192 | * |
||
| 1193 | * @param FieldList $fields |
||
| 1194 | * @param GridField $environments |
||
| 1195 | */ |
||
| 1196 | protected function setEnvironmentFields(&$fields, $environments) { |
||
| 1214 | |||
| 1215 | /** |
||
| 1216 | * @return string |
||
| 1217 | */ |
||
| 1218 | protected function getProjectFolderPath() { |
||
| 1221 | |||
| 1222 | /** |
||
| 1223 | * @return ValidationResult |
||
| 1224 | */ |
||
| 1225 | protected function validate() { |
||
| 1251 | |||
| 1252 | } |
||
| 1253 | |||
| 1254 |
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.