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 | "DeploymentDate" => "SS_Datetime" |
||
48 | ); |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | private static $has_one = array( |
||
54 | "Environment" => "DNEnvironment", |
||
55 | "Deployer" => "Member", |
||
56 | "Approver" => "Member" |
||
57 | ); |
||
58 | |||
59 | private static $default_sort = '"LastEdited" DESC'; |
||
60 | |||
61 | private static $dependencies = [ |
||
62 | 'stateMachineFactory' => '%$StateMachineFactory' |
||
63 | ]; |
||
64 | |||
65 | public function getTitle() { |
||
66 | return "#{$this->ID}: {$this->SHA} (Status: {$this->Status})"; |
||
67 | } |
||
68 | |||
69 | private static $summary_fields = array( |
||
70 | 'LastEdited' => 'Last Edited', |
||
71 | 'SHA' => 'SHA', |
||
72 | 'State' => 'State', |
||
73 | 'Deployer.Name' => 'Deployer' |
||
74 | ); |
||
75 | |||
76 | public function getFiniteState() { |
||
77 | return $this->State; |
||
78 | } |
||
79 | |||
80 | public function setFiniteState($state) { |
||
81 | $this->State = $state; |
||
82 | $this->write(); |
||
83 | } |
||
84 | |||
85 | public function getStatus() { |
||
86 | return $this->State; |
||
87 | } |
||
88 | |||
89 | public function getMachine() { |
||
90 | <<<<<<< Updated upstream |
||
|
|||
91 | return $this->stateMachineFactory->forDNDeployment($this); |
||
92 | ||||||| merged common ancestors |
||
93 | $loader = new Finite\Loader\ArrayLoader([ |
||
94 | 'class' => 'DNDeployment', |
||
95 | 'states' => [ |
||
96 | self::STATE_NEW => ['type' => StateInterface::TYPE_INITIAL], |
||
97 | self::STATE_SUBMITTED => ['type' => StateInterface::TYPE_NORMAL], |
||
98 | self::STATE_INVALID => ['type' => StateInterface::TYPE_NORMAL], |
||
99 | self::STATE_QUEUED => ['type' => StateInterface::TYPE_NORMAL], |
||
100 | self::STATE_DEPLOYING => ['type' => StateInterface::TYPE_NORMAL], |
||
101 | self::STATE_ABORTING => ['type' => StateInterface::TYPE_NORMAL], |
||
102 | self::STATE_COMPLETED => ['type' => StateInterface::TYPE_FINAL], |
||
103 | self::STATE_FAILED => ['type' => StateInterface::TYPE_FINAL], |
||
104 | ], |
||
105 | 'transitions' => [ |
||
106 | self::TR_SUBMIT => ['from' => [self::STATE_NEW], 'to' => self::STATE_SUBMITTED], |
||
107 | self::TR_QUEUE => ['from' => [self::STATE_SUBMITTED], 'to' => self::STATE_QUEUED], |
||
108 | self::TR_INVALIDATE => [ |
||
109 | 'from' => [self::STATE_NEW, self::STATE_SUBMITTED], |
||
110 | 'to' => self::STATE_INVALID |
||
111 | ], |
||
112 | self::TR_DEPLOY => ['from' => [self::STATE_QUEUED], 'to' => self::STATE_DEPLOYING], |
||
113 | self::TR_ABORT => [ |
||
114 | 'from' => [ |
||
115 | self::STATE_QUEUED, |
||
116 | self::STATE_DEPLOYING, |
||
117 | self::STATE_ABORTING |
||
118 | ], |
||
119 | 'to' => self::STATE_ABORTING |
||
120 | ], |
||
121 | self::TR_COMPLETE => ['from' => [self::STATE_DEPLOYING], 'to' => self::STATE_COMPLETED], |
||
122 | self::TR_FAIL => [ |
||
123 | 'from' => [ |
||
124 | self::STATE_NEW, |
||
125 | self::STATE_SUBMITTED, |
||
126 | self::STATE_QUEUED, |
||
127 | self::STATE_INVALID, |
||
128 | self::STATE_DEPLOYING, |
||
129 | self::STATE_ABORTING |
||
130 | ], |
||
131 | 'to' => self::STATE_FAILED |
||
132 | ], |
||
133 | ], |
||
134 | 'callbacks' => [ |
||
135 | 'after' => [ |
||
136 | ['to' => [self::STATE_QUEUED], 'do' => [$this, 'onQueue']], |
||
137 | ['to' => [self::STATE_ABORTING], 'do' => [$this, 'onAbort']], |
||
138 | ] |
||
139 | ] |
||
140 | ]); |
||
141 | $stateMachine = new Finite\StateMachine\StateMachine($this); |
||
142 | $loader->load($stateMachine); |
||
143 | $stateMachine->initialize(); |
||
144 | return $stateMachine; |
||
145 | } |
||
146 | |||
147 | |||
148 | public function onQueue() { |
||
149 | $log = $this->log(); |
||
150 | $token = $this->enqueueDeployment(); |
||
151 | $this->ResqueToken = $token; |
||
152 | $this->write(); |
||
153 | |||
154 | $message = sprintf('Deploy queued as job %s (sigFile is %s)', $token, DeployJob::sig_file_for_data_object($this)); |
||
155 | $log->write($message); |
||
156 | } |
||
157 | |||
158 | public function onAbort() { |
||
159 | // 2 is SIGINT - we can't use SIGINT constant in the mod_apache context. |
||
160 | DeployJob::set_signal($this, 2); |
||
161 | ======= |
||
162 | $notificationService = Injector::inst()->get('DeploymentNotificationService'); |
||
163 | |||
164 | $loader = new Finite\Loader\ArrayLoader([ |
||
165 | 'class' => 'DNDeployment', |
||
166 | 'states' => [ |
||
167 | self::STATE_NEW => ['type' => StateInterface::TYPE_INITIAL], |
||
168 | self::STATE_SUBMITTED => ['type' => StateInterface::TYPE_NORMAL], |
||
169 | self::STATE_INVALID => ['type' => StateInterface::TYPE_NORMAL], |
||
170 | self::STATE_QUEUED => ['type' => StateInterface::TYPE_NORMAL], |
||
171 | self::STATE_DEPLOYING => ['type' => StateInterface::TYPE_NORMAL], |
||
172 | self::STATE_ABORTING => ['type' => StateInterface::TYPE_NORMAL], |
||
173 | self::STATE_COMPLETED => ['type' => StateInterface::TYPE_FINAL], |
||
174 | self::STATE_FAILED => ['type' => StateInterface::TYPE_FINAL], |
||
175 | ], |
||
176 | 'transitions' => [ |
||
177 | self::TR_SUBMIT => ['from' => [self::STATE_NEW], 'to' => self::STATE_SUBMITTED], |
||
178 | self::TR_QUEUE => ['from' => [self::STATE_SUBMITTED], 'to' => self::STATE_QUEUED], |
||
179 | self::TR_INVALIDATE => [ |
||
180 | 'from' => [self::STATE_NEW, self::STATE_SUBMITTED], |
||
181 | 'to' => self::STATE_INVALID |
||
182 | ], |
||
183 | self::TR_DEPLOY => ['from' => [self::STATE_QUEUED], 'to' => self::STATE_DEPLOYING], |
||
184 | self::TR_ABORT => [ |
||
185 | 'from' => [ |
||
186 | self::STATE_QUEUED, |
||
187 | self::STATE_DEPLOYING, |
||
188 | self::STATE_ABORTING |
||
189 | ], |
||
190 | 'to' => self::STATE_ABORTING |
||
191 | ], |
||
192 | self::TR_COMPLETE => ['from' => [self::STATE_DEPLOYING], 'to' => self::STATE_COMPLETED], |
||
193 | self::TR_FAIL => [ |
||
194 | 'from' => [ |
||
195 | self::STATE_NEW, |
||
196 | self::STATE_SUBMITTED, |
||
197 | self::STATE_QUEUED, |
||
198 | self::STATE_INVALID, |
||
199 | self::STATE_DEPLOYING, |
||
200 | self::STATE_ABORTING |
||
201 | ], |
||
202 | 'to' => self::STATE_FAILED |
||
203 | ], |
||
204 | ], |
||
205 | 'callbacks' => [ |
||
206 | 'after' => [ |
||
207 | ['to' => [self::STATE_QUEUED], 'do' => [$this, 'onQueue']], |
||
208 | ['to' => [self::STATE_ABORTING], 'do' => [$this, 'onAbort']], |
||
209 | ['to' => [self::STATE_SUBMITTED], 'do' => [$notificationService, 'onSubmitted']] |
||
210 | ] |
||
211 | ] |
||
212 | ]); |
||
213 | $stateMachine = new Finite\StateMachine\StateMachine($this); |
||
214 | $loader->load($stateMachine); |
||
215 | $stateMachine->initialize(); |
||
216 | return $stateMachine; |
||
217 | } |
||
218 | |||
219 | |||
220 | public function onQueue() { |
||
221 | $log = $this->log(); |
||
222 | $token = $this->enqueueDeployment(); |
||
223 | $this->ResqueToken = $token; |
||
224 | $this->write(); |
||
225 | |||
226 | $message = sprintf('Deploy queued as job %s (sigFile is %s)', $token, DeployJob::sig_file_for_data_object($this)); |
||
227 | $log->write($message); |
||
228 | } |
||
229 | |||
230 | public function onAbort() { |
||
231 | // 2 is SIGINT - we can't use SIGINT constant in the mod_apache context. |
||
232 | DeployJob::set_signal($this, 2); |
||
233 | >>>>>>> Stashed changes |
||
234 | } |
||
235 | |||
236 | public function Link() { |
||
237 | return Controller::join_links($this->Environment()->Link(), 'deploy', $this->ID); |
||
238 | } |
||
239 | |||
240 | public function LogLink() { |
||
241 | return $this->Link() . '/log'; |
||
242 | } |
||
243 | |||
244 | public function canView($member = null) { |
||
245 | return $this->Environment()->canView($member); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Return a path to the log file. |
||
250 | * @return string |
||
251 | */ |
||
252 | protected function logfile() { |
||
253 | return sprintf( |
||
254 | '%s.%s.log', |
||
255 | $this->Environment()->getFullName('.'), |
||
256 | $this->ID |
||
257 | ); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @return DeploynautLogFile |
||
262 | */ |
||
263 | public function log() { |
||
264 | return Injector::inst()->createWithArgs('DeploynautLogFile', array($this->logfile())); |
||
265 | } |
||
266 | |||
267 | public function LogContent() { |
||
268 | return $this->log()->content(); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * This remains here for backwards compatibility - we don't want to expose Resque status in here. |
||
273 | * Resque job (DeployJob) will change statuses as part of its execution. |
||
274 | * |
||
275 | * @return string |
||
276 | */ |
||
277 | public function ResqueStatus() { |
||
278 | return $this->State; |
||
279 | } |
||
280 | |||
281 | |||
282 | /** |
||
283 | * Fetch the git repository |
||
284 | * |
||
285 | * @return \Gitonomy\Git\Repository|null |
||
286 | */ |
||
287 | public function getRepository() { |
||
288 | if(!$this->SHA) { |
||
289 | return null; |
||
290 | } |
||
291 | return $this->Environment()->Project()->getRepository(); |
||
292 | } |
||
293 | |||
294 | |||
295 | /** |
||
296 | * Gets the commit from source. The result is cached upstream in Repository. |
||
297 | * |
||
298 | * @return \Gitonomy\Git\Commit|null |
||
299 | */ |
||
300 | public function getCommit() { |
||
301 | $repo = $this->getRepository(); |
||
302 | if($repo) { |
||
303 | try { |
||
304 | return $repo->getCommit($this->SHA); |
||
305 | } catch(Gitonomy\Git\Exception\ReferenceNotFoundException $ex) { |
||
306 | return null; |
||
307 | } |
||
308 | } |
||
309 | |||
310 | return null; |
||
311 | } |
||
312 | |||
313 | |||
314 | /** |
||
315 | * Gets the commit message. |
||
316 | * |
||
317 | * @return string|null |
||
318 | */ |
||
319 | public function getCommitMessage() { |
||
320 | $commit = $this->getCommit(); |
||
321 | if($commit) { |
||
322 | try { |
||
323 | return Convert::raw2xml($commit->getMessage()); |
||
324 | } catch(Gitonomy\Git\Exception\ReferenceNotFoundException $e) { |
||
325 | return null; |
||
326 | } |
||
327 | } |
||
328 | return null; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Return all tags for the deployed commit. |
||
333 | * |
||
334 | * @return ArrayList |
||
335 | */ |
||
336 | public function getTags() { |
||
337 | $returnTags = array(); |
||
338 | $repo = $this->getRepository(); |
||
339 | if($repo) { |
||
340 | $tags = $repo->getReferences()->resolveTags($this->SHA); |
||
341 | if(!empty($tags)) { |
||
342 | foreach($tags as $tag) { |
||
487 |