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 | // The branch that was used to deploy this. Can't really be inferred from Git history because |
||
24 | // the commit could appear in lots of branches that are irrelevant to the user when it comes |
||
25 | // to deployment history, and the branch may have been deleted. |
||
26 | "Branch" => "Varchar(255)", |
||
27 | // Observe that this is not the same as Resque status, since ResqueStatus is not persistent |
||
28 | // It's used for finding successful deployments and displaying that in history views in the frontend |
||
29 | "Status" => "Enum('Queued, Started, Finished, Failed, n/a', 'n/a')", |
||
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 | /** |
||
56 | * @param int $int |
||
57 | * @return string |
||
58 | */ |
||
59 | public static function map_resque_status($int) { |
||
60 | $remap = array( |
||
61 | Resque_Job_Status::STATUS_WAITING => "Queued", |
||
62 | Resque_Job_Status::STATUS_RUNNING => "Running", |
||
63 | Resque_Job_Status::STATUS_FAILED => "Failed", |
||
64 | Resque_Job_Status::STATUS_COMPLETE => "Complete", |
||
65 | false => "Invalid", |
||
66 | ); |
||
67 | return $remap[$int]; |
||
68 | } |
||
69 | |||
70 | |||
71 | public function Link() { |
||
74 | |||
75 | public function LogLink() { |
||
78 | |||
79 | public function canView($member = null) { |
||
82 | |||
83 | /** |
||
84 | * Return a path to the log file. |
||
85 | * @return string |
||
86 | */ |
||
87 | protected function logfile() { |
||
94 | |||
95 | /** |
||
96 | * @return DeploynautLogFile |
||
97 | */ |
||
98 | public function log() { |
||
101 | |||
102 | public function LogContent() { |
||
105 | |||
106 | /** |
||
107 | * Returns the status of the resque job |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | View Code Duplication | public function ResqueStatus() { |
|
128 | |||
129 | |||
130 | /** |
||
131 | * Fetch the git repository |
||
132 | * |
||
133 | * @return \Gitonomy\Git\Repository|null |
||
134 | */ |
||
135 | public function getRepository() { |
||
141 | |||
142 | |||
143 | /** |
||
144 | * Gets the commit from source. The result is cached upstream in Repository. |
||
145 | * |
||
146 | * @return \Gitonomy\Git\Commit|null |
||
147 | */ |
||
148 | public function getCommit() { |
||
160 | |||
161 | |||
162 | /** |
||
163 | * Gets the commit message. |
||
164 | * |
||
165 | * @return string|null |
||
166 | */ |
||
167 | public function getCommitMessage() { |
||
178 | |||
179 | /** |
||
180 | * Return all tags for the deployed commit. |
||
181 | * |
||
182 | * @return ArrayList |
||
183 | */ |
||
184 | public function getTags() { |
||
199 | |||
200 | /** |
||
201 | * Collate the list of additional flags to affix to this deployment. |
||
202 | * Elements of the array will be rendered literally. |
||
203 | * |
||
204 | * @return ArrayList |
||
205 | */ |
||
206 | public function getFullDeployMessages() { |
||
233 | |||
234 | /** |
||
235 | * Fetches the latest tag for the deployed commit |
||
236 | * |
||
237 | * @return \Varchar|null |
||
238 | */ |
||
239 | public function getTag() { |
||
246 | |||
247 | /** |
||
248 | * @return DeploymentStrategy |
||
249 | */ |
||
250 | public function getDeploymentStrategy() { |
||
256 | |||
257 | /** |
||
258 | * Return a list of things that are going to be deployed, such |
||
259 | * as the code version, and any infrastrucutral changes. |
||
260 | * |
||
261 | * @return ArrayList |
||
262 | */ |
||
263 | public function getChanges() { |
||
289 | |||
290 | /** |
||
291 | * Start a resque job for this deployment |
||
292 | * |
||
293 | * @return string Resque token |
||
294 | */ |
||
295 | protected function enqueueDeployment() { |
||
333 | |||
334 | View Code Duplication | public function start() { |
|
344 | } |
||
345 |
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.