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 |
||
15 | abstract class SysVinitCommandBuilder implements CommandBuilderInterface |
||
16 | { |
||
17 | /** |
||
18 | * Command name. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $cmd; |
||
23 | |||
24 | /** |
||
25 | * Command action. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $action; |
||
30 | |||
31 | /** |
||
32 | * Whether the command should be quiet or not. |
||
33 | * |
||
34 | * @var boolean |
||
35 | */ |
||
36 | protected $quiet = false; |
||
37 | |||
38 | /** |
||
39 | * Service. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $service; |
||
44 | |||
45 | /** |
||
46 | * Option list. |
||
47 | * |
||
48 | * @var string[] |
||
49 | */ |
||
50 | protected $options = []; |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | 6 | View Code Duplication | public function start($service) |
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 4 | View Code Duplication | public function stop($service) |
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 4 | View Code Duplication | public function restart($service) |
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | abstract public function enable($service); |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | abstract public function disable($service); |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 2 | public function quiet() |
|
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | 12 | public function getCommand() |
|
142 | } |
||
143 |
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.