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 |
||
21 | class Configuration extends DataObject implements ConfigurationInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var Filesystem |
||
25 | */ |
||
26 | private $filesystem; |
||
27 | |||
28 | /** |
||
29 | * @var PathInterface |
||
30 | */ |
||
31 | private $path; |
||
32 | |||
33 | /** |
||
34 | * Configuration items |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | private $configItems = []; |
||
39 | |||
40 | /** |
||
41 | * Config constructor. |
||
42 | * |
||
43 | * @param array $data |
||
44 | */ |
||
45 | public function __construct(array $data = []) |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function checkConfig() |
||
71 | |||
72 | /** |
||
73 | * Overwrite data in the object. |
||
74 | * |
||
75 | * The $key parameter can be string or array. |
||
76 | * If $key is string, the attribute value will be overwritten by $value |
||
77 | * |
||
78 | * If $key is an array, it will overwrite all the data in the object. |
||
79 | * |
||
80 | * @param string|array $key |
||
81 | * @param mixed $value |
||
82 | * |
||
83 | * @return $this |
||
84 | */ |
||
85 | View Code Duplication | public function setData($key, $value = null) |
|
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function saveConfig() |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function clearConfig() |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function getConfigItems() |
||
156 | |||
157 | /** |
||
158 | * Get config file content |
||
159 | * |
||
160 | * @return bool|string |
||
161 | */ |
||
162 | private function getFileContent() |
||
166 | |||
167 | /** |
||
168 | * Initialise the default config items |
||
169 | * |
||
170 | * @return array |
||
171 | */ |
||
172 | private function initDefaultConfig() |
||
180 | } |
||
181 |
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.