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 |
||
16 | class Paginator implements PredictableInterface, \Countable |
||
17 | { |
||
18 | /** |
||
19 | * Default limit value. |
||
20 | */ |
||
21 | const DEFAULT_LIMIT = 25; |
||
22 | |||
23 | /** |
||
24 | * Default page parameter. |
||
25 | */ |
||
26 | const DEFAULT_PARAMETER = 'page'; |
||
27 | |||
28 | /** |
||
29 | * The query array will be connected to every page URL generated by paginator. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | private $queryData = []; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $pageParameter = 'page'; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | private $pageNumber = 1; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | private $countPages = 1; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | private $limit = self::DEFAULT_LIMIT; |
||
54 | |||
55 | /** |
||
56 | * @var int |
||
57 | */ |
||
58 | private $count = 0; |
||
59 | |||
60 | /** |
||
61 | * @invisible |
||
62 | * @var ServerRequestInterface |
||
63 | */ |
||
64 | private $request = null; |
||
65 | |||
66 | /** |
||
67 | * @var UriInterface |
||
68 | */ |
||
69 | private $uri = null; |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | public function __construct( |
||
81 | |||
82 | /** |
||
83 | * @param ServerRequestInterface $request |
||
84 | */ |
||
85 | public function setRequest(ServerRequestInterface $request) |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function setUri(UriInterface $uri) |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | public function getUri() |
||
108 | |||
109 | /** |
||
110 | * Specify the query (as array) which will be attached to every generated page URL. |
||
111 | * |
||
112 | * @param array $query |
||
113 | * @param bool $replace Replace existed query data entirely. |
||
114 | */ |
||
115 | public function setQuery(array $query, $replace = false) |
||
119 | |||
120 | /** |
||
121 | * @return array |
||
122 | */ |
||
123 | public function getQuery() |
||
127 | |||
128 | /** |
||
129 | * Update page parameter name from request query. Page number should be fetched from queryParams |
||
130 | * of provided request instance. |
||
131 | * |
||
132 | * @param string $pageParameter |
||
133 | * @return self |
||
134 | */ |
||
135 | public function setParameter($pageParameter) |
||
145 | |||
146 | /** |
||
147 | * Get page query parameter name. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getParameter() |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | View Code Duplication | public function setCount($count) |
|
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | public function count() |
||
178 | |||
179 | /** |
||
180 | * Set pagination limit. |
||
181 | * |
||
182 | * @param int $limit |
||
183 | * @return $this |
||
184 | */ |
||
185 | View Code Duplication | public function setLimit($limit) |
|
196 | |||
197 | /** |
||
198 | * Get pagination limit (items per page). |
||
199 | * |
||
200 | * @return int |
||
201 | */ |
||
202 | public function getLimit() |
||
206 | |||
207 | /** |
||
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | public function setPage($number) |
||
217 | |||
218 | /** |
||
219 | * {@inheritdoc} |
||
220 | */ |
||
221 | public function getPage() |
||
233 | |||
234 | /** |
||
235 | * Get pagination offset. |
||
236 | * |
||
237 | * @return int |
||
238 | */ |
||
239 | public function getOffset() |
||
243 | |||
244 | /** |
||
245 | * The count of pages required to represent all records using a specified limit value. |
||
246 | * |
||
247 | * @return int |
||
248 | */ |
||
249 | public function countPages() |
||
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | public function countDisplayed() |
||
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | public function isRequired() |
||
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | public function nextPage() |
||
285 | |||
286 | /** |
||
287 | * {@inheritdoc} |
||
288 | */ |
||
289 | public function previousPage() |
||
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | public function paginate(PaginableInterface $paginable) |
||
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | public function uri($pageNumber) |
||
320 | } |
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.