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 | public static $db = [ |
||
| 21 | "Name" => "Varchar", |
||
| 22 | "CVSPath" => "Varchar(255)", |
||
| 23 | "DiskQuotaMB" => "Int", |
||
| 24 | "AllowedEnvironmentType" => "Varchar(255)", |
||
| 25 | ]; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | public static $has_many = [ |
||
| 31 | "Environments" => "DNEnvironment", |
||
| 32 | "CreateEnvironments" => "DNCreateEnvironment" |
||
| 33 | ]; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | public static $many_many = [ |
||
| 39 | "Viewers" => "Group", |
||
| 40 | 'StarredBy' => "Member" |
||
| 41 | ]; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | public static $summary_fields = [ |
||
| 47 | "Name", |
||
| 48 | "ViewersList", |
||
| 49 | ]; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | public static $searchable_fields = [ |
||
| 55 | "Name", |
||
| 56 | ]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private static $singular_name = 'Project'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private static $plural_name = 'Projects'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | private static $default_sort = 'Name'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * In-memory cache for currentBuilds per environment since fetching them from |
||
| 75 | * disk is pretty resource hungry. |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected static $relation_cache = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var bool|Member |
||
| 83 | */ |
||
| 84 | protected static $_current_member_cache = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Display the repository URL on the project page. |
||
| 88 | * |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | private static $show_repository_url = false; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * In-memory cache to determine whether clone repo was called. |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | private static $has_cloned_cache = []; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Whitelist configuration that describes how to convert a repository URL into a link |
||
| 101 | * to a web user interface for that URL |
||
| 102 | * |
||
| 103 | * Consists of a hash of "full.lower.case.domain" => {configuration} key/value pairs |
||
| 104 | * |
||
| 105 | * {configuration} can either be boolean true to auto-detect both the host and the |
||
| 106 | * name of the UI provider, or a nested array that overrides either one or both |
||
| 107 | * of the auto-detected valyes |
||
| 108 | * |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | static private $repository_interfaces = [ |
||
| 112 | 'github.com' => [ |
||
| 113 | 'icon' => 'deploynaut/img/github.png', |
||
| 114 | 'name' => 'Github.com', |
||
| 115 | ], |
||
| 116 | 'bitbucket.org' => [ |
||
| 117 | 'commit' => 'commits', |
||
| 118 | 'name' => 'Bitbucket.org', |
||
| 119 | ], |
||
| 120 | 'repo.or.cz' => [ |
||
| 121 | 'scheme' => 'http', |
||
| 122 | 'name' => 'repo.or.cz', |
||
| 123 | 'regex' => ['^(.*)$' => '/w$1'], |
||
| 124 | ], |
||
| 125 | |||
| 126 | /* Example for adding your own gitlab repository and override all auto-detected values (with their defaults) |
||
| 127 | 'gitlab.mysite.com' => array( |
||
| 128 | 'icon' => 'deploynaut/img/git.png', |
||
| 129 | 'host' => 'gitlab.mysite.com', |
||
| 130 | 'name' => 'Gitlab', |
||
| 131 | 'regex' => array('.git$' => ''), |
||
| 132 | 'commit' => "commit" |
||
| 133 | ), |
||
| 134 | */ |
||
| 135 | ]; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Used by the sync task |
||
| 139 | * |
||
| 140 | * @param string $path |
||
| 141 | * @return \DNProject |
||
| 142 | */ |
||
| 143 | public static function create_from_path($path) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * This will clear the cache for the git getters and should be called when the local git repo is updated |
||
| 158 | */ |
||
| 159 | public function clearGitCache() { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return \Zend_Cache_Frontend_Output |
||
| 168 | */ |
||
| 169 | public static function get_git_cache() { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Return the used quota in MB. |
||
| 178 | * |
||
| 179 | * @param int $round Number of decimal places to round to |
||
| 180 | * @return double The used quota size in MB |
||
| 181 | */ |
||
| 182 | public function getUsedQuotaMB($round = 2) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Getter for DiskQuotaMB field to provide a default for existing |
||
| 197 | * records that have no quota field set, as it will need to default |
||
| 198 | * to a globally set size. |
||
| 199 | * |
||
| 200 | * @return string|int The quota size in MB |
||
| 201 | */ |
||
| 202 | public function getDiskQuotaMB() { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Has the disk quota been exceeded? |
||
| 215 | * |
||
| 216 | * @return boolean |
||
| 217 | */ |
||
| 218 | public function HasExceededDiskQuota() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Is there a disk quota set for this project? |
||
| 224 | * |
||
| 225 | * @return boolean |
||
| 226 | */ |
||
| 227 | public function HasDiskQuota() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the current disk quota usage as a percentage |
||
| 233 | * |
||
| 234 | * @return int |
||
| 235 | */ |
||
| 236 | public function DiskQuotaUsagePercent() { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get the menu to be shown on projects |
||
| 246 | * |
||
| 247 | * @return ArrayList |
||
| 248 | */ |
||
| 249 | public function Menu() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Is this project currently at the root level of the controller that handles it? |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | public function isCurrent() { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Return the current object from $this->Menu() |
||
| 280 | * Good for making titles and things |
||
| 281 | * |
||
| 282 | * @return DataObject |
||
| 283 | */ |
||
| 284 | public function CurrentMenu() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Is this project currently in a controller that is handling it or performing a sub-task? |
||
| 290 | * |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | public function isSection() { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Restrict access to viewing this project |
||
| 301 | * |
||
| 302 | * @param Member|null $member |
||
| 303 | * @return boolean |
||
| 304 | */ |
||
| 305 | public function canView($member = null) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param Member|null $member |
||
| 319 | * |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | View Code Duplication | public function canRestore($member = null) { |
|
| 338 | |||
| 339 | /** |
||
| 340 | * @param Member|null $member |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | View Code Duplication | public function canBackup($member = null) { |
|
| 359 | |||
| 360 | /** |
||
| 361 | * @param Member|null $member |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | View Code Duplication | public function canUploadArchive($member = null) { |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @param Member|null $member |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | View Code Duplication | public function canDownloadArchive($member = null) { |
|
| 401 | |||
| 402 | /** |
||
| 403 | * This is a permission check for the front-end only. |
||
| 404 | * |
||
| 405 | * Only admins can create environments for now. Also, we need to check the value |
||
| 406 | * of AllowedEnvironmentType which dictates which backend to use to render the form. |
||
| 407 | * |
||
| 408 | * @param Member|null $member |
||
| 409 | * |
||
| 410 | * @return bool |
||
| 411 | */ |
||
| 412 | public function canCreateEnvironments($member = null) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @return DataList |
||
| 425 | */ |
||
| 426 | public function DataArchives() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Return all archives which are "manual upload requests", |
||
| 433 | * meaning they don't have a file attached to them (yet). |
||
| 434 | * |
||
| 435 | * @return DataList |
||
| 436 | */ |
||
| 437 | public function PendingManualUploadDataArchives() { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Build an environment variable array to be used with this project. |
||
| 443 | * |
||
| 444 | * This is relevant if every project needs to use an individual SSH pubkey. |
||
| 445 | * |
||
| 446 | * Include this with all Gitonomy\Git\Repository, and |
||
| 447 | * \Symfony\Component\Process\Processes. |
||
| 448 | * |
||
| 449 | * @return array |
||
| 450 | */ |
||
| 451 | public function getProcessEnv() { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get a string of people allowed to view this project |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | public function getViewersList() { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @return DNData |
||
| 477 | */ |
||
| 478 | public function DNData() { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Provides a DNBuildList of builds found in this project. |
||
| 484 | * |
||
| 485 | * @return DNReferenceList |
||
| 486 | */ |
||
| 487 | public function DNBuildList() { |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Provides a list of the branches in this project. |
||
| 493 | * |
||
| 494 | * @return DNBranchList |
||
| 495 | */ |
||
| 496 | public function DNBranchList() { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Provides a list of the tags in this project. |
||
| 505 | * |
||
| 506 | * @return DNReferenceList |
||
| 507 | */ |
||
| 508 | public function DNTagList() { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @return false|Gitonomy\Git\Repository |
||
| 517 | */ |
||
| 518 | public function getRepository() { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Provides a list of environments found in this project. |
||
| 528 | * CAUTION: filterByCallback will change this into an ArrayList! |
||
| 529 | * |
||
| 530 | * @return ArrayList |
||
| 531 | */ |
||
| 532 | public function DNEnvironmentList() { |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @param string $usage |
||
| 551 | * @return ArrayList |
||
| 552 | */ |
||
| 553 | public function EnvironmentsByUsage($usage) { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Returns a map of envrionment name to build name |
||
| 559 | * |
||
| 560 | * @return false|DNDeployment |
||
| 561 | */ |
||
| 562 | public function currentBuilds() { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @param string |
||
| 575 | * @return string |
||
| 576 | */ |
||
| 577 | public function Link($action = '') { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @return string|null |
||
| 583 | */ |
||
| 584 | public function CreateEnvironmentLink() { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @return string |
||
| 593 | */ |
||
| 594 | public function ToggleStarLink() { |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @return bool |
||
| 600 | */ |
||
| 601 | public function IsStarred() { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @param string $action |
||
| 615 | * @return string |
||
| 616 | */ |
||
| 617 | public function APILink($action) { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @return FieldList |
||
| 623 | */ |
||
| 624 | public function getCMSFields() { |
||
| 678 | |||
| 679 | /** |
||
| 680 | * If there isn't a capistrano env project folder, show options to create one |
||
| 681 | * |
||
| 682 | * @param FieldList $fields |
||
| 683 | */ |
||
| 684 | public function setCreateProjectFolderField(&$fields) { |
||
| 701 | |||
| 702 | /** |
||
| 703 | * @return boolean |
||
| 704 | */ |
||
| 705 | public function projectFolderExists() { |
||
| 708 | |||
| 709 | /** |
||
| 710 | * @return bool |
||
| 711 | */ |
||
| 712 | public function repoExists() { |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Setup a job to clone a git repository. |
||
| 718 | * @return string resque token |
||
| 719 | */ |
||
| 720 | public function cloneRepo() { |
||
| 735 | |||
| 736 | /** |
||
| 737 | * @return string |
||
| 738 | */ |
||
| 739 | public function getLocalCVSPath() { |
||
| 742 | |||
| 743 | public function onBeforeWrite() { |
||
| 750 | |||
| 751 | public function onAfterWrite() { |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Delete related environments and folders |
||
| 766 | */ |
||
| 767 | public function onAfterDelete() { |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Fetch the public key for this project. |
||
| 793 | * |
||
| 794 | * @return string|void |
||
| 795 | */ |
||
| 796 | public function getPublicKey() { |
||
| 803 | |||
| 804 | /** |
||
| 805 | * This returns that path of the public key if a key directory is set. It doesn't check whether the file exists. |
||
| 806 | * |
||
| 807 | * @return string|null |
||
| 808 | */ |
||
| 809 | public function getPublicKeyPath() { |
||
| 815 | |||
| 816 | /** |
||
| 817 | * This returns that path of the private key if a key directory is set. It doesn't check whether the file exists. |
||
| 818 | * |
||
| 819 | * @return string|null |
||
| 820 | */ |
||
| 821 | public function getPrivateKeyPath() { |
||
| 830 | |||
| 831 | /** |
||
| 832 | * Returns the location of the projects key dir if one exists. |
||
| 833 | * |
||
| 834 | * @return string|null |
||
| 835 | */ |
||
| 836 | public function getKeyDir() { |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Provide current repository URL to the users. |
||
| 850 | * |
||
| 851 | * @return void|string |
||
| 852 | */ |
||
| 853 | public function getRepositoryURL() { |
||
| 859 | |||
| 860 | /** |
||
| 861 | * Get a ViewableData structure describing the UI tool that lets the user view the repository code |
||
| 862 | * |
||
| 863 | * @return ArrayData |
||
| 864 | */ |
||
| 865 | public function getRepositoryInterface() { |
||
| 905 | |||
| 906 | /** |
||
| 907 | * Convenience wrapper for a single permission code. |
||
| 908 | * |
||
| 909 | * @param string $code |
||
| 910 | * @return SS_List |
||
| 911 | */ |
||
| 912 | public function whoIsAllowed($code) { |
||
| 915 | |||
| 916 | /** |
||
| 917 | * List members who have $codes on this project. |
||
| 918 | * Does not support Permission::DENY_PERMISSION malarky, same as Permission::get_groups_by_permission anyway... |
||
| 919 | * |
||
| 920 | * @param array|string $codes |
||
| 921 | * @return SS_List |
||
| 922 | */ |
||
| 923 | public function whoIsAllowedAny($codes) { |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Convenience wrapper for a single permission code. |
||
| 945 | * |
||
| 946 | * @param string $code |
||
| 947 | * @param Member|null $member |
||
| 948 | * |
||
| 949 | * @return bool |
||
| 950 | */ |
||
| 951 | public function allowed($code, $member = null) { |
||
| 954 | |||
| 955 | /** |
||
| 956 | * Checks if a group is allowed to the project and the permission code |
||
| 957 | * |
||
| 958 | * @param string $permissionCode |
||
| 959 | * @param Group $group |
||
| 960 | * |
||
| 961 | * @return bool |
||
| 962 | */ |
||
| 963 | public function groupAllowed($permissionCode, Group $group) { |
||
| 974 | |||
| 975 | /** |
||
| 976 | * Check if member has a permission code in this project. |
||
| 977 | * |
||
| 978 | * @param array|string $codes |
||
| 979 | * @param Member|null $member |
||
| 980 | * |
||
| 981 | * @return bool |
||
| 982 | */ |
||
| 983 | public function allowedAny($codes, $member = null) { |
||
| 995 | |||
| 996 | /** |
||
| 997 | * Checks if the environment has been fully built. |
||
| 998 | * |
||
| 999 | * @return bool |
||
| 1000 | */ |
||
| 1001 | public function isProjectReady() { |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * Returns a list of environments still being created. |
||
| 1027 | * |
||
| 1028 | * @return SS_List |
||
| 1029 | */ |
||
| 1030 | public function getRunningEnvironmentCreations() { |
||
| 1034 | |||
| 1035 | /** |
||
| 1036 | * Returns a list of initial environments created for this project. |
||
| 1037 | * |
||
| 1038 | * @return DataList |
||
| 1039 | */ |
||
| 1040 | public function getInitialEnvironmentCreations() { |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * Only returns initial environments that are being created. |
||
| 1046 | * |
||
| 1047 | * @return DataList |
||
| 1048 | */ |
||
| 1049 | public function getRunningInitialEnvironmentCreations() { |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * Returns a list of completed initial environment creations. This includes failed tasks. |
||
| 1056 | * |
||
| 1057 | * @return DataList |
||
| 1058 | */ |
||
| 1059 | public function getCompleteInitialEnvironmentCreations() { |
||
| 1063 | |||
| 1064 | /** |
||
| 1065 | * @param Member $member |
||
| 1066 | * |
||
| 1067 | * @return bool |
||
| 1068 | */ |
||
| 1069 | public function canCreate($member = null) { |
||
| 1084 | |||
| 1085 | /** |
||
| 1086 | * This is a proxy call to gitonmy that caches the information per project and sha |
||
| 1087 | * |
||
| 1088 | * @param string $sha |
||
| 1089 | * @return false|\Gitonomy\Git\Commit |
||
| 1090 | */ |
||
| 1091 | public function getCommit($sha) { |
||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * @param \Gitonomy\Git\Commit $commit |
||
| 1108 | * @return string |
||
| 1109 | */ |
||
| 1110 | public function getCommitMessage(\Gitonomy\Git\Commit $commit) { |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * @param \Gitonomy\Git\Commit $commit |
||
| 1122 | * @return mixed |
||
| 1123 | */ |
||
| 1124 | public function getCommitTags(\Gitonomy\Git\Commit $commit) { |
||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * Setup a gridfield for the environment configs |
||
| 1139 | * |
||
| 1140 | * @param FieldList $fields |
||
| 1141 | * @param GridField $environments |
||
| 1142 | */ |
||
| 1143 | protected function setEnvironmentFields(&$fields, $environments) { |
||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * @return string |
||
| 1164 | */ |
||
| 1165 | protected function getProjectFolderPath() { |
||
| 1168 | |||
| 1169 | /** |
||
| 1170 | * @return ValidationResult |
||
| 1171 | */ |
||
| 1172 | protected function validate() { |
||
| 1198 | |||
| 1199 | } |
||
| 1200 | |||
| 1201 |
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.