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 |
||
14 | abstract class Command extends SymfonyCommand |
||
15 | { |
||
16 | /** |
||
17 | * Symfony console output. |
||
18 | * |
||
19 | * @var SymfonyOutput |
||
20 | */ |
||
21 | protected $output; |
||
22 | |||
23 | /** |
||
24 | * Symfony input implementation. |
||
25 | * |
||
26 | * @var InputInterface |
||
27 | */ |
||
28 | protected $input; |
||
29 | |||
30 | /** |
||
31 | * The command description. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $description; |
||
36 | |||
37 | /** |
||
38 | * Command signature. |
||
39 | * |
||
40 | * @var null|string |
||
41 | */ |
||
42 | protected $signature = null; |
||
43 | |||
44 | /** |
||
45 | * Configure the command if signature is set. |
||
46 | */ |
||
47 | protected function configure() |
||
57 | |||
58 | /** |
||
59 | * Execute the command. |
||
60 | * |
||
61 | * @param InputInterface $input |
||
62 | * @param OutputInterface $output |
||
63 | */ |
||
64 | protected function execute(InputInterface $input, OutputInterface $output) |
||
74 | |||
75 | /** |
||
76 | * Return the output implementation. |
||
77 | * |
||
78 | * @return SymfonyOutput |
||
79 | */ |
||
80 | protected function getOutput() |
||
84 | |||
85 | /** |
||
86 | * Get root symfony output implementation. |
||
87 | * |
||
88 | * @return OutputInterface |
||
89 | */ |
||
90 | protected function getOutputInterface() |
||
94 | |||
95 | /** |
||
96 | * Get the value of a command argument. |
||
97 | * |
||
98 | * @param string $key |
||
99 | * |
||
100 | * @return string|array |
||
101 | */ |
||
102 | protected function argument($key = null) |
||
110 | |||
111 | /** |
||
112 | * Determine if the given argument is present. |
||
113 | * |
||
114 | * @param string|int $name |
||
115 | * |
||
116 | * @return bool |
||
117 | */ |
||
118 | protected function hasArgument($name) |
||
122 | |||
123 | /** |
||
124 | * Get the value of a command option. |
||
125 | * |
||
126 | * @param string $key |
||
127 | * |
||
128 | * @return string|array |
||
129 | */ |
||
130 | protected function option($key = null) |
||
138 | |||
139 | /** |
||
140 | * Determine if the given option is present. |
||
141 | * |
||
142 | * @param string $name |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | protected function hasOption($name) |
||
150 | |||
151 | /** |
||
152 | * Add input to the command. |
||
153 | * |
||
154 | * @param Input $input |
||
155 | */ |
||
156 | public function addInput(Input $input) |
||
166 | |||
167 | /** |
||
168 | * Ask for a confirmation. |
||
169 | * |
||
170 | * @param string $text |
||
171 | * |
||
172 | * @return mixed |
||
173 | */ |
||
174 | public function confirm($text) |
||
181 | |||
182 | /** |
||
183 | * Ask a question. |
||
184 | * |
||
185 | * @param string $question |
||
186 | * @param mixed|null $default |
||
187 | * |
||
188 | * @return mixed |
||
189 | */ |
||
190 | View Code Duplication | public function ask($question, $default = null) |
|
197 | |||
198 | /** |
||
199 | * Ask a password. |
||
200 | * |
||
201 | * @param string $question |
||
202 | * |
||
203 | * @return mixed |
||
204 | */ |
||
205 | public function askPassword($question) |
||
215 | |||
216 | /** |
||
217 | * Ask a question where the answer is available from a list of predefined choices. |
||
218 | * |
||
219 | * @param string $question |
||
220 | * @param array $choices |
||
221 | * @param mixed|null $default |
||
222 | * |
||
223 | * @return mixed |
||
224 | */ |
||
225 | View Code Duplication | public function choose($question, array $choices, $default = null) |
|
233 | |||
234 | /** |
||
235 | * Ask a question where some auto-completion help is provided. |
||
236 | * |
||
237 | * @param string $question |
||
238 | * @param array $autoCompletion |
||
239 | * @param mixed|null $default |
||
240 | * |
||
241 | * @return mixed |
||
242 | */ |
||
243 | View Code Duplication | public function anticipate($question, array $autoCompletion, $default = null) |
|
251 | |||
252 | /** |
||
253 | * Ask a question where the answer is available from a list of predefined choices and more choices can be selected. |
||
254 | * |
||
255 | * @param string $question |
||
256 | * @param array $choices |
||
257 | * @param mixed|null $default |
||
258 | * |
||
259 | * @return mixed |
||
260 | */ |
||
261 | View Code Duplication | public function choice($question, array $choices, $default = null) |
|
269 | } |
||
270 |
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.