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 DNDeployment 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 DNDeployment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class DNDeployment extends DataObject implements Finite\StatefulInterface, HasStateMachine { |
||
| 19 | |||
| 20 | const STATE_NEW = 'New'; |
||
| 21 | const STATE_SUBMITTED = 'Submitted'; |
||
| 22 | const STATE_INVALID = 'Invalid'; |
||
| 23 | const STATE_APPROVED = 'Approved'; |
||
| 24 | const STATE_REJECTED = 'Rejected'; |
||
| 25 | const STATE_QUEUED = 'Queued'; |
||
| 26 | const STATE_DEPLOYING = 'Deploying'; |
||
| 27 | const STATE_ABORTING = 'Aborting'; |
||
| 28 | const STATE_COMPLETED = 'Completed'; |
||
| 29 | const STATE_FAILED = 'Failed'; |
||
| 30 | |||
| 31 | const TR_NEW = 'new'; |
||
| 32 | const TR_SUBMIT = 'submit'; |
||
| 33 | const TR_INVALIDATE = 'invalidate'; |
||
| 34 | const TR_APPROVE = 'approve'; |
||
| 35 | const TR_REJECT = 'reject'; |
||
| 36 | const TR_QUEUE = 'queue'; |
||
| 37 | const TR_DEPLOY = 'deploy'; |
||
| 38 | const TR_ABORT = 'abort'; |
||
| 39 | const TR_COMPLETE = 'complete'; |
||
| 40 | const TR_FAIL = 'fail'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private static $db = array( |
||
| 46 | "SHA" => "GitSHA", |
||
| 47 | "ResqueToken" => "Varchar(255)", |
||
| 48 | // The branch that was used to deploy this. Can't really be inferred from Git history because |
||
| 49 | // the commit could appear in lots of branches that are irrelevant to the user when it comes |
||
| 50 | // to deployment history, and the branch may have been deleted. |
||
| 51 | "Branch" => "Varchar(255)", |
||
| 52 | // is it a branch, tag etc, see GitDispatcher REF_TYPE_* constants |
||
| 53 | "RefType" => "Int", |
||
| 54 | "State" => "Enum('New, Submitted, Invalid, Approved, Rejected, Queued, Deploying, Aborting, Completed, Failed', 'New')", |
||
| 55 | // JSON serialised DeploymentStrategy. |
||
| 56 | "Strategy" => "Text", |
||
| 57 | "Title" => "Varchar(255)", |
||
| 58 | "Summary" => "Text", |
||
| 59 | // the date and time the deploy was queued |
||
| 60 | "DeployStarted" => "SS_Datetime", |
||
| 61 | // the date and time a deployment was requested to be approved |
||
| 62 | "DeployRequested" => "SS_Datetime", |
||
| 63 | "RejectedReason" => "Text" |
||
| 64 | ); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | private static $has_one = array( |
||
| 70 | "Environment" => "DNEnvironment", |
||
| 71 | "Deployer" => "Member", |
||
| 72 | "Approver" => "Member", |
||
| 73 | "BackupDataTransfer" => "DNDataTransfer" // denotes an automated backup done for this deployment |
||
| 74 | ); |
||
| 75 | |||
| 76 | private static $default_sort = '"LastEdited" DESC'; |
||
| 77 | |||
| 78 | private static $dependencies = [ |
||
| 79 | 'stateMachineFactory' => '%$StateMachineFactory' |
||
| 80 | ]; |
||
| 81 | |||
| 82 | private static $summary_fields = array( |
||
| 83 | 'LastEdited' => 'Last Edited', |
||
| 84 | 'SHA' => 'SHA', |
||
| 85 | 'State' => 'State', |
||
| 86 | 'Deployer.Name' => 'Deployer' |
||
| 87 | ); |
||
| 88 | |||
| 89 | public function setResqueToken($token) { |
||
| 92 | |||
| 93 | public function getFiniteState() { |
||
| 96 | |||
| 97 | public function setFiniteState($state) { |
||
| 101 | |||
| 102 | public function getStatus() { |
||
| 105 | |||
| 106 | public function getMachine() { |
||
| 109 | |||
| 110 | public function Link() { |
||
| 117 | |||
| 118 | public function LogLink() { |
||
| 121 | |||
| 122 | public function canView($member = null) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Return a path to the log file. |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | protected function logfile() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return DeploynautLogFile |
||
| 140 | */ |
||
| 141 | public function log() { |
||
| 144 | |||
| 145 | public function LogContent() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * This remains here for backwards compatibility - we don't want to expose Resque status in here. |
||
| 151 | * Resque job (DeployJob) will change statuses as part of its execution. |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function ResqueStatus() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Fetch the git repository |
||
| 161 | * |
||
| 162 | * @return \Gitonomy\Git\Repository|null |
||
| 163 | */ |
||
| 164 | public function getRepository() { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Gets the commit from source. The result is cached upstream in Repository. |
||
| 173 | * |
||
| 174 | * @return \Gitonomy\Git\Commit|null |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function getCommit() { |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Get the commit URL to the commit associated with this deployment. |
||
| 191 | * @return null|string |
||
| 192 | */ |
||
| 193 | public function getCommitURL() { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Gets the commit message. |
||
| 211 | * |
||
| 212 | * @return string|null |
||
| 213 | */ |
||
| 214 | View Code Duplication | public function getCommitMessage() { |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Gets the commit message. |
||
| 228 | * |
||
| 229 | * @return string|null |
||
| 230 | */ |
||
| 231 | View Code Duplication | public function getCommitSubjectMessage() { |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Return all tags for the deployed commit. |
||
| 245 | * |
||
| 246 | * @return ArrayList |
||
| 247 | */ |
||
| 248 | public function getTags() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Collate the list of additional flags to affix to this deployment. |
||
| 267 | * Elements of the array will be rendered literally. |
||
| 268 | * |
||
| 269 | * @return ArrayList |
||
| 270 | */ |
||
| 271 | public function getFullDeployMessages() { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Fetches the latest tag for the deployed commit |
||
| 301 | * |
||
| 302 | * @return \Varchar|null |
||
| 303 | */ |
||
| 304 | public function getTag() { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return DeploymentStrategy |
||
| 314 | */ |
||
| 315 | public function getDeploymentStrategy() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Return a list of things that are going to be deployed, such |
||
| 324 | * as the code version, and any infrastructural changes. |
||
| 325 | * |
||
| 326 | * @return ArrayList |
||
| 327 | */ |
||
| 328 | public function getChanges() { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Start a resque job for this deployment |
||
| 357 | * |
||
| 358 | * @return string Resque token |
||
| 359 | */ |
||
| 360 | public function enqueueDeployment() { |
||
| 399 | |||
| 400 | public function getSigFile() { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Signal the worker to self-abort. If we had a reliable way of figuring out the right PID, |
||
| 415 | * we could posix_kill directly, but Resque seems to not provide a way to find out the PID |
||
| 416 | * from the job nor worker. |
||
| 417 | */ |
||
| 418 | public function setSignal($signal) { |
||
| 423 | } |
||
| 424 |
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.