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 |
||
63 | class UbLangConsole extends Console |
||
64 | { |
||
65 | protected $currentPhase; |
||
66 | protected $currentPhaseStep; |
||
67 | protected $phaseNumber = 0; |
||
68 | protected $phaseMessages = array(); |
||
69 | |||
70 | /** |
||
71 | * a list of the results we have received from stories |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $storyResults = []; |
||
75 | |||
76 | /** |
||
77 | * called when storyplayer starts |
||
78 | * |
||
79 | * @param string $version |
||
80 | * @param string $url |
||
81 | * @param string $copyright |
||
82 | * @param string $license |
||
83 | * @return void |
||
84 | */ |
||
85 | public function startStoryplayer($version, $url, $copyright, $license) |
||
94 | |||
95 | public function endStoryplayer($duration) |
||
99 | |||
100 | /** |
||
101 | * called when we start a new set of phases |
||
102 | * |
||
103 | * @param string $activity |
||
104 | * what are we doing? (e.g. 'creating', 'running') |
||
105 | * @param string $name |
||
106 | * the name of the phase group |
||
107 | * @param array|null $details |
||
108 | * optional explanation of what this PhaseGroup is trying |
||
109 | * to achieve |
||
110 | * @return void |
||
111 | */ |
||
112 | public function startPhaseGroup($activity, $name, $details = null) |
||
125 | |||
126 | View Code Duplication | public function endPhaseGroup($result) |
|
157 | |||
158 | /** |
||
159 | * called when a story starts a new phase |
||
160 | * |
||
161 | * @return void |
||
162 | */ |
||
163 | public function startPhase($phase) |
||
182 | |||
183 | /** |
||
184 | * called when a story ends a phase |
||
185 | * |
||
186 | * @return void |
||
187 | */ |
||
188 | public function endPhase($phase, $phaseResult) |
||
212 | |||
213 | /** |
||
214 | * called when a story logs an action |
||
215 | * |
||
216 | * @param string $msg |
||
217 | * @return void |
||
218 | */ |
||
219 | public function logPhaseActivity($msg, $codeLine = null) |
||
251 | |||
252 | /** |
||
253 | * called when a story logs the (possibly partial) output from |
||
254 | * running a subprocess |
||
255 | * |
||
256 | * @param string $msg the output to log |
||
257 | * @return void |
||
258 | */ |
||
259 | public function logPhaseSubprocessOutput($msg) |
||
263 | |||
264 | /** |
||
265 | * called when a story logs an error |
||
266 | * |
||
267 | * @param string $phaseName |
||
268 | * @param string $msg |
||
269 | * @return void |
||
270 | */ |
||
271 | public function logPhaseError($phaseName, $msg) |
||
275 | |||
276 | /** |
||
277 | * called when a story is skipped |
||
278 | * |
||
279 | * @param string $phaseName |
||
280 | * @param string $msg |
||
281 | * @return void |
||
282 | */ |
||
283 | public function logPhaseSkipped($phaseName, $msg) |
||
287 | |||
288 | public function logPhaseCodeLine($codeLine) |
||
292 | |||
293 | /** |
||
294 | * called when the outer CLI shell encounters a fatal error |
||
295 | * |
||
296 | * @param string $msg |
||
297 | * the error message to show the user |
||
298 | * |
||
299 | * @return void |
||
300 | */ |
||
301 | public function logCliError($msg) |
||
305 | |||
306 | /** |
||
307 | * |
||
308 | * @param string $msg |
||
309 | * @param Exception $e |
||
310 | * @return void |
||
311 | */ |
||
312 | public function logCliErrorWithException($msg, $e) |
||
318 | |||
319 | /** |
||
320 | * called when the outer CLI shell needs to publish a warning |
||
321 | * |
||
322 | * @param string $msg |
||
323 | * the warning message to show the user |
||
324 | * |
||
325 | * @return void |
||
326 | */ |
||
327 | public function logCliWarning($msg) |
||
331 | |||
332 | /** |
||
333 | * called when the outer CLI shell needs to tell the user something |
||
334 | * |
||
335 | * @param string $msg |
||
336 | * the message to show the user |
||
337 | * |
||
338 | * @return void |
||
339 | */ |
||
340 | public function logCliInfo($msg) |
||
344 | |||
345 | /** |
||
346 | * an alternative to using PHP's built-in var_dump() |
||
347 | * |
||
348 | * @param string $name |
||
349 | * a human-readable name to describe $var |
||
350 | * |
||
351 | * @param mixed $var |
||
352 | * the variable to dump |
||
353 | * |
||
354 | * @return void |
||
355 | */ |
||
356 | public function logVardump($name, $var) |
||
360 | } |
||
361 |
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.