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 |
||
| 17 | class DNDeployment extends DataObject implements Finite\StatefulInterface, HasStateMachine { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private static $db = array( |
||
| 23 | "SHA" => "GitSHA", |
||
| 24 | "ResqueToken" => "Varchar(255)", |
||
| 25 | // The branch that was used to deploy this. Can't really be inferred from Git history because |
||
| 26 | // the commit could appear in lots of branches that are irrelevant to the user when it comes |
||
| 27 | // to deployment history, and the branch may have been deleted. |
||
| 28 | "Branch" => "Varchar(255)", |
||
| 29 | "State" => "Enum('New, Approved, Invalid, Queued, Deploying, Aborting, Completed, Failed', 'New')", |
||
| 30 | // JSON serialised DeploymentStrategy. |
||
| 31 | "Strategy" => "Text" |
||
| 32 | ); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private static $has_one = array( |
||
| 38 | "Environment" => "DNEnvironment", |
||
| 39 | "Deployer" => "Member", |
||
| 40 | ); |
||
| 41 | |||
| 42 | private static $default_sort = '"LastEdited" DESC'; |
||
| 43 | |||
| 44 | public function getTitle() { |
||
| 47 | |||
| 48 | private static $summary_fields = array( |
||
| 49 | 'LastEdited' => 'Last Edited', |
||
| 50 | 'SHA' => 'SHA', |
||
| 51 | 'Status' => 'Status', |
||
| 52 | 'Deployer.Name' => 'Deployer' |
||
| 53 | ); |
||
| 54 | |||
| 55 | public function getFiniteState() { |
||
| 58 | |||
| 59 | public function setFiniteState($state) { |
||
| 63 | |||
| 64 | public function getStatus() { |
||
| 67 | |||
| 68 | public function getMachine() { |
||
| 105 | |||
| 106 | |||
| 107 | public function onQueue() { |
||
| 116 | |||
| 117 | public function onAbort() { |
||
| 121 | |||
| 122 | public function Link() { |
||
| 125 | |||
| 126 | public function LogLink() { |
||
| 129 | |||
| 130 | public function canView($member = null) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Return a path to the log file. |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | protected function logfile() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return DeploynautLogFile |
||
| 148 | */ |
||
| 149 | public function log() { |
||
| 152 | |||
| 153 | public function LogContent() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * This remains here for backwards compatibility - we don't want to expose Resque status in here. |
||
| 159 | * Resque job (DeployJob) will change statuses as part of its execution. |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | public function ResqueStatus() { |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Fetch the git repository |
||
| 170 | * |
||
| 171 | * @return \Gitonomy\Git\Repository|null |
||
| 172 | */ |
||
| 173 | public function getRepository() { |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * Gets the commit from source. The result is cached upstream in Repository. |
||
| 183 | * |
||
| 184 | * @return \Gitonomy\Git\Commit|null |
||
| 185 | */ |
||
| 186 | public function getCommit() { |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * Gets the commit message. |
||
| 202 | * |
||
| 203 | * @return string|null |
||
| 204 | */ |
||
| 205 | public function getCommitMessage() { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Return all tags for the deployed commit. |
||
| 219 | * |
||
| 220 | * @return ArrayList |
||
| 221 | */ |
||
| 222 | public function getTags() { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Collate the list of additional flags to affix to this deployment. |
||
| 240 | * Elements of the array will be rendered literally. |
||
| 241 | * |
||
| 242 | * @return ArrayList |
||
| 243 | */ |
||
| 244 | public function getFullDeployMessages() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Fetches the latest tag for the deployed commit |
||
| 274 | * |
||
| 275 | * @return \Varchar|null |
||
| 276 | */ |
||
| 277 | public function getTag() { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return DeploymentStrategy |
||
| 287 | */ |
||
| 288 | public function getDeploymentStrategy() { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Return a list of things that are going to be deployed, such |
||
| 297 | * as the code version, and any infrastrucutral changes. |
||
| 298 | * |
||
| 299 | * @return ArrayList |
||
| 300 | */ |
||
| 301 | public function getChanges() { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Start a resque job for this deployment |
||
| 330 | * |
||
| 331 | * @return string Resque token |
||
| 332 | */ |
||
| 333 | protected function enqueueDeployment() { |
||
| 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.