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 |
||
| 19 | class DNDeployment extends DataObject implements Finite\StatefulInterface, HasStateMachine { |
||
| 20 | |||
| 21 | const STATE_NEW = 'New'; |
||
| 22 | const STATE_SUBMITTED = 'Submitted'; |
||
| 23 | const STATE_INVALID = 'Invalid'; |
||
| 24 | const STATE_APPROVED = 'Approved'; |
||
| 25 | const STATE_REJECTED = 'Rejected'; |
||
| 26 | const STATE_QUEUED = 'Queued'; |
||
| 27 | const STATE_DEPLOYING = 'Deploying'; |
||
| 28 | const STATE_ABORTING = 'Aborting'; |
||
| 29 | const STATE_COMPLETED = 'Completed'; |
||
| 30 | const STATE_FAILED = 'Failed'; |
||
| 31 | // Deleted is used as 'soft-delete' which will in all regards keep the record in the DB |
||
| 32 | // but not display it in the UI. This is for auditing and also for UI history updates |
||
| 33 | const STATE_DELETED = 'Deleted'; |
||
| 34 | |||
| 35 | const TR_NEW = 'new'; |
||
| 36 | const TR_SUBMIT = 'submit'; |
||
| 37 | const TR_INVALIDATE = 'invalidate'; |
||
| 38 | const TR_APPROVE = 'approve'; |
||
| 39 | const TR_REJECT = 'reject'; |
||
| 40 | const TR_QUEUE = 'queue'; |
||
| 41 | const TR_DEPLOY = 'deploy'; |
||
| 42 | const TR_ABORT = 'abort'; |
||
| 43 | const TR_COMPLETE = 'complete'; |
||
| 44 | const TR_FAIL = 'fail'; |
||
| 45 | const TR_DELETE = 'delete'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private static $db = array( |
||
| 51 | "SHA" => "GitSHA", |
||
| 52 | "ResqueToken" => "Varchar(255)", |
||
| 53 | // is it a branch, tag etc, see GitDispatcher REF_TYPE_* constants |
||
| 54 | "RefType" => "Int", |
||
| 55 | // The ref name that was used to deploy this, e.g. branch name or tag. |
||
| 56 | // Can't really be inferred from Git history because the commit could appear in lots of |
||
| 57 | // branches/tags that are irrelevant to the user when it comes to deployment history, and the reference |
||
| 58 | // may have been deleted. |
||
| 59 | "RefName" => "Varchar(255)", |
||
| 60 | "State" => "Enum('New, Submitted, Invalid, Approved, Rejected, Queued, Deploying, Aborting, Completed, Failed, Deleted', 'New')", |
||
| 61 | // JSON serialised DeploymentStrategy. |
||
| 62 | "Strategy" => "Text", |
||
| 63 | "Title" => "Varchar(255)", |
||
| 64 | "Summary" => "Text", |
||
| 65 | // the date and time the deploy was queued |
||
| 66 | "DeployStarted" => "SS_Datetime", |
||
| 67 | // the date and time a deployment was requested to be approved |
||
| 68 | "DeployRequested" => "SS_Datetime", |
||
| 69 | "RejectedReason" => "Text" |
||
| 70 | ); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | private static $has_one = array( |
||
| 76 | "Environment" => "DNEnvironment", |
||
| 77 | "Deployer" => "Member", |
||
| 78 | "Approver" => "Member", |
||
| 79 | "BackupDataTransfer" => "DNDataTransfer" // denotes an automated backup done for this deployment |
||
| 80 | ); |
||
| 81 | |||
| 82 | private static $default_sort = '"LastEdited" DESC'; |
||
| 83 | |||
| 84 | private static $dependencies = [ |
||
| 85 | 'stateMachineFactory' => '%$StateMachineFactory' |
||
| 86 | ]; |
||
| 87 | |||
| 88 | private static $summary_fields = array( |
||
| 89 | 'LastEdited' => 'Last Edited', |
||
| 90 | 'SHA' => 'SHA', |
||
| 91 | 'State' => 'State', |
||
| 92 | 'Deployer.Name' => 'Deployer' |
||
| 93 | ); |
||
| 94 | |||
| 95 | public function setResqueToken($token) { |
||
| 98 | |||
| 99 | public function getFiniteState() { |
||
| 102 | |||
| 103 | public function setFiniteState($state) { |
||
| 107 | |||
| 108 | public function getStatus() { |
||
| 111 | |||
| 112 | public function getMachine() { |
||
| 115 | |||
| 116 | public function Link() { |
||
| 123 | |||
| 124 | public function LogLink() { |
||
| 127 | |||
| 128 | public function canView($member = null) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Return a path to the log file. |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | protected function logfile() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return DeploynautLogFile |
||
| 146 | */ |
||
| 147 | public function log() { |
||
| 150 | |||
| 151 | public function LogContent() { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * This remains here for backwards compatibility - we don't want to expose Resque status in here. |
||
| 157 | * Resque job (DeployJob) will change statuses as part of its execution. |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function ResqueStatus() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Fetch the git repository |
||
| 167 | * |
||
| 168 | * @return \Gitonomy\Git\Repository|null |
||
| 169 | */ |
||
| 170 | public function getRepository() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Gets the commit from source. The result is cached upstream in Repository. |
||
| 179 | * |
||
| 180 | * @return \Gitonomy\Git\Commit|null |
||
| 181 | */ |
||
| 182 | View Code Duplication | public function getCommit() { |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Get the commit URL to the commit associated with this deployment. |
||
| 197 | * @return null|string |
||
| 198 | */ |
||
| 199 | public function getCommitURL() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Gets the commit message. |
||
| 217 | * |
||
| 218 | * @return string|null |
||
| 219 | */ |
||
| 220 | View Code Duplication | public function getCommitMessage() { |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Gets the commit message. |
||
| 234 | * |
||
| 235 | * @return string|null |
||
| 236 | */ |
||
| 237 | View Code Duplication | public function getCommitSubjectMessage() { |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Return all tags for the deployed commit. |
||
| 251 | * |
||
| 252 | * @return ArrayList |
||
| 253 | */ |
||
| 254 | public function getTags() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Collate the list of additional flags to affix to this deployment. |
||
| 273 | * Elements of the array will be rendered literally. |
||
| 274 | * |
||
| 275 | * @return ArrayList |
||
| 276 | */ |
||
| 277 | public function getFullDeployMessages() { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Fetches the latest tag for the deployed commit |
||
| 307 | * |
||
| 308 | * @return \Varchar|null |
||
| 309 | */ |
||
| 310 | public function getTag() { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return DeploymentStrategy |
||
| 320 | */ |
||
| 321 | public function getDeploymentStrategy() { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Return a list of things that are going to be deployed, such |
||
| 330 | * as the code version, and any infrastructural changes. |
||
| 331 | * |
||
| 332 | * @return ArrayList |
||
| 333 | */ |
||
| 334 | public function getChanges() { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Start a resque job for this deployment |
||
| 363 | * |
||
| 364 | * @return string Resque token |
||
| 365 | */ |
||
| 366 | public function enqueueDeployment() { |
||
| 405 | |||
| 406 | public function getSigFile() { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Signal the worker to self-abort. If we had a reliable way of figuring out the right PID, |
||
| 421 | * we could posix_kill directly, but Resque seems to not provide a way to find out the PID |
||
| 422 | * from the job nor worker. |
||
| 423 | */ |
||
| 424 | public function setSignal($signal) { |
||
| 429 | } |
||
| 430 |
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.