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 |
||
4 | class CapistranoDeploymentBackend extends Object implements DeploymentBackend { |
||
5 | |||
6 | protected $packageGenerator; |
||
7 | |||
8 | public function getPackageGenerator() { |
||
11 | |||
12 | public function setPackageGenerator(PackageGenerator $packageGenerator) { |
||
15 | |||
16 | /** |
||
17 | * Create a deployment strategy. |
||
18 | * |
||
19 | * @param DNEnvironment $environment |
||
20 | * @param array $options |
||
21 | * |
||
22 | * @return DeploymentStrategy |
||
23 | */ |
||
24 | public function planDeploy(DNEnvironment $environment, $options) { |
||
38 | |||
39 | /** |
||
40 | * Deploy the given build to the given environment. |
||
41 | * |
||
42 | * @param DNEnvironment $environment |
||
43 | * @param DeploynautLogFile $log |
||
44 | * @param DNProject $project |
||
45 | * @param array $options |
||
46 | */ |
||
47 | public function deploy( |
||
48 | DNEnvironment $environment, |
||
49 | DeploynautLogFile $log, |
||
50 | DNProject $project, |
||
51 | $options |
||
52 | ) { |
||
53 | $name = $environment->getFullName(); |
||
54 | $repository = $project->getLocalCVSPath(); |
||
55 | $sha = $options['sha']; |
||
56 | |||
57 | $args = array( |
||
58 | 'branch' => $sha, |
||
59 | 'repository' => $repository, |
||
60 | ); |
||
61 | |||
62 | $this->extend('deployStart', $environment, $sha, $log, $project); |
||
63 | |||
64 | $log->write(sprintf('Deploying "%s" to "%s"', $sha, $name)); |
||
65 | |||
66 | $this->enableMaintenance($environment, $log, $project); |
||
67 | |||
68 | // Use a package generator if specified, otherwise run a direct deploy, which is the default behaviour |
||
69 | // if build_filename isn't specified |
||
70 | if($this->packageGenerator) { |
||
71 | $log->write(sprintf('Using package generator "%s"', get_class($this->packageGenerator))); |
||
72 | |||
73 | try { |
||
74 | $args['build_filename'] = $this->packageGenerator->getPackageFilename($project->Name, $sha, $repository, $log); |
||
75 | } catch (Exception $e) { |
||
76 | $log->write($e->getMessage()); |
||
77 | throw $e; |
||
78 | } |
||
79 | |||
80 | if(empty($args['build_filename'])) { |
||
81 | throw new RuntimeException('Failed to generate package.'); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | $command = $this->getCommand('deploy', 'web', $environment, $args, $log); |
||
86 | $command->run(function($type, $buffer) use($log) { |
||
87 | $log->write($buffer); |
||
88 | }); |
||
89 | |||
90 | // Deployment cleanup. We assume it is always safe to run this at the end, regardless of the outcome. |
||
91 | $self = $this; |
||
92 | $cleanupFn = function() use($self, $environment, $args, $log) { |
||
93 | $command = $self->getCommand('deploy:cleanup', 'web', $environment, $args, $log); |
||
94 | $command->run(function($type, $buffer) use($log) { |
||
95 | $log->write($buffer); |
||
96 | }); |
||
97 | |||
98 | if(!$command->isSuccessful()) { |
||
99 | $this->extend('cleanupFailure', $environment, $sha, $log, $project); |
||
100 | $log->write('Warning: Cleanup failed, but fine to continue. Needs manual cleanup sometime.'); |
||
101 | } |
||
102 | }; |
||
103 | |||
104 | // Once the deployment has run it's necessary to update the maintenance page status |
||
105 | if(!empty($options['leaveMaintenancePage'])) { |
||
106 | $this->enableMaintenance($environment, $log, $project); |
||
107 | } |
||
108 | |||
109 | if(!$command->isSuccessful()) { |
||
110 | $cleanupFn(); |
||
111 | $this->extend('deployFailure', $environment, $sha, $log, $project); |
||
112 | throw new RuntimeException($command->getErrorOutput()); |
||
113 | } |
||
114 | |||
115 | // Check if maintenance page should be removed |
||
116 | if(empty($options['leaveMaintenancePage'])) { |
||
117 | $this->disableMaintenance($environment, $log, $project); |
||
118 | } |
||
119 | |||
120 | $cleanupFn(); |
||
121 | |||
122 | $log->write(sprintf('Deploy of "%s" to "%s" finished', $sha, $name)); |
||
123 | |||
124 | $this->extend('deployEnd', $environment, $sha, $log, $project); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Enable a maintenance page for the given environment using the maintenance:enable Capistrano task. |
||
129 | */ |
||
130 | View Code Duplication | public function enableMaintenance(DNEnvironment $environment, DeploynautLogFile $log, DNProject $project) { |
|
142 | |||
143 | /** |
||
144 | * Disable the maintenance page for the given environment using the maintenance:disable Capistrano task. |
||
145 | */ |
||
146 | View Code Duplication | public function disableMaintenance(DNEnvironment $environment, DeploynautLogFile $log, DNProject $project) { |
|
158 | |||
159 | /** |
||
160 | * Check the status using the deploy:check capistrano method |
||
161 | */ |
||
162 | public function ping(DNEnvironment $environment, DeploynautLogFile $log, DNProject $project) { |
||
169 | |||
170 | /** |
||
171 | * @inheritdoc |
||
172 | */ |
||
173 | public function dataTransfer(DNDataTransfer $dataTransfer, DeploynautLogFile $log) { |
||
208 | |||
209 | /** |
||
210 | * @param string $action Capistrano action to be executed |
||
211 | * @param string $roles Defining a server role is required to target only the required servers. |
||
212 | * @param DNEnvironment $environment |
||
213 | * @param array<string>|null $args Additional arguments for process |
||
214 | * @param DeploynautLogFile $log |
||
215 | * @return \Symfony\Component\Process\Process |
||
216 | */ |
||
217 | public function getCommand($action, $roles, DNEnvironment $environment, $args = null, DeploynautLogFile $log) { |
||
263 | |||
264 | /** |
||
265 | * Backs up database and/or assets to a designated folder, |
||
266 | * and packs up the files into a single sspak. |
||
267 | * |
||
268 | * @param DNDataTransfer $dataTransfer |
||
269 | * @param DeploynautLogFile $log |
||
270 | */ |
||
271 | protected function dataTransferBackup(DNDataTransfer $dataTransfer, DeploynautLogFile $log) { |
||
341 | |||
342 | /** |
||
343 | * Utility function for triggering the db rebuild and flush. |
||
344 | * Also cleans up and generates new error pages. |
||
345 | * @param DeploynautLogFile $log |
||
346 | */ |
||
347 | View Code Duplication | public function rebuild(DNEnvironment $environment, $log) { |
|
359 | |||
360 | /** |
||
361 | * Extracts a *.sspak file referenced through the passed in $dataTransfer |
||
362 | * and pushes it to the environment referenced in $dataTransfer. |
||
363 | * |
||
364 | * @param string $workingDir Directory for the unpacked files. |
||
365 | * @param DNDataTransfer $dataTransfer |
||
366 | * @param DeploynautLogFile $log |
||
367 | */ |
||
368 | protected function dataTransferRestore($workingDir, DNDataTransfer $dataTransfer, DeploynautLogFile $log) { |
||
418 | |||
419 | } |
||
420 |
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.