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 namespace Modules\Core\Services; |
||
5 | class Composer extends \Illuminate\Foundation\Composer |
||
6 | { |
||
7 | protected $outputHandler = null; |
||
8 | private $output; |
||
9 | |||
10 | /** |
||
11 | * Enable real time output of all commands. |
||
12 | * |
||
13 | * @param $command |
||
14 | * @return void |
||
15 | */ |
||
16 | public function enableOutput($command) |
||
17 | { |
||
18 | $this->output = function ($type, $buffer) use ($command) { |
||
19 | if (Process::ERR === $type) { |
||
20 | $command->info(trim('[ERR] > ' . $buffer)); |
||
21 | } else { |
||
22 | $command->info(trim('> ' . $buffer)); |
||
23 | } |
||
24 | }; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Disable real time output of all commands. |
||
29 | * |
||
30 | * @return void |
||
31 | */ |
||
32 | public function disableOutput() |
||
36 | |||
37 | /** |
||
38 | * Update all composer packages. |
||
39 | * |
||
40 | * @param string $package |
||
41 | * @return void |
||
42 | */ |
||
43 | View Code Duplication | public function update($package = null) |
|
52 | |||
53 | /** |
||
54 | * Require a new composer package. |
||
55 | * |
||
56 | * @param string $package |
||
57 | * @return void |
||
58 | */ |
||
59 | View Code Duplication | public function install($package) |
|
68 | |||
69 | /** |
||
70 | * @return void |
||
71 | */ |
||
72 | public function dumpAutoload() |
||
78 | |||
79 | View Code Duplication | public function remove($package) |
|
88 | } |
||
89 |
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.