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 DNEnvironment 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 DNEnvironment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class DNEnvironment extends DataObject { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * If this is set to a full pathfile, it will be used as template |
||
| 46 | * file when creating a new capistrano environment config file. |
||
| 47 | * |
||
| 48 | * If not set, the default 'environment.template' from the module |
||
| 49 | * root is used |
||
| 50 | * |
||
| 51 | * @config |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private static $template_file = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Set this to true to allow editing of the environment files via the web admin |
||
| 58 | * |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | private static $allow_web_editing = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private static $casting = array( |
||
| 67 | 'DeployHistory' => 'Text' |
||
| 68 | ); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Allowed backends. A map of Injector identifier to human-readable label. |
||
| 72 | * |
||
| 73 | * @config |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | private static $allowed_backends = array(); |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | public static $db = array( |
||
| 82 | "Filename" => "Varchar(255)", |
||
| 83 | "Name" => "Varchar(255)", |
||
| 84 | "URL" => "Varchar(255)", |
||
| 85 | "BackendIdentifier" => "Varchar(255)", // Injector identifier of the DeploymentBackend |
||
| 86 | "DryRunEnabled" => "Boolean", // True if the dry run button should be enabled on the frontend |
||
| 87 | "Usage" => "Enum('Production, UAT, Test, Unspecified', 'Unspecified')" |
||
| 88 | ); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | public static $has_one = array( |
||
| 94 | "Project" => "DNProject", |
||
| 95 | "CreateEnvironment" => "DNCreateEnvironment" |
||
| 96 | ); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | public static $has_many = array( |
||
| 102 | "Deployments" => "DNDeployment", |
||
| 103 | "DataArchives" => "DNDataArchive", |
||
| 104 | "Pipelines" => "Pipeline" // Only one Pipeline can be 'Running' at any one time. @see self::CurrentPipeline(). |
||
| 105 | ); |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | public static $many_many = array( |
||
| 111 | "Viewers" => "Member", // Who can view this environment |
||
| 112 | "ViewerGroups" => "Group", |
||
| 113 | "Deployers" => "Member", // Who can deploy to this environment |
||
| 114 | "DeployerGroups" => "Group", |
||
| 115 | "CanRestoreMembers" => "Member", // Who can restore archive files to this environment |
||
| 116 | "CanRestoreGroups" => "Group", |
||
| 117 | "CanBackupMembers" => "Member", // Who can backup archive files from this environment |
||
| 118 | "CanBackupGroups" => "Group", |
||
| 119 | "ArchiveUploaders" => "Member", // Who can upload archive files linked to this environment |
||
| 120 | "ArchiveUploaderGroups" => "Group", |
||
| 121 | "ArchiveDownloaders" => "Member", // Who can download archive files from this environment |
||
| 122 | "ArchiveDownloaderGroups" => "Group", |
||
| 123 | "ArchiveDeleters" => "Member", // Who can delete archive files from this environment, |
||
| 124 | "ArchiveDeleterGroups" => "Group", |
||
| 125 | "PipelineApprovers" => "Member", // Who can approve / reject pipelines from this environment |
||
| 126 | "PipelineApproverGroups" => "Group", |
||
| 127 | "PipelineCancellers" => "Member", // Who can abort pipelines |
||
| 128 | "PipelineCancellerGroups" => "Group" |
||
| 129 | ); |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var array |
||
| 133 | */ |
||
| 134 | public static $summary_fields = array( |
||
| 135 | "Name" => "Environment Name", |
||
| 136 | "Usage" => "Usage", |
||
| 137 | "URL" => "URL", |
||
| 138 | "DeployersList" => "Can Deploy List", |
||
| 139 | "CanRestoreMembersList" => "Can Restore List", |
||
| 140 | "CanBackupMembersList" => "Can Backup List", |
||
| 141 | "ArchiveUploadersList" => "Can Upload List", |
||
| 142 | "ArchiveDownloadersList" => "Can Download List", |
||
| 143 | "ArchiveDeletersList" => "Can Delete List", |
||
| 144 | "PipelineApproversList" => "Can Approve List", |
||
| 145 | "PipelineCancellersList" => "Can Cancel List" |
||
| 146 | ); |
||
| 147 | |||
| 148 | private static $singular_name = 'Capistrano Environment'; |
||
| 149 | |||
| 150 | private static $plural_name = 'Capistrano Environments'; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @var array |
||
| 154 | */ |
||
| 155 | public static $searchable_fields = array( |
||
| 156 | "Name", |
||
| 157 | ); |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var string |
||
| 161 | */ |
||
| 162 | private static $default_sort = 'Name'; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Used by the sync task |
||
| 166 | * |
||
| 167 | * @param string $path |
||
| 168 | * @return \DNEnvironment |
||
| 169 | */ |
||
| 170 | public static function create_from_path($path) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get the deployment backend used for this environment. |
||
| 183 | * |
||
| 184 | * Enforces compliance with the allowed_backends setting; if the DNEnvironment.BackendIdentifier value is |
||
| 185 | * illegal then that value is ignored. |
||
| 186 | * |
||
| 187 | * @return DeploymentBackend |
||
| 188 | */ |
||
| 189 | public function Backend() { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param SS_HTTPRequest $request |
||
| 215 | * |
||
| 216 | * @return DeploymentStrategy |
||
| 217 | */ |
||
| 218 | public function getDeployStrategy(\SS_HTTPRequest $request) { |
||
| 221 | |||
| 222 | public function Menu() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Return the current object from $this->Menu() |
||
| 242 | * Good for making titles and things |
||
| 243 | */ |
||
| 244 | public function CurrentMenu() { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Return a name for this environment. |
||
| 250 | * |
||
| 251 | * @param string $separator The string used when concatenating project with env name |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function getFullName($separator = ':') { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * URL for the environment that can be used if no explicit URL is set. |
||
| 260 | */ |
||
| 261 | public function getDefaultURL() { |
||
| 264 | |||
| 265 | public function getBareURL() { |
||
| 271 | |||
| 272 | public function getBareDefaultURL() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return boolean true if there is a pipeline for the current environment. |
||
| 281 | */ |
||
| 282 | public function HasPipelineSupport() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Returns a {@link Pipeline} object that is linked to this environment, but isn't saved into the database. This |
||
| 289 | * shouldn't be saved into the database unless you plan on starting an actual pipeline. |
||
| 290 | * |
||
| 291 | * @return Pipeline |
||
| 292 | */ |
||
| 293 | public function GenericPipeline() { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Returns the parsed config, based on a {@link Pipeline} being created for this {@link DNEnvironment}. |
||
| 301 | * |
||
| 302 | * @return ArrayData |
||
| 303 | */ |
||
| 304 | public function GenericPipelineConfig() { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Extract pipeline configuration data from the source yml file |
||
| 313 | * |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | public function loadPipelineConfig() { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Returns the {@link DNEnvironment} object relating to the pipeline config for this environment. The environment |
||
| 327 | * YAML file (e.g. project1-uat.yml; see docs/en/pipelines.md) contains two variable called `DependsOnProject` and |
||
| 328 | * `DependsOnEnvironment` - these are used together to find the {@link DNEnvironment} that this environment should |
||
| 329 | * rely on. |
||
| 330 | */ |
||
| 331 | public function DependsOnEnvironment() { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return bool true if there is a currently running Pipeline, and false if there isn't |
||
| 342 | */ |
||
| 343 | public function HasCurrentPipeline() { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * This can be used to determine if there is a currently running pipeline (there can only be one running per |
||
| 349 | * {@link DNEnvironment} at once), as well as getting the current pipeline to be shown in templates. |
||
| 350 | * |
||
| 351 | * @return DataObject|null The currently running pipeline, or null if there isn't any. |
||
| 352 | */ |
||
| 353 | public function CurrentPipeline() { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return bool true if the current user can cancel a running pipeline |
||
| 359 | */ |
||
| 360 | public function CanCancelPipeline() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Environments are only viewable by people that can view the environment. |
||
| 370 | * |
||
| 371 | * @param Member|null $member |
||
| 372 | * @return boolean |
||
| 373 | */ |
||
| 374 | public function canView($member = null) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Allow deploy only to some people. |
||
| 398 | * |
||
| 399 | * @param Member|null $member |
||
| 400 | * @return boolean |
||
| 401 | */ |
||
| 402 | View Code Duplication | public function canDeploy($member = null) { |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Provide reason why the user cannot deploy. |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function getCannotDeployMessage() { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Allows only selected {@link Member} objects to restore {@link DNDataArchive} objects into this |
||
| 432 | * {@link DNEnvironment}. |
||
| 433 | * |
||
| 434 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 435 | * @return boolean true if $member can restore, and false if they can't. |
||
| 436 | */ |
||
| 437 | View Code Duplication | public function canRestore($member = null) { |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Allows only selected {@link Member} objects to backup this {@link DNEnvironment} to a {@link DNDataArchive} |
||
| 458 | * file. |
||
| 459 | * |
||
| 460 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 461 | * @return boolean true if $member can backup, and false if they can't. |
||
| 462 | */ |
||
| 463 | View Code Duplication | public function canBackup($member = null) { |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Allows only selected {@link Member} objects to upload {@link DNDataArchive} objects linked to this |
||
| 489 | * {@link DNEnvironment}. |
||
| 490 | * |
||
| 491 | * Note: This is not uploading them to the actual environment itself (e.g. uploading to the live site) - it is the |
||
| 492 | * process of uploading a *.sspak file into Deploynaut for later 'restoring' to an environment. See |
||
| 493 | * {@link self::canRestore()}. |
||
| 494 | * |
||
| 495 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 496 | * @return boolean true if $member can upload archives linked to this environment, false if they can't. |
||
| 497 | */ |
||
| 498 | View Code Duplication | public function canUploadArchive($member = null) { |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Allows only selected {@link Member} objects to download {@link DNDataArchive} objects from this |
||
| 524 | * {@link DNEnvironment}. |
||
| 525 | * |
||
| 526 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 527 | * @return boolean true if $member can download archives from this environment, false if they can't. |
||
| 528 | */ |
||
| 529 | View Code Duplication | public function canDownloadArchive($member = null) { |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Determine if the specified user can abort any pipelines |
||
| 550 | * |
||
| 551 | * @param Member|null $member |
||
| 552 | * @return boolean |
||
| 553 | */ |
||
| 554 | View Code Duplication | public function canAbort($member = null) { |
|
| 569 | |||
| 570 | /** |
||
| 571 | * Determine if the specified user can approve any pipelines |
||
| 572 | * |
||
| 573 | * @param Member|null $member |
||
| 574 | * @return boolean |
||
| 575 | */ |
||
| 576 | View Code Duplication | public function canApprove($member = null) { |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Allows only selected {@link Member} objects to delete {@link DNDataArchive} objects from this |
||
| 593 | * {@link DNEnvironment}. |
||
| 594 | * |
||
| 595 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 596 | * @return boolean true if $member can delete archives from this environment, false if they can't. |
||
| 597 | */ |
||
| 598 | View Code Duplication | public function canDeleteArchive($member = null) { |
|
| 616 | /** |
||
| 617 | * Get a string of groups/people that are allowed to deploy to this environment. |
||
| 618 | * Used in DNRoot_project.ss to list {@link Member}s who have permission to perform this action. |
||
| 619 | * |
||
| 620 | * @return string |
||
| 621 | */ |
||
| 622 | public function getDeployersList() { |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Get a string of groups/people that are allowed to restore {@link DNDataArchive} objects into this environment. |
||
| 634 | * |
||
| 635 | * @return string |
||
| 636 | */ |
||
| 637 | public function getCanRestoreMembersList() { |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Get a string of groups/people that are allowed to backup {@link DNDataArchive} objects from this environment. |
||
| 649 | * |
||
| 650 | * @return string |
||
| 651 | */ |
||
| 652 | public function getCanBackupMembersList() { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Get a string of groups/people that are allowed to upload {@link DNDataArchive} |
||
| 664 | * objects linked to this environment. |
||
| 665 | * |
||
| 666 | * @return string |
||
| 667 | */ |
||
| 668 | public function getArchiveUploadersList() { |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Get a string of groups/people that are allowed to download {@link DNDataArchive} objects from this environment. |
||
| 680 | * |
||
| 681 | * @return string |
||
| 682 | */ |
||
| 683 | public function getArchiveDownloadersList() { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Get a string of groups/people that are allowed to delete {@link DNDataArchive} objects from this environment. |
||
| 695 | * |
||
| 696 | * @return string |
||
| 697 | */ |
||
| 698 | public function getArchiveDeletersList() { |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Get a string of groups/people that are allowed to approve pipelines |
||
| 710 | * |
||
| 711 | * @return string |
||
| 712 | */ |
||
| 713 | public function getPipelineApproversList() { |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Get a string of groups/people that are allowed to cancel pipelines |
||
| 725 | * |
||
| 726 | * @return string |
||
| 727 | */ |
||
| 728 | public function getPipelineCancellersList() { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * @return DNData |
||
| 740 | */ |
||
| 741 | public function DNData() { |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Get the current deployed build for this environment |
||
| 747 | * |
||
| 748 | * Dear people of the future: If you are looking to optimize this, simply create a CurrentBuildSHA(), which can be |
||
| 749 | * a lot faster. I presume you came here because of the Project display template, which only needs a SHA. |
||
| 750 | * |
||
| 751 | * @return false|DNDeployment |
||
| 752 | */ |
||
| 753 | public function CurrentBuild() { |
||
| 785 | |||
| 786 | /** |
||
| 787 | * A history of all builds deployed to this environment |
||
| 788 | * |
||
| 789 | * @return ArrayList |
||
| 790 | */ |
||
| 791 | public function DeployHistory() { |
||
| 796 | |||
| 797 | /** |
||
| 798 | * @param string $sha |
||
| 799 | * @return array |
||
| 800 | */ |
||
| 801 | protected function getCommitData($sha) { |
||
| 825 | |||
| 826 | /** |
||
| 827 | * @return string |
||
| 828 | */ |
||
| 829 | public function Link() { |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Is this environment currently at the root level of the controller that handles it? |
||
| 835 | * @return bool |
||
| 836 | */ |
||
| 837 | public function isCurrent() { |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Is this environment currently in a controller that is handling it or performing a sub-task? |
||
| 843 | * @return bool |
||
| 844 | */ |
||
| 845 | public function isSection() { |
||
| 850 | |||
| 851 | |||
| 852 | /** |
||
| 853 | * Build a set of multi-select fields for assigning permissions to a pair of group and member many_many relations |
||
| 854 | * |
||
| 855 | * @param string $groupField Group field name |
||
| 856 | * @param string $memberField Member field name |
||
| 857 | * @param array $groups List of groups |
||
| 858 | * @param array $members List of members |
||
| 859 | * @return FieldGroup |
||
| 860 | */ |
||
| 861 | protected function buildPermissionField($groupField, $memberField, $groups, $members) { |
||
| 876 | |||
| 877 | /** |
||
| 878 | * @return FieldList |
||
| 879 | */ |
||
| 880 | public function getCMSFields() { |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * @param FieldList $fields |
||
| 1058 | */ |
||
| 1059 | protected function setDeployConfigurationFields(&$fields) { |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * @param FieldList $fields |
||
| 1082 | */ |
||
| 1083 | protected function setPipelineConfigurationFields($fields) { |
||
| 1108 | |||
| 1109 | /** |
||
| 1110 | */ |
||
| 1111 | public function onBeforeWrite() { |
||
| 1120 | |||
| 1121 | public function onAfterWrite() { |
||
| 1136 | |||
| 1137 | |||
| 1138 | /** |
||
| 1139 | * Ensure that environment paths are setup on the local filesystem |
||
| 1140 | */ |
||
| 1141 | protected function checkEnvironmentPath() { |
||
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Write the deployment config file to filesystem |
||
| 1151 | */ |
||
| 1152 | protected function writeConfigFile() { |
||
| 1168 | |||
| 1169 | /** |
||
| 1170 | * Write the pipeline config file to filesystem |
||
| 1171 | */ |
||
| 1172 | protected function writePipelineFile() { |
||
| 1185 | |||
| 1186 | /** |
||
| 1187 | * Delete any related config files |
||
| 1188 | */ |
||
| 1189 | public function onAfterDelete() { |
||
| 1201 | |||
| 1202 | /** |
||
| 1203 | * @return string |
||
| 1204 | */ |
||
| 1205 | protected function getEnvironmentConfig() { |
||
| 1211 | |||
| 1212 | /** |
||
| 1213 | * @return boolean |
||
| 1214 | */ |
||
| 1215 | protected function envFileExists() { |
||
| 1221 | |||
| 1222 | /** |
||
| 1223 | * Returns the path to the ruby config file |
||
| 1224 | * |
||
| 1225 | * @return string |
||
| 1226 | */ |
||
| 1227 | public function getConfigFilename() { |
||
| 1236 | |||
| 1237 | /** |
||
| 1238 | * Returns the path to the {@link Pipeline} configuration for this environment. |
||
| 1239 | * Uses the same path and filename as the capistrano config, but with .yml extension. |
||
| 1240 | * |
||
| 1241 | * @return string |
||
| 1242 | */ |
||
| 1243 | public function getPipelineFilename() { |
||
| 1253 | |||
| 1254 | /** |
||
| 1255 | * Does this environment have a pipeline config file |
||
| 1256 | * |
||
| 1257 | * @return boolean |
||
| 1258 | */ |
||
| 1259 | protected function pipelineFileExists() { |
||
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Helper function to convert a multi-dimensional array (associative or indexed) to an {@link ArrayList} or |
||
| 1269 | * {@link ArrayData} object structure, so that values can be used in templates. |
||
| 1270 | * |
||
| 1271 | * @param array $array The (single- or multi-dimensional) array to convert |
||
| 1272 | * @return object Either an {@link ArrayList} or {@link ArrayData} object, or the original item ($array) if $array |
||
| 1273 | * isn't an array. |
||
| 1274 | */ |
||
| 1275 | public static function array_to_viewabledata($array) { |
||
| 1300 | |||
| 1301 | |||
| 1302 | |||
| 1303 | /** |
||
| 1304 | * Helper function to retrieve filtered commits from an environment |
||
| 1305 | * this environment depends on |
||
| 1306 | * |
||
| 1307 | * @return DataList |
||
| 1308 | */ |
||
| 1309 | public function getDependentFilteredCommits() { |
||
| 1334 | |||
| 1335 | /** |
||
| 1336 | * Enable the maintenance page |
||
| 1337 | * |
||
| 1338 | * @param DeploynautLogFile $log |
||
| 1339 | */ |
||
| 1340 | public function enableMaintenace($log) { |
||
| 1344 | |||
| 1345 | /** |
||
| 1346 | * Disable maintenance page |
||
| 1347 | * |
||
| 1348 | * @param DeploynautLogFile $log |
||
| 1349 | */ |
||
| 1350 | public function disableMaintenance($log) { |
||
| 1354 | |||
| 1355 | protected function validate() { |
||
| 1365 | |||
| 1366 | /** |
||
| 1367 | * Fetchs all deployments in progress. Limits to 1 hour to prevent deployments |
||
| 1368 | * if an old deployment is stuck. |
||
| 1369 | * |
||
| 1370 | * @return DataList |
||
| 1371 | */ |
||
| 1372 | public function runningDeployments() { |
||
| 1380 | |||
| 1381 | } |
||
| 1382 | |||
| 1383 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: