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 |
||
8 | class DockerMachineCli implements Machine |
||
9 | { |
||
10 | const DEFAULT_MACHINE_NAME = 'inviqa-dock-cli'; |
||
11 | |||
12 | /** |
||
13 | * @var ProcessRunner |
||
14 | */ |
||
15 | private $processRunner; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $name; |
||
21 | |||
22 | /** |
||
23 | * @param ProcessRunner $processRunner |
||
24 | * @param string $name |
||
25 | */ |
||
26 | public function __construct(ProcessRunner $processRunner, $name) |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | View Code Duplication | public function isRunning() |
|
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | View Code Duplication | public function start() |
|
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | View Code Duplication | public function stop() |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | public function getIp() |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | View Code Duplication | public function isCreated() |
|
92 | { |
||
93 | $process = $this->processRunner->run('docker-machine status '.$this->name, false); |
||
94 | if(!$process->isSuccessful() || strpos($process->getOutput(), 'Error') === 0) { |
||
95 | return false; |
||
96 | } |
||
97 | |||
98 | return true; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | View Code Duplication | public function create() |
|
112 | |||
113 | /** |
||
114 | * @return string |
||
115 | */ |
||
116 | public function getEnvironmentDeclaration() |
||
120 | } |
||
121 |
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.