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 |
||
17 | class DataObject implements DataInterface |
||
18 | { |
||
19 | /** |
||
20 | * Object attributes |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $_data = []; |
||
25 | |||
26 | /** |
||
27 | * Constructor |
||
28 | * |
||
29 | * By default is looking for first argument as array and assigns it as object attributes |
||
30 | * This behavior may change in child classes |
||
31 | * |
||
32 | * @param array $data |
||
33 | */ |
||
34 | public function __construct(array $data = []) |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function addData(array $arr) |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | View Code Duplication | public function setData($key, $value) |
|
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function unsetData($key = null) |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function setDataUsingMethod($key, $args = []) |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function getData($key = '', $index = null) |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public function getDataByKey($key) |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | public function getDataByPath($path) |
||
153 | |||
154 | /** |
||
155 | * Get value from _data array without parse key |
||
156 | * |
||
157 | * @param string $key |
||
158 | * |
||
159 | * @return mixed |
||
160 | */ |
||
161 | protected function _getData($key) |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | public function getDataUsingMethod($key, $args = null) |
||
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | public function hasData($key = '') |
||
191 | } |
||
192 |
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.