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 = array( |
||
| 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 = array( |
||
| 31 | "Environments" => "DNEnvironment", |
||
| 32 | "CreateEnvironments" => "DNCreateEnvironment" |
||
| 33 | ); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | public static $many_many = array( |
||
| 39 | "Viewers" => "Group", |
||
| 40 | 'StarredBy' => "Member" |
||
| 41 | ); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | public static $summary_fields = array( |
||
| 47 | "Name", |
||
| 48 | "ViewersList", |
||
| 49 | ); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | public static $searchable_fields = array( |
||
| 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 | * Display the repository URL on the project page. |
||
| 75 | * |
||
| 76 | * @var bool |
||
| 77 | */ |
||
| 78 | private static $show_repository_url = false; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * In-memory cache for currentBuilds per environment since fetching them from |
||
| 82 | * disk is pretty resource hungry. |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected static $relation_cache = array(); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * In-memory cache to determine whether clone repo was called. |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | private static $has_cloned_cache = array(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var bool|Member |
||
| 96 | */ |
||
| 97 | protected static $_current_member_cache = null; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Used by the sync task |
||
| 101 | * |
||
| 102 | * @param string $path |
||
| 103 | * @return \DNProject |
||
| 104 | */ |
||
| 105 | public static function create_from_path($path) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * This will clear the cache for the git getters and should be called when the local git repo is updated |
||
| 120 | */ |
||
| 121 | public function clearGitCache() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return \Zend_Cache_Frontend_Output |
||
| 130 | */ |
||
| 131 | public static function get_git_cache() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Return the used quota in MB. |
||
| 140 | * |
||
| 141 | * @param int $round Number of decimal places to round to |
||
| 142 | * @return double The used quota size in MB |
||
| 143 | */ |
||
| 144 | public function getUsedQuotaMB($round = 2) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Getter for DiskQuotaMB field to provide a default for existing |
||
| 159 | * records that have no quota field set, as it will need to default |
||
| 160 | * to a globally set size. |
||
| 161 | * |
||
| 162 | * @return string|int The quota size in MB |
||
| 163 | */ |
||
| 164 | public function getDiskQuotaMB() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Has the disk quota been exceeded? |
||
| 177 | * |
||
| 178 | * @return boolean |
||
| 179 | */ |
||
| 180 | public function HasExceededDiskQuota() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Is there a disk quota set for this project? |
||
| 186 | * |
||
| 187 | * @return boolean |
||
| 188 | */ |
||
| 189 | public function HasDiskQuota() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Returns the current disk quota usage as a percentage |
||
| 195 | * |
||
| 196 | * @return int |
||
| 197 | */ |
||
| 198 | public function DiskQuotaUsagePercent() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get the menu to be shown on projects |
||
| 208 | * |
||
| 209 | * @return ArrayList |
||
| 210 | */ |
||
| 211 | public function Menu() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Is this project currently at the root level of the controller that handles it? |
||
| 233 | * |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | public function isCurrent() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Return the current object from $this->Menu() |
||
| 242 | * Good for making titles and things |
||
| 243 | * |
||
| 244 | * @return DataObject |
||
| 245 | */ |
||
| 246 | public function CurrentMenu() { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Is this project currently in a controller that is handling it or performing a sub-task? |
||
| 252 | * |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | public function isSection() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Restrict access to viewing this project |
||
| 263 | * |
||
| 264 | * @param Member|null $member |
||
| 265 | * @return boolean |
||
| 266 | */ |
||
| 267 | public function canView($member = null) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param Member|null $member |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | View Code Duplication | public function canRestore($member = null) { |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @param Member|null $member |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | View Code Duplication | public function canBackup($member = null) { |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @param Member|null $member |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | View Code Duplication | public function canUploadArchive($member = null) { |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @param Member|null $member |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | View Code Duplication | public function canDownloadArchive($member = null) { |
|
| 359 | |||
| 360 | /** |
||
| 361 | * This is a permission check for the front-end only. |
||
| 362 | * |
||
| 363 | * Only admins can create environments for now. Also, we need to check the value |
||
| 364 | * of AllowedEnvironmentType which dictates which backend to use to render the form. |
||
| 365 | * |
||
| 366 | * @param Member|null $member |
||
| 367 | * |
||
| 368 | * @return bool |
||
| 369 | */ |
||
| 370 | public function canCreateEnvironments($member = null) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @return DataList |
||
| 383 | */ |
||
| 384 | public function DataArchives() { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Return all archives which are "manual upload requests", |
||
| 391 | * meaning they don't have a file attached to them (yet). |
||
| 392 | * |
||
| 393 | * @return DataList |
||
| 394 | */ |
||
| 395 | public function PendingManualUploadDataArchives() { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Build an environment variable array to be used with this project. |
||
| 401 | * |
||
| 402 | * This is relevant if every project needs to use an individual SSH pubkey. |
||
| 403 | * |
||
| 404 | * Include this with all Gitonomy\Git\Repository, and |
||
| 405 | * \Symfony\Component\Process\Processes. |
||
| 406 | * |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | public function getProcessEnv() { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Get a string of people allowed to view this project |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function getViewersList() { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @return DNData |
||
| 435 | */ |
||
| 436 | public function DNData() { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Provides a DNBuildList of builds found in this project. |
||
| 442 | * |
||
| 443 | * @return DNReferenceList |
||
| 444 | */ |
||
| 445 | public function DNBuildList() { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Provides a list of the branches in this project. |
||
| 451 | * |
||
| 452 | * @return DNBranchList |
||
| 453 | */ |
||
| 454 | public function DNBranchList() { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Provides a list of the tags in this project. |
||
| 463 | * |
||
| 464 | * @return DNReferenceList |
||
| 465 | */ |
||
| 466 | public function DNTagList() { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @return false|Gitonomy\Git\Repository |
||
| 475 | */ |
||
| 476 | public function getRepository() { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Provides a list of environments found in this project. |
||
| 486 | * CAUTION: filterByCallback will change this into an ArrayList! |
||
| 487 | * |
||
| 488 | * @return ArrayList |
||
| 489 | */ |
||
| 490 | public function DNEnvironmentList() { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param string $usage |
||
| 509 | * @return ArrayList |
||
| 510 | */ |
||
| 511 | public function EnvironmentsByUsage($usage) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Returns a map of envrionment name to build name |
||
| 517 | * |
||
| 518 | * @return false|DNDeployment |
||
| 519 | */ |
||
| 520 | public function currentBuilds() { |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @param string |
||
| 533 | * @return string |
||
| 534 | */ |
||
| 535 | public function Link($action = '') { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @return string|null |
||
| 541 | */ |
||
| 542 | public function CreateEnvironmentLink() { |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @return string |
||
| 551 | */ |
||
| 552 | public function ToggleStarLink() { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @return bool |
||
| 558 | */ |
||
| 559 | public function IsStarred() { |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @param string $action |
||
| 573 | * @return string |
||
| 574 | */ |
||
| 575 | public function APILink($action) { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @return FieldList |
||
| 581 | */ |
||
| 582 | public function getCMSFields() { |
||
| 636 | |||
| 637 | /** |
||
| 638 | * If there isn't a capistrano env project folder, show options to create one |
||
| 639 | * |
||
| 640 | * @param FieldList $fields |
||
| 641 | */ |
||
| 642 | public function setCreateProjectFolderField(&$fields) { |
||
| 659 | |||
| 660 | /** |
||
| 661 | * @return boolean |
||
| 662 | */ |
||
| 663 | public function projectFolderExists() { |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @return bool |
||
| 669 | */ |
||
| 670 | public function repoExists() { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Setup a job to clone a git repository. |
||
| 676 | * @return string resque token |
||
| 677 | */ |
||
| 678 | public function cloneRepo() { |
||
| 693 | |||
| 694 | /** |
||
| 695 | * @return string |
||
| 696 | */ |
||
| 697 | public function getLocalCVSPath() { |
||
| 700 | |||
| 701 | public function onBeforeWrite() { |
||
| 708 | |||
| 709 | public function onAfterWrite() { |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Delete related environments and folders |
||
| 724 | */ |
||
| 725 | public function onAfterDelete() { |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Fetch the public key for this project. |
||
| 751 | * |
||
| 752 | * @return string|void |
||
| 753 | */ |
||
| 754 | public function getPublicKey() { |
||
| 761 | |||
| 762 | /** |
||
| 763 | * This returns that path of the public key if a key directory is set. It doesn't check whether the file exists. |
||
| 764 | * |
||
| 765 | * @return string|null |
||
| 766 | */ |
||
| 767 | public function getPublicKeyPath() { |
||
| 773 | |||
| 774 | /** |
||
| 775 | * This returns that path of the private key if a key directory is set. It doesn't check whether the file exists. |
||
| 776 | * |
||
| 777 | * @return string|null |
||
| 778 | */ |
||
| 779 | public function getPrivateKeyPath() { |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Returns the location of the projects key dir if one exists. |
||
| 791 | * |
||
| 792 | * @return string|null |
||
| 793 | */ |
||
| 794 | public function getKeyDir() { |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Setup a gridfield for the environment configs |
||
| 808 | * |
||
| 809 | * @param FieldList $fields |
||
| 810 | * @param GridField $environments |
||
| 811 | */ |
||
| 812 | protected function setEnvironmentFields(&$fields, $environments) { |
||
| 830 | |||
| 831 | /** |
||
| 832 | * Provide current repository URL to the users. |
||
| 833 | * |
||
| 834 | * @return void|string |
||
| 835 | */ |
||
| 836 | public function getRepositoryURL() { |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Whitelist configuration that describes how to convert a repository URL into a link |
||
| 845 | * to a web user interface for that URL |
||
| 846 | * |
||
| 847 | * Consists of a hash of "full.lower.case.domain" => {configuration} key/value pairs |
||
| 848 | * |
||
| 849 | * {configuration} can either be boolean true to auto-detect both the host and the |
||
| 850 | * name of the UI provider, or a nested array that overrides either one or both |
||
| 851 | * of the auto-detected valyes |
||
| 852 | * |
||
| 853 | * @var array |
||
| 854 | */ |
||
| 855 | static private $repository_interfaces = array( |
||
| 856 | 'github.com' => array( |
||
| 857 | 'icon' => 'deploynaut/img/github.png', |
||
| 858 | 'name' => 'Github.com', |
||
| 859 | ), |
||
| 860 | 'bitbucket.org' => array( |
||
| 861 | 'commit' => 'commits', |
||
| 862 | 'name' => 'Bitbucket.org', |
||
| 863 | ), |
||
| 864 | 'repo.or.cz' => array( |
||
| 865 | 'scheme' => 'http', |
||
| 866 | 'name' => 'repo.or.cz', |
||
| 867 | 'regex' => array('^(.*)$' => '/w$1'), |
||
| 868 | ), |
||
| 869 | |||
| 870 | /* Example for adding your own gitlab repository and override all auto-detected values (with their defaults) |
||
| 871 | 'gitlab.mysite.com' => array( |
||
| 872 | 'icon' => 'deploynaut/img/git.png', |
||
| 873 | 'host' => 'gitlab.mysite.com', |
||
| 874 | 'name' => 'Gitlab', |
||
| 875 | 'regex' => array('.git$' => ''), |
||
| 876 | 'commit' => "commit" |
||
| 877 | ), |
||
| 878 | */ |
||
| 879 | ); |
||
| 880 | |||
| 881 | /** |
||
| 882 | * Get a ViewableData structure describing the UI tool that lets the user view the repository code |
||
| 883 | * |
||
| 884 | * @return ArrayData |
||
| 885 | */ |
||
| 886 | public function getRepositoryInterface() { |
||
| 926 | |||
| 927 | /** |
||
| 928 | * @return string |
||
| 929 | */ |
||
| 930 | protected function getProjectFolderPath() { |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Convenience wrapper for a single permission code. |
||
| 936 | * |
||
| 937 | * @param string $code |
||
| 938 | * @return SS_List |
||
| 939 | */ |
||
| 940 | public function whoIsAllowed($code) { |
||
| 943 | |||
| 944 | /** |
||
| 945 | * List members who have $codes on this project. |
||
| 946 | * Does not support Permission::DENY_PERMISSION malarky, same as Permission::get_groups_by_permission anyway... |
||
| 947 | * |
||
| 948 | * @param array|string $codes |
||
| 949 | * @return SS_List |
||
| 950 | */ |
||
| 951 | public function whoIsAllowedAny($codes) { |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Convenience wrapper for a single permission code. |
||
| 971 | * |
||
| 972 | * @param string $code |
||
| 973 | * @param Member|null $member |
||
| 974 | * |
||
| 975 | * @return bool |
||
| 976 | */ |
||
| 977 | public function allowed($code, $member = null) { |
||
| 980 | |||
| 981 | /** |
||
| 982 | * Checks if a group is allowed to the project and the permission code |
||
| 983 | * |
||
| 984 | * @param string $permissionCode |
||
| 985 | * @param Group $group |
||
| 986 | * |
||
| 987 | * @return bool |
||
| 988 | */ |
||
| 989 | public function groupAllowed($permissionCode, Group $group) { |
||
| 1000 | |||
| 1001 | /** |
||
| 1002 | * Check if member has a permission code in this project. |
||
| 1003 | * |
||
| 1004 | * @param array|string $codes |
||
| 1005 | * @param Member|null $member |
||
| 1006 | * |
||
| 1007 | * @return bool |
||
| 1008 | */ |
||
| 1009 | public function allowedAny($codes, $member = null) { |
||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * Checks if the environment has been fully built. |
||
| 1022 | * |
||
| 1023 | * @return bool |
||
| 1024 | */ |
||
| 1025 | public function isProjectReady() { |
||
| 1046 | |||
| 1047 | /** |
||
| 1048 | * Returns a list of environments still being created. |
||
| 1049 | * |
||
| 1050 | * @return SS_List |
||
| 1051 | */ |
||
| 1052 | public function getRunningEnvironmentCreations() { |
||
| 1056 | |||
| 1057 | /** |
||
| 1058 | * Returns a list of initial environments created for this project. |
||
| 1059 | * |
||
| 1060 | * @return DataList |
||
| 1061 | */ |
||
| 1062 | public function getInitialEnvironmentCreations() { |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * Only returns initial environments that are being created. |
||
| 1068 | * |
||
| 1069 | * @return DataList |
||
| 1070 | */ |
||
| 1071 | public function getRunningInitialEnvironmentCreations() { |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Returns a list of completed initial environment creations. This includes failed tasks. |
||
| 1078 | * |
||
| 1079 | * @return DataList |
||
| 1080 | */ |
||
| 1081 | public function getCompleteInitialEnvironmentCreations() { |
||
| 1085 | |||
| 1086 | /** |
||
| 1087 | * @return ValidationResult |
||
| 1088 | */ |
||
| 1089 | protected function validate() { |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * @param Member $member |
||
| 1118 | * |
||
| 1119 | * @return bool |
||
| 1120 | */ |
||
| 1121 | 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) { |
||
| 1153 | |||
| 1154 | /** |
||
| 1155 | * @param \Gitonomy\Git\Commit $commit |
||
| 1156 | * @return string |
||
| 1157 | */ |
||
| 1158 | public function getCommitMessage(\Gitonomy\Git\Commit $commit) { |
||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * @param \Gitonomy\Git\Commit $commit |
||
| 1170 | * @return mixed |
||
| 1171 | */ |
||
| 1172 | public function getCommitTags(\Gitonomy\Git\Commit $commit) { |
||
| 1184 | |||
| 1185 | } |
||
| 1186 | |||
| 1187 |
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.