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 | "CVSPath" => "Varchar(255)", |
||
| 23 | "DiskQuotaMB" => "Int", |
||
| 24 | "AllowedEnvironmentType" => "Varchar(255)", |
||
| 25 | ]; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private static $has_many = [ |
||
| 31 | "Environments" => "DNEnvironment", |
||
| 32 | "CreateEnvironments" => "DNCreateEnvironment", |
||
| 33 | "Fetches" => "DNGitFetch" |
||
| 34 | ]; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private static $many_many = [ |
||
| 40 | "Viewers" => "Group", |
||
| 41 | 'StarredBy' => "Member" |
||
| 42 | ]; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | private static $summary_fields = [ |
||
| 48 | "Name", |
||
| 49 | "ViewersList", |
||
| 50 | ]; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private static $searchable_fields = [ |
||
| 56 | "Name", |
||
| 57 | ]; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private static $singular_name = 'Project'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private static $plural_name = 'Projects'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private static $default_sort = 'Name'; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * In-memory cache for currentBuilds per environment since fetching them from |
||
| 76 | * disk is pretty resource hungry. |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected static $relation_cache = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var bool|Member |
||
| 84 | */ |
||
| 85 | protected static $_current_member_cache = null; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Display the repository URL on the project page. |
||
| 89 | * |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | private static $show_repository_url = false; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * In-memory cache to determine whether clone repo was called. |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | private static $has_cloned_cache = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Whitelist configuration that describes how to convert a repository URL into a link |
||
| 102 | * to a web user interface for that URL |
||
| 103 | * |
||
| 104 | * Consists of a hash of "full.lower.case.domain" => {configuration} key/value pairs |
||
| 105 | * |
||
| 106 | * {configuration} can either be boolean true to auto-detect both the host and the |
||
| 107 | * name of the UI provider, or a nested array that overrides either one or both |
||
| 108 | * of the auto-detected values |
||
| 109 | * |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | private static $repository_interfaces = [ |
||
| 113 | 'github.com' => [ |
||
| 114 | 'icon' => 'deploynaut/img/github.png', |
||
| 115 | 'name' => 'Github.com', |
||
| 116 | ], |
||
| 117 | 'bitbucket.org' => [ |
||
| 118 | 'commit' => 'commits', |
||
| 119 | 'name' => 'Bitbucket.org', |
||
| 120 | ], |
||
| 121 | 'repo.or.cz' => [ |
||
| 122 | 'scheme' => 'http', |
||
| 123 | 'name' => 'repo.or.cz', |
||
| 124 | 'regex' => ['^(.*)$' => '/w$1'], |
||
| 125 | ], |
||
| 126 | |||
| 127 | /* Example for adding your own gitlab repository and override all auto-detected values (with their defaults) |
||
| 128 | 'gitlab.mysite.com' => array( |
||
| 129 | 'icon' => 'deploynaut/img/git.png', |
||
| 130 | 'host' => 'gitlab.mysite.com', |
||
| 131 | 'name' => 'Gitlab', |
||
| 132 | 'regex' => array('.git$' => ''), |
||
| 133 | 'commit' => "commit" |
||
| 134 | ), |
||
| 135 | */ |
||
| 136 | ]; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Used by the sync task |
||
| 140 | * |
||
| 141 | * @param string $path |
||
| 142 | * @return \DNProject |
||
| 143 | */ |
||
| 144 | public static function create_from_path($path) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * This will clear the cache for the git getters and should be called when the local git repo is updated |
||
| 159 | */ |
||
| 160 | public function clearGitCache() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return \Zend_Cache_Frontend_Output |
||
| 169 | */ |
||
| 170 | public static function get_git_cache() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Return the used quota in MB. |
||
| 179 | * |
||
| 180 | * @param int $round Number of decimal places to round to |
||
| 181 | * @return double The used quota size in MB |
||
| 182 | */ |
||
| 183 | public function getUsedQuotaMB($round = 2) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Getter for DiskQuotaMB field to provide a default for existing |
||
| 198 | * records that have no quota field set, as it will need to default |
||
| 199 | * to a globally set size. |
||
| 200 | * |
||
| 201 | * @return string|int The quota size in MB |
||
| 202 | */ |
||
| 203 | public function getDiskQuotaMB() { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Has the disk quota been exceeded? |
||
| 216 | * |
||
| 217 | * @return boolean |
||
| 218 | */ |
||
| 219 | public function HasExceededDiskQuota() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Is there a disk quota set for this project? |
||
| 225 | * |
||
| 226 | * @return boolean |
||
| 227 | */ |
||
| 228 | public function HasDiskQuota() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Returns the current disk quota usage as a percentage |
||
| 234 | * |
||
| 235 | * @return int |
||
| 236 | */ |
||
| 237 | public function DiskQuotaUsagePercent() { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get the menu to be shown on projects |
||
| 247 | * |
||
| 248 | * @return ArrayList |
||
| 249 | */ |
||
| 250 | public function Menu() { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Is this project currently at the root level of the controller that handles it? |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | public function isCurrent() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Return the current object from $this->Menu() |
||
| 281 | * Good for making titles and things |
||
| 282 | * |
||
| 283 | * @return DataObject |
||
| 284 | */ |
||
| 285 | public function CurrentMenu() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Is this project currently in a controller that is handling it or performing a sub-task? |
||
| 291 | * |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | public function isSection() { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Restrict access to viewing this project |
||
| 302 | * |
||
| 303 | * @param Member|null $member |
||
| 304 | * @return boolean |
||
| 305 | */ |
||
| 306 | public function canView($member = null) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param Member|null $member |
||
| 320 | * |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | View Code Duplication | public function canRestore($member = null) { |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @param Member|null $member |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | View Code Duplication | public function canBackup($member = null) { |
|
| 360 | |||
| 361 | /** |
||
| 362 | * @param Member|null $member |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | View Code Duplication | public function canUploadArchive($member = null) { |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @param Member|null $member |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | View Code Duplication | public function canDownloadArchive($member = null) { |
|
| 402 | |||
| 403 | /** |
||
| 404 | * This is a permission check for the front-end only. |
||
| 405 | * |
||
| 406 | * Only admins can create environments for now. Also, we need to check the value |
||
| 407 | * of AllowedEnvironmentType which dictates which backend to use to render the form. |
||
| 408 | * |
||
| 409 | * @param Member|null $member |
||
| 410 | * |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | public function canCreateEnvironments($member = null) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @return DataList |
||
| 426 | */ |
||
| 427 | public function DataArchives() { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Return all archives which are "manual upload requests", |
||
| 434 | * meaning they don't have a file attached to them (yet). |
||
| 435 | * |
||
| 436 | * @return DataList |
||
| 437 | */ |
||
| 438 | public function PendingManualUploadDataArchives() { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Build an environment variable array to be used with this project. |
||
| 444 | * |
||
| 445 | * This is relevant if every project needs to use an individual SSH pubkey. |
||
| 446 | * |
||
| 447 | * Include this with all Gitonomy\Git\Repository, and |
||
| 448 | * \Symfony\Component\Process\Processes. |
||
| 449 | * |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | public function getProcessEnv() { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Get a string of people allowed to view this project |
||
| 469 | * |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | public function getViewersList() { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @return DNData |
||
| 478 | */ |
||
| 479 | public function DNData() { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Provides a DNBuildList of builds found in this project. |
||
| 485 | * |
||
| 486 | * @return DNReferenceList |
||
| 487 | */ |
||
| 488 | public function DNBuildList() { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Provides a list of the branches in this project. |
||
| 494 | * |
||
| 495 | * @return DNBranchList |
||
| 496 | */ |
||
| 497 | public function DNBranchList() { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Provides a list of the tags in this project. |
||
| 506 | * |
||
| 507 | * @return DNReferenceList |
||
| 508 | */ |
||
| 509 | public function DNTagList() { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @return false|Gitonomy\Git\Repository |
||
| 518 | */ |
||
| 519 | public function getRepository() { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Provides a list of environments found in this project. |
||
| 529 | * CAUTION: filterByCallback will change this into an ArrayList! |
||
| 530 | * |
||
| 531 | * @return ArrayList |
||
| 532 | */ |
||
| 533 | public function DNEnvironmentList() { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @param string $usage |
||
| 552 | * @return ArrayList |
||
| 553 | */ |
||
| 554 | public function EnvironmentsByUsage($usage) { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Returns a map of envrionment name to build name |
||
| 560 | * |
||
| 561 | * @return false|DNDeployment |
||
| 562 | */ |
||
| 563 | public function currentBuilds() { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param string |
||
| 576 | * @return string |
||
| 577 | */ |
||
| 578 | public function Link($action = '') { |
||
| 581 | |||
| 582 | /** |
||
| 583 | * @return string|null |
||
| 584 | */ |
||
| 585 | public function CreateEnvironmentLink() { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * @return string |
||
| 594 | */ |
||
| 595 | public function ToggleStarLink() { |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @return bool |
||
| 601 | */ |
||
| 602 | public function IsStarred() { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @param string $action |
||
| 616 | * @return string |
||
| 617 | */ |
||
| 618 | public function APILink($action) { |
||
| 621 | |||
| 622 | /** |
||
| 623 | * @return FieldList |
||
| 624 | */ |
||
| 625 | public function getCMSFields() { |
||
| 679 | |||
| 680 | /** |
||
| 681 | * If there isn't a capistrano env project folder, show options to create one |
||
| 682 | * |
||
| 683 | * @param FieldList $fields |
||
| 684 | */ |
||
| 685 | public function setCreateProjectFolderField(&$fields) { |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @return boolean |
||
| 705 | */ |
||
| 706 | public function projectFolderExists() { |
||
| 709 | |||
| 710 | /** |
||
| 711 | * @return bool |
||
| 712 | */ |
||
| 713 | public function repoExists() { |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Setup a job to clone a git repository. |
||
| 719 | * @return string resque token |
||
| 720 | */ |
||
| 721 | public function cloneRepo() { |
||
| 736 | |||
| 737 | /** |
||
| 738 | * @return string |
||
| 739 | */ |
||
| 740 | public function getLocalCVSPath() { |
||
| 743 | |||
| 744 | public function onBeforeWrite() { |
||
| 751 | |||
| 752 | public function onAfterWrite() { |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Delete related environments and folders |
||
| 767 | */ |
||
| 768 | public function onAfterDelete() { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Fetch the public key for this project. |
||
| 803 | * |
||
| 804 | * @return string|void |
||
| 805 | */ |
||
| 806 | public function getPublicKey() { |
||
| 813 | |||
| 814 | /** |
||
| 815 | * This returns that path of the public key if a key directory is set. It doesn't check whether the file exists. |
||
| 816 | * |
||
| 817 | * @return string|null |
||
| 818 | */ |
||
| 819 | public function getPublicKeyPath() { |
||
| 825 | |||
| 826 | /** |
||
| 827 | * This returns that path of the private key if a key directory is set. It doesn't check whether the file exists. |
||
| 828 | * |
||
| 829 | * @return string|null |
||
| 830 | */ |
||
| 831 | public function getPrivateKeyPath() { |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Returns the location of the projects key dir if one exists. |
||
| 843 | * |
||
| 844 | * @return string|null |
||
| 845 | */ |
||
| 846 | public function getKeyDir() { |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Provide current repository URL to the users. |
||
| 860 | * |
||
| 861 | * @return void|string |
||
| 862 | */ |
||
| 863 | public function getRepositoryURL() { |
||
| 869 | |||
| 870 | /** |
||
| 871 | * Get a ViewableData structure describing the UI tool that lets the user view the repository code |
||
| 872 | * |
||
| 873 | * @return ArrayData |
||
| 874 | */ |
||
| 875 | public function getRepositoryInterface() { |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Convenience wrapper for a single permission code. |
||
| 918 | * |
||
| 919 | * @param string $code |
||
| 920 | * @return SS_List |
||
| 921 | */ |
||
| 922 | public function whoIsAllowed($code) { |
||
| 925 | |||
| 926 | /** |
||
| 927 | * List members who have $codes on this project. |
||
| 928 | * Does not support Permission::DENY_PERMISSION malarky, same as Permission::get_groups_by_permission anyway... |
||
| 929 | * |
||
| 930 | * @param array|string $codes |
||
| 931 | * @return SS_List |
||
| 932 | */ |
||
| 933 | public function whoIsAllowedAny($codes) { |
||
| 952 | |||
| 953 | /** |
||
| 954 | * Convenience wrapper for a single permission code. |
||
| 955 | * |
||
| 956 | * @param string $code |
||
| 957 | * @param Member|null $member |
||
| 958 | * |
||
| 959 | * @return bool |
||
| 960 | */ |
||
| 961 | public function allowed($code, $member = null) { |
||
| 964 | |||
| 965 | /** |
||
| 966 | * Checks if a group is allowed to the project and the permission code |
||
| 967 | * |
||
| 968 | * @param string $permissionCode |
||
| 969 | * @param Group $group |
||
| 970 | * |
||
| 971 | * @return bool |
||
| 972 | */ |
||
| 973 | public function groupAllowed($permissionCode, Group $group) { |
||
| 984 | |||
| 985 | /** |
||
| 986 | * Check if member has a permission code in this project. |
||
| 987 | * |
||
| 988 | * @param array|string $codes |
||
| 989 | * @param Member|null $member |
||
| 990 | * |
||
| 991 | * @return bool |
||
| 992 | */ |
||
| 993 | public function allowedAny($codes, $member = null) { |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Checks if the environment has been fully built. |
||
| 1008 | * |
||
| 1009 | * @return bool |
||
| 1010 | */ |
||
| 1011 | public function isProjectReady() { |
||
| 1034 | |||
| 1035 | /** |
||
| 1036 | * Returns a list of environments still being created. |
||
| 1037 | * |
||
| 1038 | * @return SS_List |
||
| 1039 | */ |
||
| 1040 | public function getRunningEnvironmentCreations() { |
||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * Returns a list of initial environments created for this project. |
||
| 1047 | * |
||
| 1048 | * @return DataList |
||
| 1049 | */ |
||
| 1050 | public function getInitialEnvironmentCreations() { |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * Only returns initial environments that are being created. |
||
| 1056 | * |
||
| 1057 | * @return DataList |
||
| 1058 | */ |
||
| 1059 | public function getRunningInitialEnvironmentCreations() { |
||
| 1063 | |||
| 1064 | /** |
||
| 1065 | * Returns a list of completed initial environment creations. This includes failed tasks. |
||
| 1066 | * |
||
| 1067 | * @return DataList |
||
| 1068 | */ |
||
| 1069 | public function getCompleteInitialEnvironmentCreations() { |
||
| 1073 | |||
| 1074 | /** |
||
| 1075 | * @param Member $member |
||
| 1076 | * |
||
| 1077 | * @return bool |
||
| 1078 | */ |
||
| 1079 | public function canCreate($member = null) { |
||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * This is a proxy call to gitonmy that caches the information per project and sha |
||
| 1097 | * |
||
| 1098 | * @param string $sha |
||
| 1099 | * @return false|\Gitonomy\Git\Commit |
||
| 1100 | */ |
||
| 1101 | public function getCommit($sha) { |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * @param \Gitonomy\Git\Commit $commit |
||
| 1118 | * @return string |
||
| 1119 | */ |
||
| 1120 | public function getCommitMessage(\Gitonomy\Git\Commit $commit) { |
||
| 1129 | |||
| 1130 | /** |
||
| 1131 | * @param \Gitonomy\Git\Commit $commit |
||
| 1132 | * @return mixed |
||
| 1133 | */ |
||
| 1134 | public function getCommitTags(\Gitonomy\Git\Commit $commit) { |
||
| 1146 | |||
| 1147 | /** |
||
| 1148 | * Setup a gridfield for the environment configs |
||
| 1149 | * |
||
| 1150 | * @param FieldList $fields |
||
| 1151 | * @param GridField $environments |
||
| 1152 | */ |
||
| 1153 | protected function setEnvironmentFields(&$fields, $environments) { |
||
| 1171 | |||
| 1172 | /** |
||
| 1173 | * @return string |
||
| 1174 | */ |
||
| 1175 | protected function getProjectFolderPath() { |
||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * @return ValidationResult |
||
| 1181 | */ |
||
| 1182 | protected function validate() { |
||
| 1208 | |||
| 1209 | } |
||
| 1210 | |||
| 1211 |
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.