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 CapistranoDeploymentBackend 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 CapistranoDeploymentBackend, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class CapistranoDeploymentBackend extends Object implements DeploymentBackend { |
||
4 | |||
5 | protected $packageGenerator; |
||
6 | |||
7 | public function getPackageGenerator() { |
||
10 | |||
11 | public function setPackageGenerator(PackageGenerator $packageGenerator) { |
||
14 | |||
15 | /** |
||
16 | * Create a deployment strategy. |
||
17 | * |
||
18 | * @param \DNEnvironment $environment |
||
19 | * @param array $options |
||
20 | * |
||
21 | * @return DeploymentStrategy |
||
22 | */ |
||
23 | public function planDeploy(\DNEnvironment $environment, $options) { |
||
37 | |||
38 | /** |
||
39 | * Deploy the given build to the given environment. |
||
40 | * |
||
41 | * @param \DNEnvironment $environment |
||
42 | * @param \DeploynautLogFile $log |
||
43 | * @param \DNProject $project |
||
44 | * @param array $options |
||
45 | */ |
||
46 | public function deploy( |
||
47 | \DNEnvironment $environment, |
||
48 | \DeploynautLogFile $log, |
||
49 | \DNProject $project, |
||
50 | $options |
||
51 | ) { |
||
52 | $name = $environment->getFullName(); |
||
53 | $repository = $project->getLocalCVSPath(); |
||
54 | $sha = $options['sha']; |
||
55 | |||
56 | $args = array( |
||
57 | 'branch' => $sha, |
||
58 | 'repository' => $repository, |
||
59 | ); |
||
60 | |||
61 | $this->extend('deployStart', $environment, $sha, $log, $project); |
||
62 | |||
63 | $log->write(sprintf('Deploying "%s" to "%s"', $sha, $name)); |
||
64 | |||
65 | $this->enableMaintenance($environment, $log, $project); |
||
66 | |||
67 | // Use a package generator if specified, otherwise run a direct deploy, which is the default behaviour |
||
68 | // if build_filename isn't specified |
||
69 | if($this->packageGenerator) { |
||
70 | $log->write(sprintf('Using package generator "%s"', get_class($this->packageGenerator))); |
||
71 | |||
72 | try { |
||
73 | $args['build_filename'] = $this->packageGenerator->getPackageFilename($project->Name, $sha, $repository, $log); |
||
74 | } catch (Exception $e) { |
||
75 | $log->write($e->getMessage()); |
||
76 | throw $e; |
||
77 | } |
||
78 | |||
79 | if(empty($args['build_filename'])) { |
||
80 | throw new RuntimeException('Failed to generate package.'); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | $command = $this->getCommand('deploy', 'web', $environment, $args, $log); |
||
85 | $command->run(function($type, $buffer) use($log) { |
||
86 | $log->write($buffer); |
||
87 | }); |
||
88 | |||
89 | $error = null; |
||
90 | |||
91 | $deploySuccessful = $command->isSuccessful(); |
||
92 | if ($deploySuccessful) { |
||
93 | // Deployment automatically removes .htaccess, i.e. disables maintenance. Fine to smoketest. |
||
94 | $deploySuccessful = $this->smokeTest($environment, $log); |
||
95 | } |
||
96 | |||
97 | if (!$deploySuccessful) { |
||
98 | $this->enableMaintenance($environment, $log, $project); |
||
99 | |||
100 | $rollbackSuccessful = $this->deployRollback($environment, $log, $project, $options, $args); |
||
101 | if ($rollbackSuccessful) { |
||
102 | // Again, .htaccess removed, maintenance off. |
||
103 | $rollbackSuccessful = $this->smokeTest($environment, $log); |
||
104 | } |
||
105 | |||
106 | if (!$rollbackSuccessful) { |
||
107 | $this->enableMaintenance($environment, $log, $project); |
||
108 | $currentBuild = $environment->CurrentBuild(); |
||
109 | $this->extend('deployRollbackFailure', $environment, $currentBuild, $log, $project); |
||
110 | $log->write('Rollback failed'); |
||
111 | |||
112 | $error = $command->getErrorOutput(); |
||
113 | } else { |
||
114 | $error = 'Rollback successful'; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | // Regardless of outcome, try to clean up. |
||
119 | $cleanup = $this->getCommand('deploy:cleanup', 'web', $environment, $args, $log); |
||
120 | $cleanup->run(function($type, $buffer) use($log) { |
||
121 | $log->write($buffer); |
||
122 | }); |
||
123 | if(!$cleanup->isSuccessful()) { |
||
124 | $this->extend('cleanupFailure', $environment, $sha, $log, $project); |
||
125 | $log->write('Warning: Cleanup failed, but fine to continue. Needs manual cleanup sometime.'); |
||
126 | } |
||
127 | |||
128 | $this->extend('deployEnd', $environment, $sha, $log, $project); |
||
129 | |||
130 | if ($error!==null) { |
||
131 | throw new RuntimeException($error); |
||
132 | } |
||
133 | |||
134 | $log->write(sprintf('Deploy of "%s" to "%s" finished', $sha, $name)); |
||
135 | } |
||
136 | |||
137 | private function deployRollback( |
||
138 | \DNEnvironment $environment, |
||
139 | \DeploynautLogFile $log, |
||
140 | \DNProject $project, |
||
141 | $options, |
||
142 | $args |
||
143 | ) { |
||
144 | $sha = $options['sha']; |
||
145 | |||
146 | $this->extend('deployFailure', $environment, $sha, $log, $project); |
||
147 | |||
148 | $currentBuild = $environment->CurrentBuild(); |
||
149 | if (empty($currentBuild) || (!empty($options['no_rollback']) && $options['no_rollback'] !== 'false')) { |
||
150 | throw new RuntimeException($command->getErrorOutput()); |
||
151 | } |
||
152 | |||
153 | // re-run deploy with the current build sha to rollback |
||
154 | $log->write('Deploy failed. Rolling back'); |
||
155 | $rollbackArgs = array_merge($args, ['branch' => $currentBuild->SHA]); |
||
156 | $command = $this->getCommand('deploy', 'web', $environment, $rollbackArgs, $log); |
||
157 | $command->run(function($type, $buffer) use($log) { |
||
158 | $log->write($buffer); |
||
159 | }); |
||
160 | |||
161 | return $command->isSuccessful(); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param \DNEnvironment $environment |
||
166 | * @return ArrayList |
||
167 | */ |
||
168 | public function getDeployOptions(\DNEnvironment $environment) { |
||
174 | |||
175 | /** |
||
176 | * Enable a maintenance page for the given environment using the maintenance:enable Capistrano task. |
||
177 | */ |
||
178 | View Code Duplication | public function enableMaintenance(\DNEnvironment $environment, \DeploynautLogFile $log, \DNProject $project) { |
|
190 | |||
191 | /** |
||
192 | * Disable the maintenance page for the given environment using the maintenance:disable Capistrano task. |
||
193 | */ |
||
194 | View Code Duplication | public function disableMaintenance(\DNEnvironment $environment, \DeploynautLogFile $log, \DNProject $project) { |
|
206 | |||
207 | /** |
||
208 | * Check the status using the deploy:check capistrano method |
||
209 | */ |
||
210 | public function ping(\DNEnvironment $environment, \DeploynautLogFile $log, \DNProject $project) { |
||
217 | |||
218 | /** |
||
219 | * @inheritdoc |
||
220 | */ |
||
221 | public function dataTransfer(\DNDataTransfer $dataTransfer, \DeploynautLogFile $log) { |
||
257 | |||
258 | /** |
||
259 | * @param string $action Capistrano action to be executed |
||
260 | * @param string $roles Defining a server role is required to target only the required servers. |
||
261 | * @param \DNEnvironment $environment |
||
262 | * @param array<string>|null $args Additional arguments for process |
||
263 | * @param \DeploynautLogFile $log |
||
264 | * @return \Symfony\Component\Process\Process |
||
265 | */ |
||
266 | public function getCommand($action, $roles, \DNEnvironment $environment, $args = null, \DeploynautLogFile $log) { |
||
313 | |||
314 | /** |
||
315 | * Backs up database and/or assets to a designated folder, |
||
316 | * and packs up the files into a single sspak. |
||
317 | * |
||
318 | * @param \DNDataTransfer $dataTransfer |
||
319 | * @param DeploynautLogFile $log |
||
320 | */ |
||
321 | protected function dataTransferBackup(\DNDataTransfer $dataTransfer, \DeploynautLogFile $log) { |
||
399 | |||
400 | /** |
||
401 | * Utility function for triggering the db rebuild and flush. |
||
402 | * Also cleans up and generates new error pages. |
||
403 | * @param DeploynautLogFile $log |
||
404 | */ |
||
405 | View Code Duplication | public function rebuild(\DNEnvironment $environment, $log) { |
|
417 | |||
418 | /** |
||
419 | * Extracts a *.sspak file referenced through the passed in $dataTransfer |
||
420 | * and pushes it to the environment referenced in $dataTransfer. |
||
421 | * |
||
422 | * @param string $workingDir Directory for the unpacked files. |
||
423 | * @param DNDataTransfer $dataTransfer |
||
424 | * @param DeploynautLogFile $log |
||
425 | */ |
||
426 | protected function dataTransferRestore($workingDir, \DNDataTransfer $dataTransfer, \DeploynautLogFile $log) { |
||
477 | |||
478 | /** |
||
479 | * This is mostly copy-pasted from Anthill/Smoketest. |
||
480 | * |
||
481 | * @param \DNEnvironment $environment |
||
482 | * @param \DeploynautLogFile $log |
||
483 | * @return bool |
||
484 | */ |
||
485 | protected function smokeTest(\DNEnvironment $environment, \DeploynautLogFile $log) { |
||
570 | |||
571 | } |
||
572 |
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.