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 |
||
20 | class TextWrapper |
||
21 | { |
||
22 | /** |
||
23 | * @var \Phinx\Console\PhinxApplication |
||
24 | */ |
||
25 | protected $app; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $options; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $exitCode; |
||
36 | |||
37 | /** |
||
38 | * @param \Phinx\Console\PhinxApplication $app Application |
||
39 | * @param array $options Options |
||
40 | */ |
||
41 | public function __construct(PhinxApplication $app, array $options = []) |
||
46 | |||
47 | /** |
||
48 | * Get the application instance. |
||
49 | * |
||
50 | * @return \Phinx\Console\PhinxApplication |
||
51 | */ |
||
52 | public function getApp() |
||
56 | |||
57 | /** |
||
58 | * Returns the exit code from the last run command. |
||
59 | * |
||
60 | * @return int |
||
61 | */ |
||
62 | public function getExitCode() |
||
66 | |||
67 | /** |
||
68 | * Returns the output from running the "status" command. |
||
69 | * |
||
70 | * @param string|null $env environment name (optional) |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | public function getStatus($env = null) |
||
92 | |||
93 | /** |
||
94 | * Returns the output from running the "migrate" command. |
||
95 | * |
||
96 | * @param string|null $env environment name (optional) |
||
97 | * @param string|null $target target version (optional) |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | View Code Duplication | public function getMigrate($env = null, $target = null) |
|
119 | |||
120 | /** |
||
121 | * Returns the output from running the "seed:run" command. |
||
122 | * |
||
123 | * @param string|null $env Environment name |
||
124 | * @param string|null $target Target version |
||
125 | * @param string[]|string|null $seed Array of seed names or seed name |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | public function getSeed($env = null, $target = null, $seed = null) |
||
130 | { |
||
131 | $command = ['seed:run']; |
||
132 | if ($env ?: $this->hasOption('environment')) { |
||
133 | $command += ['-e' => $env ?: $this->getOption('environment')]; |
||
134 | } |
||
135 | if ($this->hasOption('configuration')) { |
||
136 | $command += ['-c' => $this->getOption('configuration')]; |
||
137 | } |
||
138 | if ($this->hasOption('parser')) { |
||
139 | $command += ['-p' => $this->getOption('parser')]; |
||
140 | } |
||
141 | if ($target) { |
||
142 | $command += ['-t' => $target]; |
||
143 | } |
||
144 | if ($seed) { |
||
145 | $seed = (array)$seed; |
||
146 | $command += ['-s' => $seed]; |
||
147 | } |
||
148 | |||
149 | return $this->executeRun($command); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Returns the output from running the "rollback" command. |
||
154 | * |
||
155 | * @param string|null $env Environment name (optional) |
||
156 | * @param mixed $target Target version, or 0 (zero) fully revert (optional) |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | View Code Duplication | public function getRollback($env = null, $target = null) |
|
180 | |||
181 | /** |
||
182 | * Check option from options array |
||
183 | * |
||
184 | * @param string $key Key |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | protected function hasOption($key) |
||
192 | |||
193 | /** |
||
194 | * Get option from options array |
||
195 | * |
||
196 | * @param string $key Key |
||
197 | * |
||
198 | * @return string|null |
||
199 | */ |
||
200 | protected function getOption($key) |
||
208 | |||
209 | /** |
||
210 | * Set option in options array |
||
211 | * |
||
212 | * @param string $key Key |
||
213 | * @param string $value Value |
||
214 | * |
||
215 | * @return $this |
||
216 | */ |
||
217 | public function setOption($key, $value) |
||
223 | |||
224 | /** |
||
225 | * Execute a command, capturing output and storing the exit code. |
||
226 | * |
||
227 | * @param array $command Command |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | protected function executeRun(array $command) |
||
248 | } |
||
249 |
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.