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 | "IsVirtual" => "Boolean", |
||
| 24 | "CVSPath" => "Varchar(255)", |
||
| 25 | "DiskQuotaMB" => "Int", |
||
| 26 | "AllowedEnvironmentType" => "Varchar(255)" |
||
| 27 | ]; |
||
| 28 | |||
| 29 | private static $defaults = [ |
||
| 30 | "IsVirtual" => false |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | private static $has_many = [ |
||
| 37 | "Environments" => "DNEnvironment", |
||
| 38 | "CreateEnvironments" => "DNCreateEnvironment", |
||
| 39 | "Fetches" => "DNGitFetch" |
||
| 40 | ]; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private static $many_many = [ |
||
| 46 | "Viewers" => "Group", |
||
| 47 | "StarredBy" => "Member" |
||
| 48 | ]; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private static $summary_fields = [ |
||
| 54 | "Name", |
||
| 55 | "ViewersList", |
||
| 56 | ]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | private static $searchable_fields = [ |
||
| 62 | "Name", |
||
| 63 | ]; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private static $singular_name = 'Project'; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | private static $plural_name = 'Projects'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | private static $default_sort = 'Name'; |
||
| 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 = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var bool|Member |
||
| 90 | */ |
||
| 91 | protected static $_current_member_cache = null; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Display the repository URL on the project page. |
||
| 95 | * |
||
| 96 | * @var bool |
||
| 97 | */ |
||
| 98 | private static $show_repository_url = false; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * In-memory cache to determine whether clone repo was called. |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | private static $has_cloned_cache = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Whitelist configuration that describes how to convert a repository URL into a link |
||
| 108 | * to a web user interface for that URL |
||
| 109 | * |
||
| 110 | * Consists of a hash of "full.lower.case.domain" => {configuration} key/value pairs |
||
| 111 | * |
||
| 112 | * {configuration} can either be boolean true to auto-detect both the host and the |
||
| 113 | * name of the UI provider, or a nested array that overrides either one or both |
||
| 114 | * of the auto-detected values |
||
| 115 | * |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | private static $repository_interfaces = [ |
||
| 119 | 'github.com' => [ |
||
| 120 | 'icon' => 'deploynaut/img/github.png', |
||
| 121 | 'name' => 'Github.com', |
||
| 122 | ], |
||
| 123 | 'bitbucket.org' => [ |
||
| 124 | 'commit' => 'commits', |
||
| 125 | 'name' => 'Bitbucket.org', |
||
| 126 | ], |
||
| 127 | 'repo.or.cz' => [ |
||
| 128 | 'scheme' => 'http', |
||
| 129 | 'name' => 'repo.or.cz', |
||
| 130 | 'regex' => ['^(.*)$' => '/w$1'], |
||
| 131 | ], |
||
| 132 | |||
| 133 | /* Example for adding your own gitlab repository and override all auto-detected values (with their defaults) |
||
| 134 | 'gitlab.mysite.com' => array( |
||
| 135 | 'icon' => 'deploynaut/img/git.png', |
||
| 136 | 'host' => 'gitlab.mysite.com', |
||
| 137 | 'name' => 'Gitlab', |
||
| 138 | 'regex' => array('.git$' => ''), |
||
| 139 | 'commit' => "commit" |
||
| 140 | ), |
||
| 141 | */ |
||
| 142 | ]; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Used by the sync task |
||
| 146 | * |
||
| 147 | * @param string $path |
||
| 148 | * @return \DNProject |
||
| 149 | */ |
||
| 150 | public static function create_from_path($path) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * This will clear the cache for the git getters and should be called when the local git repo is updated |
||
| 165 | */ |
||
| 166 | public function clearGitCache() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @return \Zend_Cache_Frontend_Output |
||
| 175 | */ |
||
| 176 | public static function get_git_cache() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Return the used quota in MB. |
||
| 185 | * |
||
| 186 | * @param int $round Number of decimal places to round to |
||
| 187 | * @return double The used quota size in MB |
||
| 188 | */ |
||
| 189 | public function getUsedQuotaMB($round = 2) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Getter for DiskQuotaMB field to provide a default for existing |
||
| 204 | * records that have no quota field set, as it will need to default |
||
| 205 | * to a globally set size. |
||
| 206 | * |
||
| 207 | * @return string|int The quota size in MB |
||
| 208 | */ |
||
| 209 | public function getDiskQuotaMB() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function getPrimaryEnvType() { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Has the disk quota been exceeded? |
||
| 231 | * |
||
| 232 | * @return boolean |
||
| 233 | */ |
||
| 234 | public function HasExceededDiskQuota() { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Is there a disk quota set for this project? |
||
| 240 | * |
||
| 241 | * @return boolean |
||
| 242 | */ |
||
| 243 | public function HasDiskQuota() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Returns the current disk quota usage as a percentage |
||
| 249 | * |
||
| 250 | * @return int |
||
| 251 | */ |
||
| 252 | public function DiskQuotaUsagePercent() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get the menu to be shown on projects |
||
| 262 | * |
||
| 263 | * @return ArrayList |
||
| 264 | */ |
||
| 265 | public function Menu() { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Is this project currently at the root level of the controller that handles it? |
||
| 287 | * |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | public function isCurrent() { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Return the current object from $this->Menu() |
||
| 296 | * Good for making titles and things |
||
| 297 | * |
||
| 298 | * @return DataObject |
||
| 299 | */ |
||
| 300 | public function CurrentMenu() { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Is this project currently in a controller that is handling it or performing a sub-task? |
||
| 306 | * |
||
| 307 | * @return bool |
||
| 308 | */ |
||
| 309 | public function isSection() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Restrict access to viewing this project |
||
| 317 | * |
||
| 318 | * @param Member|null $member |
||
| 319 | * @return boolean |
||
| 320 | */ |
||
| 321 | public function canView($member = null) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param Member|null $member |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | View Code Duplication | public function canRestore($member = null) { |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @param Member|null $member |
||
| 357 | * @return bool |
||
| 358 | */ |
||
| 359 | View Code Duplication | public function canBackup($member = null) { |
|
| 375 | |||
| 376 | /** |
||
| 377 | * @param Member|null $member |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | View Code Duplication | public function canUploadArchive($member = null) { |
|
| 396 | |||
| 397 | /** |
||
| 398 | * @param Member|null $member |
||
| 399 | * @return bool |
||
| 400 | */ |
||
| 401 | View Code Duplication | public function canDownloadArchive($member = null) { |
|
| 417 | |||
| 418 | /** |
||
| 419 | * This is a permission check for the front-end only. |
||
| 420 | * |
||
| 421 | * Only admins can create environments for now. Also, we need to check the value |
||
| 422 | * of AllowedEnvironmentType which dictates which backend to use to render the form. |
||
| 423 | * |
||
| 424 | * @param Member|null $member |
||
| 425 | * |
||
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | public function canCreateEnvironments($member = null) { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @return DataList |
||
| 441 | */ |
||
| 442 | public function DataArchives() { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Return all archives which are "manual upload requests", |
||
| 449 | * meaning they don't have a file attached to them (yet). |
||
| 450 | * |
||
| 451 | * @return DataList |
||
| 452 | */ |
||
| 453 | public function PendingManualUploadDataArchives() { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Build an environment variable array to be used with this project. |
||
| 459 | * |
||
| 460 | * This is relevant if every project needs to use an individual SSH pubkey. |
||
| 461 | * |
||
| 462 | * Include this with all Gitonomy\Git\Repository, and |
||
| 463 | * \Symfony\Component\Process\Processes. |
||
| 464 | * |
||
| 465 | * @return array |
||
| 466 | */ |
||
| 467 | public function getProcessEnv() { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Get a string of people allowed to view this project |
||
| 484 | * |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | public function getViewersList() { |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @return DNData |
||
| 493 | */ |
||
| 494 | public function DNData() { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Provides a DNBuildList of builds found in this project. |
||
| 500 | * |
||
| 501 | * @return DNReferenceList |
||
| 502 | */ |
||
| 503 | public function DNBuildList() { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Provides a list of the branches in this project. |
||
| 509 | * |
||
| 510 | * @return DNBranchList |
||
| 511 | */ |
||
| 512 | public function DNBranchList() { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Provides a list of the tags in this project. |
||
| 521 | * |
||
| 522 | * @return DNReferenceList |
||
| 523 | */ |
||
| 524 | public function DNTagList() { |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @return false|Gitonomy\Git\Repository |
||
| 533 | */ |
||
| 534 | public function getRepository() { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Resolve a git reference like a branch or tag into a SHA. |
||
| 544 | * @return bool|string |
||
| 545 | */ |
||
| 546 | public function resolveRevision($value) { |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Provides a list of environments found in this project. |
||
| 562 | * CAUTION: filterByCallback will change this into an ArrayList! |
||
| 563 | * |
||
| 564 | * @return ArrayList |
||
| 565 | */ |
||
| 566 | public function DNEnvironmentList() { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @param string $usage |
||
| 585 | * @return ArrayList |
||
| 586 | */ |
||
| 587 | public function EnvironmentsByUsage($usage) { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Returns a map of envrionment name to build name |
||
| 593 | * |
||
| 594 | * @return false|DNDeployment |
||
| 595 | */ |
||
| 596 | public function currentBuilds() { |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @param string |
||
| 609 | * @return string |
||
| 610 | */ |
||
| 611 | public function Link($action = '') { |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @return string |
||
| 617 | */ |
||
| 618 | public function getCMSEditLink() { |
||
| 621 | /** |
||
| 622 | * @return string|null |
||
| 623 | */ |
||
| 624 | public function CreateEnvironmentLink() { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * @return string |
||
| 633 | */ |
||
| 634 | public function ToggleStarLink() { |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @return bool |
||
| 640 | */ |
||
| 641 | public function IsStarred() { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * @param string $action |
||
| 655 | * @return string |
||
| 656 | */ |
||
| 657 | public function APILink($action) { |
||
| 660 | |||
| 661 | /** |
||
| 662 | * @return FieldList |
||
| 663 | */ |
||
| 664 | public function getCMSFields() { |
||
| 722 | |||
| 723 | /** |
||
| 724 | * If there isn't a capistrano env project folder, show options to create one |
||
| 725 | * |
||
| 726 | * @param FieldList $fields |
||
| 727 | */ |
||
| 728 | public function setCreateProjectFolderField(&$fields) { |
||
| 745 | |||
| 746 | /** |
||
| 747 | * @return boolean |
||
| 748 | */ |
||
| 749 | public function projectFolderExists() { |
||
| 752 | |||
| 753 | /** |
||
| 754 | * @return bool |
||
| 755 | */ |
||
| 756 | public function repoExists() { |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Setup a job to clone a git repository. |
||
| 762 | * @return string resque token |
||
| 763 | */ |
||
| 764 | public function cloneRepo() { |
||
| 779 | |||
| 780 | /** |
||
| 781 | * @return string |
||
| 782 | */ |
||
| 783 | public function getLocalCVSPath() { |
||
| 786 | |||
| 787 | public function onBeforeWrite() { |
||
| 794 | |||
| 795 | public function onAfterWrite() { |
||
| 807 | |||
| 808 | /** |
||
| 809 | * Delete related environments and folders |
||
| 810 | */ |
||
| 811 | public function onAfterDelete() { |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Fetch the public key for this project. |
||
| 846 | * |
||
| 847 | * @return string|void |
||
| 848 | */ |
||
| 849 | public function getPublicKey() { |
||
| 856 | |||
| 857 | /** |
||
| 858 | * This returns that path of the public key if a key directory is set. It doesn't check whether the file exists. |
||
| 859 | * |
||
| 860 | * @return string|null |
||
| 861 | */ |
||
| 862 | public function getPublicKeyPath() { |
||
| 868 | |||
| 869 | /** |
||
| 870 | * This returns that path of the private key if a key directory is set. It doesn't check whether the file exists. |
||
| 871 | * |
||
| 872 | * @return string|null |
||
| 873 | */ |
||
| 874 | public function getPrivateKeyPath() { |
||
| 883 | |||
| 884 | /** |
||
| 885 | * Returns the location of the projects key dir if one exists. |
||
| 886 | * |
||
| 887 | * @return string|null |
||
| 888 | */ |
||
| 889 | public function getKeyDir() { |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Provide current repository URL to the users. |
||
| 903 | * |
||
| 904 | * @return void|string |
||
| 905 | */ |
||
| 906 | public function getRepositoryURL() { |
||
| 912 | |||
| 913 | /** |
||
| 914 | * Get a ViewableData structure describing the UI tool that lets the user view the repository code |
||
| 915 | * |
||
| 916 | * @return ArrayData |
||
| 917 | */ |
||
| 918 | public function getRepositoryInterface() { |
||
| 958 | |||
| 959 | /** |
||
| 960 | * Convenience wrapper for a single permission code. |
||
| 961 | * |
||
| 962 | * @param string $code |
||
| 963 | * @return SS_List |
||
| 964 | */ |
||
| 965 | public function whoIsAllowed($code) { |
||
| 968 | |||
| 969 | /** |
||
| 970 | * List members who have $codes on this project. |
||
| 971 | * Does not support Permission::DENY_PERMISSION malarky, same as Permission::get_groups_by_permission anyway... |
||
| 972 | * |
||
| 973 | * @param array|string $codes |
||
| 974 | * @return SS_List |
||
| 975 | */ |
||
| 976 | public function whoIsAllowedAny($codes) { |
||
| 995 | |||
| 996 | /** |
||
| 997 | * Convenience wrapper for a single permission code. |
||
| 998 | * |
||
| 999 | * @param string $code |
||
| 1000 | * @param Member|null $member |
||
| 1001 | * |
||
| 1002 | * @return bool |
||
| 1003 | */ |
||
| 1004 | public function allowed($code, $member = null) { |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Checks if a group is allowed to the project and the permission code |
||
| 1010 | * |
||
| 1011 | * @param string $permissionCode |
||
| 1012 | * @param Group $group |
||
| 1013 | * |
||
| 1014 | * @return bool |
||
| 1015 | */ |
||
| 1016 | public function groupAllowed($permissionCode, Group $group) { |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * Check if member has a permission code in this project. |
||
| 1030 | * |
||
| 1031 | * @param array|string $codes |
||
| 1032 | * @param Member|null $member |
||
| 1033 | * |
||
| 1034 | * @return bool |
||
| 1035 | */ |
||
| 1036 | public function allowedAny($codes, $member = null) { |
||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * Checks if the environment has been fully built. |
||
| 1051 | * |
||
| 1052 | * @return bool |
||
| 1053 | */ |
||
| 1054 | public function isProjectReady() { |
||
| 1077 | |||
| 1078 | /** |
||
| 1079 | * Returns a list of environments still being created. |
||
| 1080 | * |
||
| 1081 | * @return SS_List |
||
| 1082 | */ |
||
| 1083 | public function getRunningEnvironmentCreations() { |
||
| 1087 | |||
| 1088 | /** |
||
| 1089 | * Returns a list of initial environments created for this project. |
||
| 1090 | * |
||
| 1091 | * @return DataList |
||
| 1092 | */ |
||
| 1093 | public function getInitialEnvironmentCreations() { |
||
| 1096 | |||
| 1097 | /** |
||
| 1098 | * Only returns initial environments that are being created. |
||
| 1099 | * |
||
| 1100 | * @return DataList |
||
| 1101 | */ |
||
| 1102 | public function getRunningInitialEnvironmentCreations() { |
||
| 1106 | |||
| 1107 | /** |
||
| 1108 | * Returns a list of completed initial environment creations. This includes failed tasks. |
||
| 1109 | * |
||
| 1110 | * @return DataList |
||
| 1111 | */ |
||
| 1112 | public function getCompleteInitialEnvironmentCreations() { |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * @param Member $member |
||
| 1119 | * |
||
| 1120 | * @return bool |
||
| 1121 | */ |
||
| 1122 | public function canCreate($member = null) { |
||
| 1137 | |||
| 1138 | /** |
||
| 1139 | * This is a proxy call to gitonmy that caches the information per project and sha |
||
| 1140 | * |
||
| 1141 | * @param string $sha |
||
| 1142 | * @return false|\Gitonomy\Git\Commit |
||
| 1143 | */ |
||
| 1144 | public function getCommit($sha) { |
||
| 1162 | |||
| 1163 | /** |
||
| 1164 | * @param \Gitonomy\Git\Commit $commit |
||
| 1165 | * @return string |
||
| 1166 | */ |
||
| 1167 | View Code Duplication | public function getCommitMessage(\Gitonomy\Git\Commit $commit) { |
|
| 1176 | |||
| 1177 | /** |
||
| 1178 | * get the commit "subject", getCommitMessage get the full message |
||
| 1179 | * |
||
| 1180 | * @param \Gitonomy\Git\Commit $commit |
||
| 1181 | * @return string |
||
| 1182 | */ |
||
| 1183 | View Code Duplication | public function getCommitSubjectMessage(\Gitonomy\Git\Commit $commit) { |
|
| 1192 | |||
| 1193 | /** |
||
| 1194 | * @param \Gitonomy\Git\Commit $commit |
||
| 1195 | * @return mixed |
||
| 1196 | */ |
||
| 1197 | public function getCommitTags(\Gitonomy\Git\Commit $commit) { |
||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Setup a gridfield for the environment configs |
||
| 1212 | * |
||
| 1213 | * @param FieldList $fields |
||
| 1214 | * @param GridField $environments |
||
| 1215 | */ |
||
| 1216 | protected function setEnvironmentFields(&$fields, $environments) { |
||
| 1234 | |||
| 1235 | /** |
||
| 1236 | * @return string |
||
| 1237 | */ |
||
| 1238 | protected function getProjectFolderPath() { |
||
| 1241 | |||
| 1242 | /** |
||
| 1243 | * @return ValidationResult |
||
| 1244 | */ |
||
| 1245 | protected function validate() { |
||
| 1271 | |||
| 1272 | } |
||
| 1273 | |||
| 1274 |
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.