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 | class AdapterArrayObject extends AbstractAdapter implements AdapterInterface, \Zend\Stdlib\InitializableInterface |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * |
||
20 | * @var \ArrayObject | \Zend\Stdlib\ArrayObject |
||
21 | */ |
||
22 | protected $object; |
||
23 | |||
24 | /** |
||
25 | * |
||
26 | * @var int |
||
27 | */ |
||
28 | protected $page; |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $order; |
||
35 | |||
36 | /** |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $column; |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | * @var int |
||
45 | */ |
||
46 | protected $itemCountPerPage; |
||
47 | |||
48 | /** |
||
49 | * Quick search |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $quickSearch; |
||
53 | |||
54 | |||
55 | /** |
||
56 | * Array of filters |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $filters; |
||
60 | |||
61 | const DEFAULT_PAGE = 1; |
||
62 | const DEFAULT_ORDER = 'asc'; |
||
63 | const DEFAULT_ITEM_COUNT_PER_PAGE = 2; |
||
64 | |||
65 | |||
66 | View Code Duplication | public function __construct($object) |
|
76 | |||
77 | /** |
||
78 | * Init method |
||
79 | */ |
||
80 | public function init() |
||
100 | |||
101 | public function getPureValueOfFilter($key) |
||
105 | |||
106 | public function getValueOfFilter($key, $prefix = 'zff_') |
||
110 | |||
111 | /** |
||
112 | * Get page |
||
113 | * |
||
114 | * @return int |
||
115 | */ |
||
116 | public function getPage() |
||
120 | |||
121 | /** |
||
122 | * Set page |
||
123 | * |
||
124 | * @param string $page |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function setPage($page) |
||
132 | |||
133 | /** |
||
134 | * Get order |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | public function getOrder() |
||
142 | |||
143 | /** |
||
144 | * Set asc or desc ordering |
||
145 | * |
||
146 | * @param $order |
||
147 | */ |
||
148 | public function setOrder($order) |
||
152 | |||
153 | /** |
||
154 | * Get column |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getColumn() |
||
162 | |||
163 | /** |
||
164 | * |
||
165 | * @param string $column |
||
166 | * @return $this |
||
167 | */ |
||
168 | public function setColumn($column) |
||
173 | |||
174 | /** |
||
175 | * Get item count per page |
||
176 | * |
||
177 | * @return int |
||
178 | */ |
||
179 | public function getItemCountPerPage() |
||
183 | |||
184 | /** |
||
185 | * |
||
186 | * @param int $itemCountPerPage |
||
187 | */ |
||
188 | public function setItemCountPerPage($itemCountPerPage) |
||
192 | |||
193 | /** |
||
194 | * Return offset |
||
195 | * |
||
196 | * @return int |
||
197 | */ |
||
198 | public function getOffset() |
||
202 | |||
203 | /** |
||
204 | * Get quick search string |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getQuickSearch() |
||
212 | } |
||
213 |
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.