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:
1 | <?php |
||
16 | class ComposeManager |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * Start service containers |
||
21 | * |
||
22 | * @param mixed $composeFiles The compose files names |
||
23 | */ |
||
24 | public function start($composeFiles = array()) |
||
32 | |||
33 | /** |
||
34 | * Stop service containers |
||
35 | * |
||
36 | * @param mixed $composeFiles The compose files names |
||
37 | */ |
||
38 | public function stop($composeFiles = array()) |
||
46 | |||
47 | /** |
||
48 | * Stop service containers |
||
49 | * |
||
50 | * @param mixed $composeFiles The compose files names |
||
51 | * @param boolean $force If the remove need to be force (default=false) |
||
52 | * @param boolean $removeVolumes If we need to remove the volumes (default=false) |
||
53 | */ |
||
54 | public function remove($composeFiles = array(), $force = false, $removeVolumes = false) |
||
71 | |||
72 | /** |
||
73 | * Stop service containers |
||
74 | * |
||
75 | * @param mixed $composeFiles The compose files names |
||
76 | * @param string $signal Optionnal to precise SIGNAL to send to the container for SIGKILL replacement. |
||
77 | */ |
||
78 | View Code Duplication | public function kill($composeFiles = array(), $signal = 'SIGKILL') |
|
92 | |||
93 | /** |
||
94 | * Build service images |
||
95 | * |
||
96 | * @param mixed $composeFiles The compose files names |
||
97 | * @param boolean $pull If we want attempt to pull a newer version of the from image |
||
98 | * @param boolean $forceRemove If we want remove the intermediate containers |
||
99 | * @param bollean $cache If we can use the cache when building the image |
||
100 | */ |
||
101 | public function build($composeFiles = array(), $pull = true, $forceRemove = false, $cache = true) |
||
123 | |||
124 | /** |
||
125 | * Build service images |
||
126 | * |
||
127 | * @param mixed $composeFiles The compose files names |
||
128 | */ |
||
129 | public function pull($composeFiles = array()) |
||
139 | |||
140 | |||
141 | /** |
||
142 | * Restart running containers |
||
143 | * |
||
144 | * @param mixed $composeFiles The compose files names |
||
145 | * @param integer $timeout If we want attempt to pull a newer version of the from image |
||
146 | */ |
||
147 | View Code Duplication | public function restart($composeFiles = array(), $timeout = 10) |
|
161 | |||
162 | /** |
||
163 | * Run service with command |
||
164 | * |
||
165 | * @param string $service Service name |
||
166 | * @param string $command Command to pass to service |
||
167 | * @param mixed $composeFiles The compose files names |
||
168 | */ |
||
169 | public function run($service, $command, $composeFiles = array()) |
||
182 | |||
183 | /** |
||
184 | * List containers |
||
185 | * |
||
186 | * @param mixed $composeFiles The compose files names |
||
187 | */ |
||
188 | public function ps($composeFiles = array()) |
||
196 | |||
197 | /** |
||
198 | * List IP containers |
||
199 | * |
||
200 | * @param mixed $composeFiles The compose files names |
||
201 | */ |
||
202 | View Code Duplication | public function ips($composeFiles = array()) |
|
212 | |||
213 | /** |
||
214 | * Process result with returned code and output |
||
215 | * |
||
216 | * @param array $result The result of command with output and returnCode |
||
217 | * |
||
218 | * @throws DockerInstallationMissingException When returned code is 127 |
||
219 | * @throws ComposeFileNotFoundException When no compose file precise and docker-compose.yml not found |
||
220 | * @throws DockerHostConnexionErrorException When we can't connect to docker host |
||
221 | * @throws \Exception When an unknown error is returned |
||
222 | */ |
||
223 | private function processResult($result) |
||
243 | |||
244 | /** |
||
245 | * Create the composeFileCollection from the type of value given |
||
246 | * |
||
247 | * @param mixed $composeFiles The docker-compose files (can be array, string or ComposeFile) |
||
248 | * |
||
249 | * @return ComposeFileCollection |
||
250 | */ |
||
251 | private function createComposeFileCollection($composeFiles) |
||
263 | |||
264 | /** |
||
265 | * Format the command to execute |
||
266 | * |
||
267 | * @param string $subcommand The subcommand to pass to docker-compose command |
||
268 | * @param ComposeFileCollection $composeFiles The compose files to precise in the command |
||
269 | */ |
||
270 | private function formatCommand($subcommand, ComposeFileCollection $composeFiles) |
||
297 | |||
298 | /** |
||
299 | * Execute docker-compose commande |
||
300 | * @codeCoverageIgnore |
||
301 | * @param string $command The command to execute |
||
302 | */ |
||
303 | protected function execute($command) |
||
318 | } |
||
319 |
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.