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 | ); |
||
| 59 | |||
| 60 | private static $default_sort = '"LastEdited" DESC'; |
||
| 61 | |||
| 62 | private static $dependencies = [ |
||
| 63 | 'stateMachineFactory' => '%$StateMachineFactory' |
||
| 64 | ]; |
||
| 65 | |||
| 66 | public function getTitle() { |
||
| 69 | |||
| 70 | private static $summary_fields = array( |
||
| 71 | 'LastEdited' => 'Last Edited', |
||
| 72 | 'SHA' => 'SHA', |
||
| 73 | 'State' => 'State', |
||
| 74 | 'Deployer.Name' => 'Deployer' |
||
| 75 | ); |
||
| 76 | |||
| 77 | public function setResqueToken($token) { |
||
| 80 | |||
| 81 | public function getFiniteState() { |
||
| 84 | |||
| 85 | public function setFiniteState($state) { |
||
| 89 | |||
| 90 | public function getStatus() { |
||
| 93 | |||
| 94 | public function getMachine() { |
||
| 97 | |||
| 98 | public function Link() { |
||
| 101 | |||
| 102 | public function LogLink() { |
||
| 105 | |||
| 106 | public function canView($member = null) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Return a path to the log file. |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | protected function logfile() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return DeploynautLogFile |
||
| 124 | */ |
||
| 125 | public function log() { |
||
| 128 | |||
| 129 | public function LogContent() { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * This remains here for backwards compatibility - we don't want to expose Resque status in here. |
||
| 135 | * Resque job (DeployJob) will change statuses as part of its execution. |
||
| 136 | * |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function ResqueStatus() { |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Fetch the git repository |
||
| 146 | * |
||
| 147 | * @return \Gitonomy\Git\Repository|null |
||
| 148 | */ |
||
| 149 | public function getRepository() { |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * Gets the commit from source. The result is cached upstream in Repository. |
||
| 159 | * |
||
| 160 | * @return \Gitonomy\Git\Commit|null |
||
| 161 | */ |
||
| 162 | public function getCommit() { |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * Gets the commit message. |
||
| 178 | * |
||
| 179 | * @return string|null |
||
| 180 | */ |
||
| 181 | public function getCommitMessage() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Return all tags for the deployed commit. |
||
| 195 | * |
||
| 196 | * @return ArrayList |
||
| 197 | */ |
||
| 198 | public function getTags() { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Collate the list of additional flags to affix to this deployment. |
||
| 216 | * Elements of the array will be rendered literally. |
||
| 217 | * |
||
| 218 | * @return ArrayList |
||
| 219 | */ |
||
| 220 | public function getFullDeployMessages() { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Fetches the latest tag for the deployed commit |
||
| 250 | * |
||
| 251 | * @return \Varchar|null |
||
| 252 | */ |
||
| 253 | public function getTag() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return DeploymentStrategy |
||
| 263 | */ |
||
| 264 | public function getDeploymentStrategy() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Return a list of things that are going to be deployed, such |
||
| 273 | * as the code version, and any infrastrucutral changes. |
||
| 274 | * |
||
| 275 | * @return ArrayList |
||
| 276 | */ |
||
| 277 | public function getChanges() { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Start a resque job for this deployment |
||
| 306 | * |
||
| 307 | * @return string Resque token |
||
| 308 | */ |
||
| 309 | public function enqueueDeployment() { |
||
| 348 | |||
| 349 | public function getSigFile() { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Signal the worker to self-abort. If we had a reliable way of figuring out the right PID, |
||
| 364 | * we could posix_kill directly, but Resque seems to not provide a way to find out the PID |
||
| 365 | * from the job nor worker. |
||
| 366 | */ |
||
| 367 | public function setSignal($signal) { |
||
| 372 | } |
||
| 373 |
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.