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 | /** |
||
| 46 | * If this is set to a full pathfile, it will be used as template |
||
| 47 | * file when creating a new capistrano environment config file. |
||
| 48 | * |
||
| 49 | * If not set, the default 'environment.template' from the module |
||
| 50 | * root is used |
||
| 51 | * |
||
| 52 | * @config |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private static $template_file = ''; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Set this to true to allow editing of the environment files via the web admin |
||
| 59 | * |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | private static $allow_web_editing = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private static $casting = array( |
||
| 68 | 'DeployHistory' => 'Text' |
||
| 69 | ); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Allowed backends. A map of Injector identifier to human-readable label. |
||
| 73 | * |
||
| 74 | * @config |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | private static $allowed_backends = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | public static $db = array( |
||
| 83 | "Filename" => "Varchar(255)", |
||
| 84 | "Name" => "Varchar(255)", |
||
| 85 | "URL" => "Varchar(255)", |
||
| 86 | "BackendIdentifier" => "Varchar(255)", // Injector identifier of the DeploymentBackend |
||
| 87 | "DryRunEnabled" => "Boolean", // True if the dry run button should be enabled on the frontend |
||
| 88 | "Usage" => "Enum('Production, UAT, Test, Unspecified', 'Unspecified')" |
||
| 89 | ); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | public static $has_one = array( |
||
| 95 | "Project" => "DNProject", |
||
| 96 | "CreateEnvironment" => "DNCreateEnvironment" |
||
| 97 | ); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | public static $has_many = array( |
||
| 103 | "Deployments" => "DNDeployment", |
||
| 104 | "DataArchives" => "DNDataArchive", |
||
| 105 | "Pipelines" => "Pipeline" // Only one Pipeline can be 'Running' at any one time. @see self::CurrentPipeline(). |
||
| 106 | ); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | public static $many_many = array( |
||
| 112 | "Viewers" => "Member", // Who can view this environment |
||
| 113 | "ViewerGroups" => "Group", |
||
| 114 | "Deployers" => "Member", // Who can deploy to this environment |
||
| 115 | "DeployerGroups" => "Group", |
||
| 116 | "CanRestoreMembers" => "Member", // Who can restore archive files to this environment |
||
| 117 | "CanRestoreGroups" => "Group", |
||
| 118 | "CanBackupMembers" => "Member", // Who can backup archive files from this environment |
||
| 119 | "CanBackupGroups" => "Group", |
||
| 120 | "ArchiveUploaders" => "Member", // Who can upload archive files linked to this environment |
||
| 121 | "ArchiveUploaderGroups" => "Group", |
||
| 122 | "ArchiveDownloaders" => "Member", // Who can download archive files from this environment |
||
| 123 | "ArchiveDownloaderGroups" => "Group", |
||
| 124 | "ArchiveDeleters" => "Member", // Who can delete archive files from this environment, |
||
| 125 | "ArchiveDeleterGroups" => "Group", |
||
| 126 | "PipelineApprovers" => "Member", // Who can approve / reject pipelines from this environment |
||
| 127 | "PipelineApproverGroups" => "Group", |
||
| 128 | "PipelineCancellers" => "Member", // Who can abort pipelines |
||
| 129 | "PipelineCancellerGroups" => "Group" |
||
| 130 | ); |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var array |
||
| 134 | */ |
||
| 135 | public static $summary_fields = array( |
||
| 136 | "Name" => "Environment Name", |
||
| 137 | "Usage" => "Usage", |
||
| 138 | "URL" => "URL", |
||
| 139 | "DeployersList" => "Can Deploy List", |
||
| 140 | "CanRestoreMembersList" => "Can Restore List", |
||
| 141 | "CanBackupMembersList" => "Can Backup List", |
||
| 142 | "ArchiveUploadersList" => "Can Upload List", |
||
| 143 | "ArchiveDownloadersList" => "Can Download List", |
||
| 144 | "ArchiveDeletersList" => "Can Delete List", |
||
| 145 | "PipelineApproversList" => "Can Approve List", |
||
| 146 | "PipelineCancellersList" => "Can Cancel List" |
||
| 147 | ); |
||
| 148 | |||
| 149 | private static $singular_name = 'Capistrano Environment'; |
||
| 150 | |||
| 151 | private static $plural_name = 'Capistrano Environments'; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var array |
||
| 155 | */ |
||
| 156 | public static $searchable_fields = array( |
||
| 157 | "Name", |
||
| 158 | ); |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var string |
||
| 162 | */ |
||
| 163 | private static $default_sort = 'Name'; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Used by the sync task |
||
| 167 | * |
||
| 168 | * @param string $path |
||
| 169 | * @return \DNEnvironment |
||
| 170 | */ |
||
| 171 | public static function create_from_path($path) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get the deployment backend used for this environment. |
||
| 185 | * |
||
| 186 | * Enforces compliance with the allowed_backends setting; if the DNEnvironment.BackendIdentifier value is |
||
| 187 | * illegal then that value is ignored. |
||
| 188 | * |
||
| 189 | * @return DeploymentBackend |
||
| 190 | */ |
||
| 191 | public function Backend() |
||
| 215 | |||
| 216 | public function Menu() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Return the current object from $this->Menu() |
||
| 237 | * Good for making titles and things |
||
| 238 | */ |
||
| 239 | public function CurrentMenu() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Return a name for this environment. |
||
| 246 | * |
||
| 247 | * @param string $separator The string used when concatenating project with env name |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public function getFullName($separator = ':') |
||
| 254 | |||
| 255 | public function getBareURL() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return boolean true if there is a pipeline for the current environment. |
||
| 265 | */ |
||
| 266 | public function HasPipelineSupport() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Returns a {@link Pipeline} object that is linked to this environment, but isn't saved into the database. This |
||
| 274 | * shouldn't be saved into the database unless you plan on starting an actual pipeline. |
||
| 275 | * |
||
| 276 | * @return Pipeline |
||
| 277 | */ |
||
| 278 | public function GenericPipeline() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns the parsed config, based on a {@link Pipeline} being created for this {@link DNEnvironment}. |
||
| 287 | * |
||
| 288 | * @return ArrayData |
||
| 289 | */ |
||
| 290 | public function GenericPipelineConfig() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Extract pipeline configuration data from the source yml file |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public function loadPipelineConfig() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Returns the {@link DNEnvironment} object relating to the pipeline config for this environment. The environment |
||
| 315 | * YAML file (e.g. project1-uat.yml; see docs/en/pipelines.md) contains two variable called `DependsOnProject` and |
||
| 316 | * `DependsOnEnvironment` - these are used together to find the {@link DNEnvironment} that this environment should |
||
| 317 | * rely on. |
||
| 318 | */ |
||
| 319 | public function DependsOnEnvironment() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return bool true if there is a currently running Pipeline, and false if there isn't |
||
| 331 | */ |
||
| 332 | public function HasCurrentPipeline() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * This can be used to determine if there is a currently running pipeline (there can only be one running per |
||
| 339 | * {@link DNEnvironment} at once), as well as getting the current pipeline to be shown in templates. |
||
| 340 | * |
||
| 341 | * @return DataObject|null The currently running pipeline, or null if there isn't any. |
||
| 342 | */ |
||
| 343 | public function CurrentPipeline() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return bool true if the current user can cancel a running pipeline |
||
| 350 | */ |
||
| 351 | public function CanCancelPipeline() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Environments are only viewable by people that can view the environment. |
||
| 362 | * |
||
| 363 | * @param Member|null $member |
||
| 364 | * @return boolean |
||
| 365 | */ |
||
| 366 | public function canView($member = null) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Allow deploy only to some people. |
||
| 391 | * |
||
| 392 | * @param Member|null $member |
||
| 393 | * @return boolean |
||
| 394 | */ |
||
| 395 | View Code Duplication | public function canDeploy($member = null) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Allows only selected {@link Member} objects to restore {@link DNDataArchive} objects into this |
||
| 421 | * {@link DNEnvironment}. |
||
| 422 | * |
||
| 423 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 424 | * @return boolean true if $member can restore, and false if they can't. |
||
| 425 | */ |
||
| 426 | View Code Duplication | public function canRestore($member = null) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Allows only selected {@link Member} objects to backup this {@link DNEnvironment} to a {@link DNDataArchive} |
||
| 452 | * file. |
||
| 453 | * |
||
| 454 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 455 | * @return boolean true if $member can backup, and false if they can't. |
||
| 456 | */ |
||
| 457 | View Code Duplication | public function canBackup($member = null) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Allows only selected {@link Member} objects to upload {@link DNDataArchive} objects linked to this |
||
| 488 | * {@link DNEnvironment}. |
||
| 489 | * |
||
| 490 | * Note: This is not uploading them to the actual environment itself (e.g. uploading to the live site) - it is the |
||
| 491 | * process of uploading a *.sspak file into Deploynaut for later 'restoring' to an environment. See |
||
| 492 | * {@link self::canRestore()}. |
||
| 493 | * |
||
| 494 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 495 | * @return boolean true if $member can upload archives linked to this environment, false if they can't. |
||
| 496 | */ |
||
| 497 | View Code Duplication | public function canUploadArchive($member = null) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Allows only selected {@link Member} objects to download {@link DNDataArchive} objects from this |
||
| 528 | * {@link DNEnvironment}. |
||
| 529 | * |
||
| 530 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 531 | * @return boolean true if $member can download archives from this environment, false if they can't. |
||
| 532 | */ |
||
| 533 | View Code Duplication | public function canDownloadArchive($member = null) |
|
| 556 | |||
| 557 | /** |
||
| 558 | * Determine if the specified user can abort any pipelines |
||
| 559 | * |
||
| 560 | * @param Member|null $member |
||
| 561 | * @return boolean |
||
| 562 | */ |
||
| 563 | View Code Duplication | public function canAbort($member = null) |
|
| 579 | |||
| 580 | /** |
||
| 581 | * Determine if the specified user can approve any pipelines |
||
| 582 | * |
||
| 583 | * @param Member|null $member |
||
| 584 | * @return boolean |
||
| 585 | */ |
||
| 586 | View Code Duplication | public function canApprove($member = null) |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Allows only selected {@link Member} objects to delete {@link DNDataArchive} objects from this |
||
| 604 | * {@link DNEnvironment}. |
||
| 605 | * |
||
| 606 | * @param Member|null $member The {@link Member} object to test against. If null, uses Member::currentMember(); |
||
| 607 | * @return boolean true if $member can delete archives from this environment, false if they can't. |
||
| 608 | */ |
||
| 609 | View Code Duplication | public function canDeleteArchive($member = null) |
|
| 632 | /** |
||
| 633 | * Get a string of groups/people that are allowed to deploy to this environment. |
||
| 634 | * Used in DNRoot_project.ss to list {@link Member}s who have permission to perform this action. |
||
| 635 | * |
||
| 636 | * @return string |
||
| 637 | */ |
||
| 638 | public function getDeployersList() |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Get a string of groups/people that are allowed to restore {@link DNDataArchive} objects into this environment. |
||
| 651 | * |
||
| 652 | * @return string |
||
| 653 | */ |
||
| 654 | public function getCanRestoreMembersList() |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Get a string of groups/people that are allowed to backup {@link DNDataArchive} objects from this environment. |
||
| 667 | * |
||
| 668 | * @return string |
||
| 669 | */ |
||
| 670 | public function getCanBackupMembersList() |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Get a string of groups/people that are allowed to upload {@link DNDataArchive} |
||
| 683 | * objects linked to this environment. |
||
| 684 | * |
||
| 685 | * @return string |
||
| 686 | */ |
||
| 687 | public function getArchiveUploadersList() |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Get a string of groups/people that are allowed to download {@link DNDataArchive} objects from this environment. |
||
| 700 | * |
||
| 701 | * @return string |
||
| 702 | */ |
||
| 703 | public function getArchiveDownloadersList() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Get a string of groups/people that are allowed to delete {@link DNDataArchive} objects from this environment. |
||
| 716 | * |
||
| 717 | * @return string |
||
| 718 | */ |
||
| 719 | public function getArchiveDeletersList() |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Get a string of groups/people that are allowed to approve pipelines |
||
| 732 | * |
||
| 733 | * @return string |
||
| 734 | */ |
||
| 735 | public function getPipelineApproversList() |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Get a string of groups/people that are allowed to cancel pipelines |
||
| 748 | * |
||
| 749 | * @return string |
||
| 750 | */ |
||
| 751 | public function getPipelineCancellersList() |
||
| 761 | |||
| 762 | /** |
||
| 763 | * @return DNData |
||
| 764 | */ |
||
| 765 | public function DNData() |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Get the current deployed build for this environment |
||
| 772 | * |
||
| 773 | * Dear people of the future: If you are looking to optimize this, simply create a CurrentBuildSHA(), which can be |
||
| 774 | * a lot faster. I presume you came here because of the Project display template, which only needs a SHA. |
||
| 775 | * |
||
| 776 | * @return false|DNDeployment |
||
| 777 | */ |
||
| 778 | public function CurrentBuild() |
||
| 812 | |||
| 813 | /** |
||
| 814 | * A history of all builds deployed to this environment |
||
| 815 | * |
||
| 816 | * @return ArrayList |
||
| 817 | */ |
||
| 818 | public function DeployHistory() |
||
| 824 | |||
| 825 | /** |
||
| 826 | * @param string $sha |
||
| 827 | * @return array |
||
| 828 | */ |
||
| 829 | protected function getCommitData($sha) |
||
| 854 | |||
| 855 | /** |
||
| 856 | * @return string |
||
| 857 | */ |
||
| 858 | public function Link() |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Is this environment currently at the root level of the controller that handles it? |
||
| 865 | * @return bool |
||
| 866 | */ |
||
| 867 | public function isCurrent() |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Is this environment currently in a controller that is handling it or performing a sub-task? |
||
| 874 | * @return bool |
||
| 875 | */ |
||
| 876 | public function isSection() |
||
| 882 | |||
| 883 | |||
| 884 | /** |
||
| 885 | * Build a set of multi-select fields for assigning permissions to a pair of group and member many_many relations |
||
| 886 | * |
||
| 887 | * @param string $groupField Group field name |
||
| 888 | * @param string $memberField Member field name |
||
| 889 | * @param array $groups List of groups |
||
| 890 | * @param array $members List of members |
||
| 891 | * @return FieldGroup |
||
| 892 | */ |
||
| 893 | protected function buildPermissionField($groupField, $memberField, $groups, $members) |
||
| 909 | |||
| 910 | /** |
||
| 911 | * @return FieldList |
||
| 912 | */ |
||
| 913 | public function getCMSFields() |
||
| 1089 | |||
| 1090 | /** |
||
| 1091 | * @param FieldList $fields |
||
| 1092 | */ |
||
| 1093 | protected function setDeployConfigurationFields(&$fields) |
||
| 1114 | |||
| 1115 | /** |
||
| 1116 | * @param FieldList $fields |
||
| 1117 | */ |
||
| 1118 | protected function setPipelineConfigurationFields($fields) |
||
| 1144 | |||
| 1145 | /** |
||
| 1146 | */ |
||
| 1147 | public function onBeforeWrite() |
||
| 1157 | |||
| 1158 | public function onAfterWrite() |
||
| 1174 | |||
| 1175 | |||
| 1176 | /** |
||
| 1177 | * Ensure that environment paths are setup on the local filesystem |
||
| 1178 | */ |
||
| 1179 | protected function checkEnvironmentPath() |
||
| 1187 | |||
| 1188 | /** |
||
| 1189 | * Write the deployment config file to filesystem |
||
| 1190 | */ |
||
| 1191 | protected function writeConfigFile() |
||
| 1208 | |||
| 1209 | /** |
||
| 1210 | * Write the pipeline config file to filesystem |
||
| 1211 | */ |
||
| 1212 | protected function writePipelineFile() |
||
| 1226 | |||
| 1227 | /** |
||
| 1228 | * Delete any related config files |
||
| 1229 | */ |
||
| 1230 | public function onAfterDelete() |
||
| 1243 | |||
| 1244 | /** |
||
| 1245 | * @return string |
||
| 1246 | */ |
||
| 1247 | protected function getEnvironmentConfig() |
||
| 1254 | |||
| 1255 | /** |
||
| 1256 | * @return boolean |
||
| 1257 | */ |
||
| 1258 | protected function envFileExists() |
||
| 1265 | |||
| 1266 | /** |
||
| 1267 | * Returns the path to the ruby config file |
||
| 1268 | * |
||
| 1269 | * @return string |
||
| 1270 | */ |
||
| 1271 | public function getConfigFilename() |
||
| 1281 | |||
| 1282 | /** |
||
| 1283 | * Returns the path to the {@link Pipeline} configuration for this environment. |
||
| 1284 | * Uses the same path and filename as the capistrano config, but with .yml extension. |
||
| 1285 | * |
||
| 1286 | * @return string |
||
| 1287 | */ |
||
| 1288 | public function getPipelineFilename() |
||
| 1299 | |||
| 1300 | /** |
||
| 1301 | * Does this environment have a pipeline config file |
||
| 1302 | * |
||
| 1303 | * @return boolean |
||
| 1304 | */ |
||
| 1305 | protected function pipelineFileExists() |
||
| 1313 | |||
| 1314 | /** |
||
| 1315 | * Helper function to convert a multi-dimensional array (associative or indexed) to an {@link ArrayList} or |
||
| 1316 | * {@link ArrayData} object structure, so that values can be used in templates. |
||
| 1317 | * |
||
| 1318 | * @param array $array The (single- or multi-dimensional) array to convert |
||
| 1319 | * @return object Either an {@link ArrayList} or {@link ArrayData} object, or the original item ($array) if $array |
||
| 1320 | * isn't an array. |
||
| 1321 | */ |
||
| 1322 | public static function array_to_viewabledata($array) |
||
| 1348 | |||
| 1349 | |||
| 1350 | |||
| 1351 | /** |
||
| 1352 | * Helper function to retrieve filtered commits from an environment |
||
| 1353 | * this environment depends on |
||
| 1354 | * |
||
| 1355 | * @return DataList |
||
| 1356 | */ |
||
| 1357 | public function getDependentFilteredCommits() |
||
| 1383 | |||
| 1384 | /** |
||
| 1385 | * Enable the maintenance page |
||
| 1386 | * |
||
| 1387 | * @param DeploynautLogFile $log |
||
| 1388 | */ |
||
| 1389 | public function enableMaintenace($log) |
||
| 1394 | |||
| 1395 | /** |
||
| 1396 | * Disable maintenance page |
||
| 1397 | * |
||
| 1398 | * @param DeploynautLogFile $log |
||
| 1399 | */ |
||
| 1400 | public function disableMaintenance($log) |
||
| 1405 | |||
| 1406 | protected function validate() |
||
| 1417 | } |
||
| 1418 |
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: