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_QUEUED = 'Queued'; |
||
24 | const STATE_DEPLOYING = 'Deploying'; |
||
25 | const STATE_ABORTING = 'Aborting'; |
||
26 | const STATE_COMPLETED = 'Completed'; |
||
27 | const STATE_FAILED = 'Failed'; |
||
28 | |||
29 | const TR_SUBMIT = 'submit'; |
||
30 | const TR_INVALIDATE = 'invalidate'; |
||
31 | const TR_QUEUE = 'queue'; |
||
32 | const TR_DEPLOY = 'deploy'; |
||
33 | const TR_ABORT = 'abort'; |
||
34 | const TR_COMPLETE = 'complete'; |
||
35 | const TR_FAIL = 'fail'; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private static $db = array( |
||
41 | "SHA" => "GitSHA", |
||
42 | "ResqueToken" => "Varchar(255)", |
||
43 | // The branch that was used to deploy this. Can't really be inferred from Git history because |
||
44 | // the commit could appear in lots of branches that are irrelevant to the user when it comes |
||
45 | // to deployment history, and the branch may have been deleted. |
||
46 | "Branch" => "Varchar(255)", |
||
47 | // is it a branch, tag etc, see GitDispatcher REF_TYPE_* constants |
||
48 | "RefType" => "Int", |
||
49 | "State" => "Enum('New, Submitted, Invalid, Queued, Deploying, Aborting, Completed, Failed', 'New')", |
||
50 | // JSON serialised DeploymentStrategy. |
||
51 | "Strategy" => "Text", |
||
52 | "Summary" => "Text", |
||
53 | // the date and time the deploy was queued |
||
54 | "DeployStarted" => "SS_Datetime", |
||
55 | "DeployRequested" => "SS_Datetime" |
||
56 | ); |
||
57 | |||
58 | /** |
||
59 | * @var array |
||
60 | */ |
||
61 | private static $has_one = array( |
||
62 | "Environment" => "DNEnvironment", |
||
63 | "Deployer" => "Member", |
||
64 | "Approver" => "Member", |
||
65 | "BackupDataTransfer" => "DNDataTransfer" // denotes an automated backup done for this deployment |
||
66 | ); |
||
67 | |||
68 | private static $default_sort = '"LastEdited" DESC'; |
||
69 | |||
70 | private static $dependencies = [ |
||
71 | 'stateMachineFactory' => '%$StateMachineFactory' |
||
72 | ]; |
||
73 | |||
74 | public function getTitle() { |
||
77 | |||
78 | private static $summary_fields = array( |
||
79 | 'LastEdited' => 'Last Edited', |
||
80 | 'SHA' => 'SHA', |
||
81 | 'State' => 'State', |
||
82 | 'Deployer.Name' => 'Deployer' |
||
83 | ); |
||
84 | |||
85 | public function setResqueToken($token) { |
||
88 | |||
89 | public function getFiniteState() { |
||
92 | |||
93 | public function setFiniteState($state) { |
||
97 | |||
98 | public function getStatus() { |
||
101 | |||
102 | public function getMachine() { |
||
105 | |||
106 | public function Link() { |
||
109 | |||
110 | public function LogLink() { |
||
113 | |||
114 | public function canView($member = null) { |
||
117 | |||
118 | /** |
||
119 | * Return a path to the log file. |
||
120 | * @return string |
||
121 | */ |
||
122 | protected function logfile() { |
||
129 | |||
130 | /** |
||
131 | * @return DeploynautLogFile |
||
132 | */ |
||
133 | public function log() { |
||
136 | |||
137 | public function LogContent() { |
||
140 | |||
141 | /** |
||
142 | * This remains here for backwards compatibility - we don't want to expose Resque status in here. |
||
143 | * Resque job (DeployJob) will change statuses as part of its execution. |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | public function ResqueStatus() { |
||
150 | |||
151 | /** |
||
152 | * Fetch the git repository |
||
153 | * |
||
154 | * @return \Gitonomy\Git\Repository|null |
||
155 | */ |
||
156 | public function getRepository() { |
||
162 | |||
163 | /** |
||
164 | * Gets the commit from source. The result is cached upstream in Repository. |
||
165 | * |
||
166 | * @return \Gitonomy\Git\Commit|null |
||
167 | */ |
||
168 | View Code Duplication | public function getCommit() { |
|
169 | $repo = $this->getRepository(); |
||
170 | if($repo) { |
||
171 | try { |
||
172 | return $this->Environment()->getCommit($this->SHA); |
||
173 | } catch(Gitonomy\Git\Exception\ReferenceNotFoundException $ex) { |
||
174 | return null; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | return null; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Get the commit URL to the commit associated with this deployment. |
||
183 | * @return null|string |
||
184 | */ |
||
185 | public function getCommitURL() { |
||
200 | |||
201 | /** |
||
202 | * Gets the commit message. |
||
203 | * |
||
204 | * @return string|null |
||
205 | */ |
||
206 | View Code Duplication | public function getCommitMessage() { |
|
217 | |||
218 | /** |
||
219 | * Return all tags for the deployed commit. |
||
220 | * |
||
221 | * @return ArrayList |
||
222 | */ |
||
223 | public function getTags() { |
||
236 | |||
237 | /** |
||
238 | * Collate the list of additional flags to affix to this deployment. |
||
239 | * Elements of the array will be rendered literally. |
||
240 | * |
||
241 | * @return ArrayList |
||
242 | */ |
||
243 | public function getFullDeployMessages() { |
||
270 | |||
271 | /** |
||
272 | * Fetches the latest tag for the deployed commit |
||
273 | * |
||
274 | * @return \Varchar|null |
||
275 | */ |
||
276 | public function getTag() { |
||
283 | |||
284 | /** |
||
285 | * @return DeploymentStrategy |
||
286 | */ |
||
287 | public function getDeploymentStrategy() { |
||
293 | |||
294 | /** |
||
295 | * Return a list of things that are going to be deployed, such |
||
296 | * as the code version, and any infrastructural changes. |
||
297 | * |
||
298 | * @return ArrayList |
||
299 | */ |
||
300 | public function getChanges() { |
||
326 | |||
327 | /** |
||
328 | * Start a resque job for this deployment |
||
329 | * |
||
330 | * @return string Resque token |
||
331 | */ |
||
332 | public function enqueueDeployment() { |
||
371 | |||
372 | public function getSigFile() { |
||
384 | |||
385 | /** |
||
386 | * Signal the worker to self-abort. If we had a reliable way of figuring out the right PID, |
||
387 | * we could posix_kill directly, but Resque seems to not provide a way to find out the PID |
||
388 | * from the job nor worker. |
||
389 | */ |
||
390 | public function setSignal($signal) { |
||
395 | } |
||
396 |
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
.