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 |
||
| 15 | class DNDeployment extends DataObject implements Finite\StatefulInterface, HasStateMachine { |
||
| 16 | |||
| 17 | const STATE_NEW = 'New'; |
||
| 18 | const STATE_SUBMITTED = 'Submitted'; |
||
| 19 | const STATE_INVALID = 'Invalid'; |
||
| 20 | const STATE_QUEUED = 'Queued'; |
||
| 21 | const STATE_DEPLOYING = 'Deploying'; |
||
| 22 | const STATE_ABORTING = 'Aborting'; |
||
| 23 | const STATE_COMPLETED = 'Completed'; |
||
| 24 | const STATE_FAILED = 'Failed'; |
||
| 25 | |||
| 26 | const TR_SUBMIT = 'submit'; |
||
| 27 | const TR_INVALIDATE = 'invalidate'; |
||
| 28 | const TR_QUEUE = 'queue'; |
||
| 29 | const TR_DEPLOY = 'deploy'; |
||
| 30 | const TR_ABORT = 'abort'; |
||
| 31 | const TR_COMPLETE = 'complete'; |
||
| 32 | const TR_FAIL = 'fail'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private static $db = array( |
||
| 38 | "SHA" => "GitSHA", |
||
| 39 | "ResqueToken" => "Varchar(255)", |
||
| 40 | // The branch that was used to deploy this. Can't really be inferred from Git history because |
||
| 41 | // the commit could appear in lots of branches that are irrelevant to the user when it comes |
||
| 42 | // to deployment history, and the branch may have been deleted. |
||
| 43 | "Branch" => "Varchar(255)", |
||
| 44 | "State" => "Enum('New, Submitted, Invalid, Queued, Deploying, Aborting, Completed, Failed', 'New')", |
||
| 45 | // JSON serialised DeploymentStrategy. |
||
| 46 | "Strategy" => "Text", |
||
| 47 | "Summary" => "Text", |
||
| 48 | "DatePlanned" => "SS_Datetime" |
||
| 49 | ); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private static $has_one = array( |
||
| 55 | "Environment" => "DNEnvironment", |
||
| 56 | "Deployer" => "Member", |
||
| 57 | "Approver" => "Member", |
||
| 58 | "BackupDataTransfer" => "DNDataTransfer" // denotes an automated backup done for this deployment |
||
| 59 | ); |
||
| 60 | |||
| 61 | private static $default_sort = '"LastEdited" DESC'; |
||
| 62 | |||
| 63 | private static $dependencies = [ |
||
| 64 | 'stateMachineFactory' => '%$StateMachineFactory' |
||
| 65 | ]; |
||
| 66 | |||
| 67 | public function getTitle() { |
||
| 68 | return "#{$this->ID}: {$this->SHA} (Status: {$this->Status})"; |
||
|
|
|||
| 69 | } |
||
| 70 | |||
| 71 | private static $summary_fields = array( |
||
| 72 | 'LastEdited' => 'Last Edited', |
||
| 73 | 'SHA' => 'SHA', |
||
| 74 | 'State' => 'State', |
||
| 75 | 'Deployer.Name' => 'Deployer' |
||
| 76 | ); |
||
| 77 | |||
| 78 | public function setResqueToken($token) { |
||
| 79 | $this->ResqueToken = $token; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function getFiniteState() { |
||
| 83 | return $this->State; |
||
| 84 | } |
||
| 85 | |||
| 86 | public function setFiniteState($state) { |
||
| 87 | $this->State = $state; |
||
| 88 | $this->write(); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function getStatus() { |
||
| 92 | return $this->State; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function getMachine() { |
||
| 96 | return $this->stateMachineFactory->forDNDeployment($this); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function Link() { |
||
| 100 | return Controller::join_links($this->Environment()->Link(), 'deploy', $this->ID); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function LogLink() { |
||
| 104 | return $this->Link() . '/log'; |
||
| 105 | } |
||
| 106 | |||
| 107 | public function canView($member = null) { |
||
| 108 | return $this->Environment()->canView($member); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Return a path to the log file. |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | protected function logfile() { |
||
| 116 | return sprintf( |
||
| 117 | '%s.%s.log', |
||
| 118 | $this->Environment()->getFullName('.'), |
||
| 119 | $this->ID |
||
| 120 | ); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return DeploynautLogFile |
||
| 125 | */ |
||
| 126 | public function log() { |
||
| 127 | return Injector::inst()->createWithArgs('DeploynautLogFile', array($this->logfile())); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function LogContent() { |
||
| 131 | return $this->log()->content(); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * This remains here for backwards compatibility - we don't want to expose Resque status in here. |
||
| 136 | * Resque job (DeployJob) will change statuses as part of its execution. |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function ResqueStatus() { |
||
| 141 | return $this->State; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Fetch the git repository |
||
| 146 | * |
||
| 147 | * @return \Gitonomy\Git\Repository|null |
||
| 148 | */ |
||
| 149 | public function getRepository() { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Gets the commit from source. The result is cached upstream in Repository. |
||
| 158 | * |
||
| 159 | * @return \Gitonomy\Git\Commit|null |
||
| 160 | */ |
||
| 161 | public function getCommit() { |
||
| 162 | $repo = $this->getRepository(); |
||
| 163 | if($repo) { |
||
| 164 | try { |
||
| 165 | return $repo->getCommit($this->SHA); |
||
| 166 | } catch(Gitonomy\Git\Exception\ReferenceNotFoundException $ex) { |
||
| 167 | return null; |
||
| 168 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get the commit URL to the commit associated with this deployment. |
||
| 176 | * @return null|string |
||
| 177 | */ |
||
| 178 | public function getCommitURL() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Gets the commit message. |
||
| 196 | * |
||
| 197 | * @return string|null |
||
| 198 | */ |
||
| 199 | public function getCommitMessage() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Return all tags for the deployed commit. |
||
| 213 | * |
||
| 214 | * @return ArrayList |
||
| 215 | */ |
||
| 216 | public function getTags() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Collate the list of additional flags to affix to this deployment. |
||
| 234 | * Elements of the array will be rendered literally. |
||
| 235 | * |
||
| 236 | * @return ArrayList |
||
| 237 | */ |
||
| 238 | public function getFullDeployMessages() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Fetches the latest tag for the deployed commit |
||
| 268 | * |
||
| 269 | * @return \Varchar|null |
||
| 270 | */ |
||
| 271 | public function getTag() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return DeploymentStrategy |
||
| 281 | */ |
||
| 282 | public function getDeploymentStrategy() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Return a list of things that are going to be deployed, such |
||
| 291 | * as the code version, and any infrastructural changes. |
||
| 292 | * |
||
| 293 | * @return ArrayList |
||
| 294 | */ |
||
| 295 | public function getChanges() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Start a resque job for this deployment |
||
| 324 | * |
||
| 325 | * @return string Resque token |
||
| 326 | */ |
||
| 327 | public function enqueueDeployment() { |
||
| 366 | |||
| 367 | public function getSigFile() { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Signal the worker to self-abort. If we had a reliable way of figuring out the right PID, |
||
| 382 | * we could posix_kill directly, but Resque seems to not provide a way to find out the PID |
||
| 383 | * from the job nor worker. |
||
| 384 | */ |
||
| 385 | public function setSignal($signal) { |
||
| 390 | } |
||
| 391 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.