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 |
||
19 | class CommandProcessor |
||
20 | { |
||
21 | /** var HookManager */ |
||
22 | protected $hookManager; |
||
23 | /** var FormatterManager */ |
||
24 | protected $formatterManager; |
||
25 | /** var callable */ |
||
26 | protected $displayErrorFunction; |
||
27 | |||
28 | public function __construct(HookManager $hookManager) |
||
32 | |||
33 | /** |
||
34 | * Return the hook manager |
||
35 | * @return HookManager |
||
36 | */ |
||
37 | public function hookManager() |
||
41 | |||
42 | public function setFormatterManager(FormatterManager $formatterManager) |
||
46 | |||
47 | public function setDisplayErrorFunction(callable $fn) |
||
51 | |||
52 | /** |
||
53 | * Return the formatter manager |
||
54 | * @return FormatterManager |
||
55 | */ |
||
56 | public function formatterManager() |
||
60 | |||
61 | public function process( |
||
84 | |||
85 | View Code Duplication | public function validateRunAndAlter( |
|
105 | |||
106 | public function processResults($names, $result, $args, $annotationData) |
||
110 | |||
111 | /** |
||
112 | * Handle the result output and status code calculation. |
||
113 | */ |
||
114 | public function handleResults(OutputInterface $output, $names, $result, AnnotationData $annotationData, $options = []) |
||
134 | |||
135 | /** |
||
136 | * Run the main command callback |
||
137 | */ |
||
138 | protected function runCommandCallback($commandCallback, $args) |
||
148 | |||
149 | /** |
||
150 | * Determine the formatter that should be used to render |
||
151 | * output. |
||
152 | * |
||
153 | * If the user specified a format via the --format option, |
||
154 | * then always return that. Otherwise, return the default |
||
155 | * format, unless --pipe was specified, in which case |
||
156 | * return the default pipe format, format-pipe. |
||
157 | * |
||
158 | * n.b. --pipe is a handy option introduced in Drush 2 |
||
159 | * (or perhaps even Drush 1) that indicates that the command |
||
160 | * should select the output format that is most appropriate |
||
161 | * for use in scripts (e.g. to pipe to another command). |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | protected function getFormat($options) |
||
182 | |||
183 | /** |
||
184 | * Determine whether we should use stdout or stderr. |
||
185 | */ |
||
186 | protected function chooseOutputStream(OutputInterface $output, $status) |
||
195 | |||
196 | /** |
||
197 | * Call the formatter to output the provided data. |
||
198 | */ |
||
199 | protected function writeUsingFormatter(OutputInterface $output, $structuredOutput, AnnotationData $annotationData, $options) |
||
211 | |||
212 | /** |
||
213 | * Description |
||
214 | * @param type $output |
||
215 | * @param type $status |
||
216 | * @param type $structuredOutput |
||
217 | * @return type |
||
218 | */ |
||
219 | protected function writeErrorMessage($output, $status, $structuredOutput, $originalResult) |
||
228 | |||
229 | /** |
||
230 | * If the result object is a string, then print it. |
||
231 | */ |
||
232 | protected function writeCommandOutput( |
||
243 | |||
244 | /** |
||
245 | * If a status code was set, then return it; otherwise, |
||
246 | * presume success. |
||
247 | */ |
||
248 | protected function interpretStatusCode($status) |
||
255 | } |
||
256 |
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.