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 |
||
8 | class Pagination |
||
9 | { |
||
10 | /** |
||
11 | * @var int|null Current page number. |
||
12 | */ |
||
13 | protected $currentPage; |
||
14 | |||
15 | /** |
||
16 | * @var string Name of query parameter that contains page number. |
||
17 | */ |
||
18 | protected $pageParam = 'page'; |
||
19 | |||
20 | /** |
||
21 | * @var string Name of query parameter that contains number of items on page. |
||
22 | */ |
||
23 | protected $pageSizeParam = 'per-page'; |
||
24 | |||
25 | /** |
||
26 | * @var string Name of route. If route was not specified then current route |
||
27 | * will be used. |
||
28 | */ |
||
29 | protected $route; |
||
30 | |||
31 | /** |
||
32 | * @var int Total number of items. |
||
33 | */ |
||
34 | protected $totalCount = 0; |
||
35 | |||
36 | /** |
||
37 | * @var int Default number of items per page. Will be used if [[$pageSize]] |
||
38 | * not specified. |
||
39 | */ |
||
40 | protected $defaultPageSize = 20; |
||
41 | |||
42 | /** |
||
43 | * @var int Default limit of items per page. |
||
44 | */ |
||
45 | protected $maxPageSize = 50; |
||
46 | |||
47 | /** |
||
48 | * @var int|null Default number of items per page. |
||
49 | */ |
||
50 | protected $pageSize; |
||
51 | |||
52 | /** |
||
53 | * @var Request |
||
54 | */ |
||
55 | protected $request; |
||
56 | |||
57 | /** |
||
58 | * Pagination constructor. |
||
59 | * |
||
60 | * @param RequestStack $requestStack |
||
61 | */ |
||
62 | public function __construct(RequestStack $requestStack) |
||
66 | |||
67 | /** |
||
68 | * Calculate total number of pages. |
||
69 | * |
||
70 | * @return int number of pages |
||
71 | */ |
||
72 | public function getPageCount() |
||
84 | |||
85 | /** |
||
86 | * Get current page number. Pages numeration starts from zero. |
||
87 | * |
||
88 | * @return int the zero-based current page number. |
||
89 | */ |
||
90 | public function getCurrentPage() |
||
100 | |||
101 | /** |
||
102 | * Set current page number. |
||
103 | * |
||
104 | * @param int $pageNumber |
||
105 | * |
||
106 | * @return $this |
||
107 | */ |
||
108 | protected function setPage($pageNumber) |
||
129 | |||
130 | /** |
||
131 | * Get current number of items per page. If it's not specified yet the value |
||
132 | * will be taken from query parameters. In other case default value will |
||
133 | * be used. |
||
134 | * |
||
135 | * @return int |
||
136 | */ |
||
137 | public function getPageSize() |
||
152 | |||
153 | /** |
||
154 | * Set number of items to show per page. By default limit will be used. |
||
155 | * |
||
156 | * @param int $pageSize |
||
157 | * @param bool $useLimit |
||
158 | * |
||
159 | * @return $this |
||
160 | * @throws \Exception |
||
161 | */ |
||
162 | public function setPageSize($pageSize, $useLimit = true) |
||
183 | |||
184 | /** |
||
185 | * Fetch current route name. |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getRoute() |
||
197 | |||
198 | /** |
||
199 | * @return int Get offset value that can be used in data source query. |
||
200 | */ |
||
201 | public function getOffset() |
||
207 | |||
208 | /** |
||
209 | * @return int Get limit value that can be used in data source query.] |
||
210 | */ |
||
211 | public function getLimit() |
||
217 | |||
218 | /** |
||
219 | * Get name of query parameter that stores current page index. |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | public function getPageParamName() |
||
227 | |||
228 | /** |
||
229 | * Get default number of items per page. |
||
230 | * |
||
231 | * @return int |
||
232 | */ |
||
233 | public function getDefaultPageSize() |
||
237 | |||
238 | /** |
||
239 | * Get name of query parameter that stores number of items per page. |
||
240 | * |
||
241 | * @return string |
||
242 | */ |
||
243 | public function getPageSizeParam() |
||
247 | |||
248 | /** |
||
249 | * Set total number of items. |
||
250 | * |
||
251 | * @param int $totalCount |
||
252 | * |
||
253 | * @return $this |
||
254 | * @throws \Exception |
||
255 | */ |
||
256 | View Code Duplication | public function setTotalCount($totalCount) |
|
270 | |||
271 | /** |
||
272 | * Set route name. |
||
273 | * |
||
274 | * @param string $route Route name. |
||
275 | * |
||
276 | * @return $this |
||
277 | * @throws \Exception |
||
278 | */ |
||
279 | View Code Duplication | public function setRoute($route) |
|
292 | |||
293 | /** |
||
294 | * @param int $defaultPageSize |
||
295 | * |
||
296 | * @return $this |
||
297 | * @throws \Exception |
||
298 | */ |
||
299 | View Code Duplication | public function setDefaultPageSize($defaultPageSize) |
|
313 | |||
314 | /** |
||
315 | * @param string $pageSizeParam |
||
316 | * |
||
317 | * @return $this |
||
318 | * @throws \Exception |
||
319 | */ |
||
320 | View Code Duplication | public function setPageSizeParam($pageSizeParam) |
|
334 | |||
335 | /** |
||
336 | * @param string $pageParam |
||
337 | * |
||
338 | * @return $this |
||
339 | * @throws \Exception |
||
340 | */ |
||
341 | View Code Duplication | public function setPageParam($pageParam) |
|
355 | } |
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.