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 { |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | private static $db = array( |
||
21 | "SHA" => "GitSHA", |
||
22 | "ResqueToken" => "Varchar(255)", |
||
23 | // Observe that this is not the same as Resque status, since ResqueStatus is not persistent |
||
24 | // It's used for finding successful deployments and displaying that in history views in the frontend |
||
25 | "Status" => "Enum('Queued, Started, Finished, Failed, n/a', 'n/a')", |
||
26 | // JSON serialised DeploymentStrategy. |
||
27 | "Strategy" => "Text" |
||
28 | ); |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private static $has_one = array( |
||
34 | "Environment" => "DNEnvironment", |
||
35 | "Deployer" => "Member", |
||
36 | ); |
||
37 | |||
38 | private static $default_sort = '"LastEdited" DESC'; |
||
39 | |||
40 | public function getTitle() { |
||
43 | |||
44 | private static $summary_fields = array( |
||
45 | 'LastEdited' => 'Last Edited', |
||
46 | 'SHA' => 'SHA', |
||
47 | 'Status' => 'Status', |
||
48 | 'Deployer.Name' => 'Deployer' |
||
49 | ); |
||
50 | |||
51 | /** |
||
52 | * @param int $int |
||
53 | * @return string |
||
54 | */ |
||
55 | View Code Duplication | public static function map_resque_status($int) { |
|
|
|||
56 | $remap = array( |
||
57 | Resque_Job_Status::STATUS_WAITING => "Queued", |
||
58 | Resque_Job_Status::STATUS_RUNNING => "Running", |
||
59 | Resque_Job_Status::STATUS_FAILED => "Failed", |
||
60 | Resque_Job_Status::STATUS_COMPLETE => "Complete", |
||
61 | false => "Invalid", |
||
62 | ); |
||
63 | return $remap[$int]; |
||
64 | } |
||
65 | |||
66 | |||
67 | public function Link() { |
||
70 | |||
71 | public function LogLink() { |
||
74 | |||
75 | public function canView($member = null) { |
||
78 | |||
79 | /** |
||
80 | * Return a path to the log file. |
||
81 | * @return string |
||
82 | */ |
||
83 | protected function logfile() { |
||
84 | return sprintf( |
||
85 | '%s.%s.log', |
||
86 | $this->Environment()->getFullName('.'), |
||
87 | $this->ID |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return DeploynautLogFile |
||
93 | */ |
||
94 | public function log() { |
||
97 | |||
98 | public function LogContent() { |
||
101 | |||
102 | /** |
||
103 | * Returns the status of the resque job |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | View Code Duplication | public function ResqueStatus() { |
|
124 | |||
125 | |||
126 | /** |
||
127 | * Fetch the git repository |
||
128 | * |
||
129 | * @return \Gitonomy\Git\Repository|null |
||
130 | */ |
||
131 | public function getRepository() { |
||
132 | if(!$this->SHA) { |
||
133 | return null; |
||
134 | } |
||
135 | return $this->Environment()->Project()->getRepository(); |
||
136 | } |
||
137 | |||
138 | |||
139 | /** |
||
140 | * Gets the commit from source. The result is cached upstream in Repository. |
||
141 | * |
||
142 | * @return \Gitonomy\Git\Commit|null |
||
143 | */ |
||
144 | public function getCommit() { |
||
145 | $repo = $this->getRepository(); |
||
146 | if($repo) { |
||
147 | try { |
||
148 | return $repo->getCommit($this->SHA); |
||
149 | } catch(Gitonomy\Git\Exception\ReferenceNotFoundException $ex) { |
||
150 | return null; |
||
151 | } |
||
152 | } |
||
153 | |||
154 | return null; |
||
155 | } |
||
156 | |||
157 | |||
158 | /** |
||
159 | * Gets the commit message. |
||
160 | * |
||
161 | * @return string|null |
||
162 | */ |
||
163 | public function getCommitMessage() { |
||
164 | $commit = $this->getCommit(); |
||
165 | if($commit) { |
||
166 | try { |
||
167 | return Convert::raw2xml($commit->getMessage()); |
||
168 | } catch(Gitonomy\Git\Exception\ReferenceNotFoundException $e) { |
||
169 | return null; |
||
170 | } |
||
171 | } |
||
172 | return null; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Return all tags for the deployed commit. |
||
177 | * |
||
178 | * @return ArrayList |
||
179 | */ |
||
180 | public function getTags() { |
||
195 | |||
196 | /** |
||
197 | * Collate the list of additional flags to affix to this deployment. |
||
198 | * Elements of the array will be rendered literally. |
||
199 | * |
||
200 | * @return ArrayList |
||
201 | */ |
||
202 | public function getFullDeployMessages() { |
||
229 | |||
230 | /** |
||
231 | * Fetches the latest tag for the deployed commit |
||
232 | * |
||
233 | * @return \Varchar|null |
||
234 | */ |
||
235 | public function getTag() { |
||
242 | |||
243 | /** |
||
244 | * @return DeploymentStrategy |
||
245 | */ |
||
246 | public function getDeploymentStrategy() { |
||
252 | |||
253 | /** |
||
254 | * Return a list of things that are going to be deployed, such |
||
255 | * as the code version, and any infrastrucutral changes. |
||
256 | * |
||
257 | * @return ArrayList |
||
258 | */ |
||
259 | public function getChanges() { |
||
260 | $list = new ArrayList(); |
||
261 | $strategy = $this->getDeploymentStrategy(); |
||
262 | foreach($strategy->getChanges() as $name => $change) { |
||
263 | if(empty($change['to'])) continue; |
||
264 | |||
265 | $list->push(new ArrayData([ |
||
266 | 'Name' => $name, |
||
267 | 'From' => $change['from'], |
||
268 | 'To' => $change['to'], |
||
269 | 'Description' => isset($change['description']) ? $change['description'] : '', |
||
270 | 'Changed' => $change['from'] != $change['to'], |
||
271 | 'CompareUrl' => isset($change['compareUrl']) ? $change['compareUrl'] : '' |
||
272 | ])); |
||
273 | } |
||
274 | |||
275 | return $list; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Start a resque job for this deployment |
||
280 | * |
||
281 | * @return string Resque token |
||
282 | */ |
||
283 | protected function enqueueDeployment() { |
||
321 | |||
322 | View Code Duplication | public function start() { |
|
332 | } |
||
333 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.