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 | );  | 
            ||
| 64 | |||
| 65 | /**  | 
            ||
| 66 | * @var array  | 
            ||
| 67 | */  | 
            ||
| 68 | private static $has_one = array(  | 
            ||
| 69 | "Environment" => "DNEnvironment",  | 
            ||
| 70 | "Deployer" => "Member",  | 
            ||
| 71 | "Approver" => "Member",  | 
            ||
| 72 | "BackupDataTransfer" => "DNDataTransfer" // denotes an automated backup done for this deployment  | 
            ||
| 73 | );  | 
            ||
| 74 | |||
| 75 | private static $default_sort = '"LastEdited" DESC';  | 
            ||
| 76 | |||
| 77 | private static $dependencies = [  | 
            ||
| 78 | 'stateMachineFactory' => '%$StateMachineFactory'  | 
            ||
| 79 | ];  | 
            ||
| 80 | |||
| 81 | private static $summary_fields = array(  | 
            ||
| 82 | 'LastEdited' => 'Last Edited',  | 
            ||
| 83 | 'SHA' => 'SHA',  | 
            ||
| 84 | 'State' => 'State',  | 
            ||
| 85 | 'Deployer.Name' => 'Deployer'  | 
            ||
| 86 | );  | 
            ||
| 87 | |||
| 88 | /**  | 
            ||
| 89 | * Check for feature flags:  | 
            ||
| 90 | * - FLAG_NEWDEPLOY_ENABLED: set to true to enable globally  | 
            ||
| 91 | * - FLAG_NEWDEPLOY_ENABLED_FOR_MEMBERS: set to semicolon-separated list of email addresses of allowed users.  | 
            ||
| 92 | *  | 
            ||
| 93 | * @return boolean  | 
            ||
| 94 | */  | 
            ||
| 95 | View Code Duplication | 	public static function flag_new_deploy_enabled() { | 
            |
| 108 | |||
| 109 | 	public function setResqueToken($token) { | 
            ||
| 112 | |||
| 113 | 	public function getFiniteState() { | 
            ||
| 116 | |||
| 117 | 	public function setFiniteState($state) { | 
            ||
| 121 | |||
| 122 | 	public function getStatus() { | 
            ||
| 125 | |||
| 126 | 	public function getMachine() { | 
            ||
| 129 | |||
| 130 | 	public function Link() { | 
            ||
| 137 | |||
| 138 | 	public function LogLink() { | 
            ||
| 141 | |||
| 142 | 	public function canView($member = null) { | 
            ||
| 145 | |||
| 146 | /**  | 
            ||
| 147 | * Return a path to the log file.  | 
            ||
| 148 | * @return string  | 
            ||
| 149 | */  | 
            ||
| 150 | 	protected function logfile() { | 
            ||
| 157 | |||
| 158 | /**  | 
            ||
| 159 | * @return DeploynautLogFile  | 
            ||
| 160 | */  | 
            ||
| 161 | 	public function log() { | 
            ||
| 164 | |||
| 165 | 	public function LogContent() { | 
            ||
| 168 | |||
| 169 | /**  | 
            ||
| 170 | * This remains here for backwards compatibility - we don't want to expose Resque status in here.  | 
            ||
| 171 | * Resque job (DeployJob) will change statuses as part of its execution.  | 
            ||
| 172 | *  | 
            ||
| 173 | * @return string  | 
            ||
| 174 | */  | 
            ||
| 175 | 	public function ResqueStatus() { | 
            ||
| 178 | |||
| 179 | /**  | 
            ||
| 180 | * Fetch the git repository  | 
            ||
| 181 | *  | 
            ||
| 182 | * @return \Gitonomy\Git\Repository|null  | 
            ||
| 183 | */  | 
            ||
| 184 | 	public function getRepository() { | 
            ||
| 190 | |||
| 191 | /**  | 
            ||
| 192 | * Gets the commit from source. The result is cached upstream in Repository.  | 
            ||
| 193 | *  | 
            ||
| 194 | * @return \Gitonomy\Git\Commit|null  | 
            ||
| 195 | */  | 
            ||
| 196 | View Code Duplication | 	public function getCommit() { | 
            |
| 208 | |||
| 209 | /**  | 
            ||
| 210 | * Get the commit URL to the commit associated with this deployment.  | 
            ||
| 211 | * @return null|string  | 
            ||
| 212 | */  | 
            ||
| 213 | 	public function getCommitURL() { | 
            ||
| 228 | |||
| 229 | /**  | 
            ||
| 230 | * Gets the commit message.  | 
            ||
| 231 | *  | 
            ||
| 232 | * @return string|null  | 
            ||
| 233 | */  | 
            ||
| 234 | View Code Duplication | 	public function getCommitMessage() { | 
            |
| 245 | |||
| 246 | /**  | 
            ||
| 247 | * Return all tags for the deployed commit.  | 
            ||
| 248 | *  | 
            ||
| 249 | * @return ArrayList  | 
            ||
| 250 | */  | 
            ||
| 251 | 	public function getTags() { | 
            ||
| 267 | |||
| 268 | /**  | 
            ||
| 269 | * Collate the list of additional flags to affix to this deployment.  | 
            ||
| 270 | * Elements of the array will be rendered literally.  | 
            ||
| 271 | *  | 
            ||
| 272 | * @return ArrayList  | 
            ||
| 273 | */  | 
            ||
| 274 | 	public function getFullDeployMessages() { | 
            ||
| 301 | |||
| 302 | /**  | 
            ||
| 303 | * Fetches the latest tag for the deployed commit  | 
            ||
| 304 | *  | 
            ||
| 305 | * @return \Varchar|null  | 
            ||
| 306 | */  | 
            ||
| 307 | 	public function getTag() { | 
            ||
| 314 | |||
| 315 | /**  | 
            ||
| 316 | * @return DeploymentStrategy  | 
            ||
| 317 | */  | 
            ||
| 318 | 	public function getDeploymentStrategy() { | 
            ||
| 324 | |||
| 325 | /**  | 
            ||
| 326 | * Return a list of things that are going to be deployed, such  | 
            ||
| 327 | * as the code version, and any infrastructural changes.  | 
            ||
| 328 | *  | 
            ||
| 329 | * @return ArrayList  | 
            ||
| 330 | */  | 
            ||
| 331 | 	public function getChanges() { | 
            ||
| 357 | |||
| 358 | /**  | 
            ||
| 359 | * Start a resque job for this deployment  | 
            ||
| 360 | *  | 
            ||
| 361 | * @return string Resque token  | 
            ||
| 362 | */  | 
            ||
| 363 | 	public function enqueueDeployment() { | 
            ||
| 402 | |||
| 403 | 	public function getSigFile() { | 
            ||
| 415 | |||
| 416 | /**  | 
            ||
| 417 | * Signal the worker to self-abort. If we had a reliable way of figuring out the right PID,  | 
            ||
| 418 | * we could posix_kill directly, but Resque seems to not provide a way to find out the PID  | 
            ||
| 419 | * from the job nor worker.  | 
            ||
| 420 | */  | 
            ||
| 421 | 	public function setSignal($signal) { | 
            ||
| 426 | }  | 
            ||
| 427 | 
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.