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 |
||
4 | class Pagination implements PaginationInterface |
||
5 | { |
||
6 | |||
7 | |||
8 | /** |
||
9 | * The total number of items to paginate |
||
10 | * @var int |
||
11 | */ |
||
12 | public $items_count; |
||
13 | |||
14 | /** |
||
15 | * The current page number |
||
16 | * @var int|null |
||
17 | */ |
||
18 | public $page_number; |
||
19 | |||
20 | /** |
||
21 | * The number of page items in use |
||
22 | * @var int |
||
23 | */ |
||
24 | public $page_size; |
||
25 | |||
26 | /** |
||
27 | * The default number of items on a page |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $default_page_size = 25; |
||
31 | |||
32 | /** |
||
33 | * The maximum page length |
||
34 | * @var int |
||
35 | */ |
||
36 | protected $max_page_size = 100; |
||
37 | |||
38 | |||
39 | |||
40 | |||
41 | /** |
||
42 | * @param int $items_count The total number of items to paginate |
||
43 | * @param int $page_size Optional: number of items on a single page, default: 25 |
||
44 | * @param int $page_size Optional: Maximum number of items on a single page, default: 100 |
||
45 | */ |
||
46 | 144 | public function __construct( int $items_count, int $page_size = null, int $max_page_size = null ) |
|
53 | |||
54 | |||
55 | /** |
||
56 | * @inheritDoc |
||
57 | */ |
||
58 | 72 | public function isActive() : bool |
|
62 | |||
63 | |||
64 | /** |
||
65 | * @inheritDoc |
||
66 | */ |
||
67 | 36 | public function isDefaultPageSize() : bool |
|
71 | |||
72 | |||
73 | |||
74 | |||
75 | |||
76 | /** |
||
77 | * @inheritDoc |
||
78 | */ |
||
79 | 36 | public function getPagesCount() : int |
|
83 | |||
84 | |||
85 | /** |
||
86 | * @inheritDoc |
||
87 | */ |
||
88 | 108 | public function getPageSize() |
|
92 | |||
93 | |||
94 | /** |
||
95 | * @inheritDoc |
||
96 | * @throws PaginationRangeException When page size not between 1 and $max_page_size |
||
97 | */ |
||
98 | 28 | public function setPageSize( $size ) |
|
113 | |||
114 | |||
115 | |||
116 | |||
117 | |||
118 | /** |
||
119 | * @inheritDoc |
||
120 | */ |
||
121 | 72 | public function getCurrent() |
|
125 | |||
126 | |||
127 | |||
128 | /** |
||
129 | * @inheritDoc |
||
130 | * @throws PaginationInvalidArgumentException when page number is not integer |
||
131 | * @throws PaginationRangeException when page number does not exists |
||
132 | */ |
||
133 | 96 | public function setCurrent( $number ) |
|
157 | |||
158 | |||
159 | |||
160 | |||
161 | |||
162 | /** |
||
163 | * @inheritDoc |
||
164 | */ |
||
165 | 48 | View Code Duplication | public function getPrevious() |
176 | |||
177 | |||
178 | |||
179 | /** |
||
180 | * @inheritDoc |
||
181 | */ |
||
182 | 48 | View Code Duplication | public function getNext() |
193 | |||
194 | |||
195 | |||
196 | |||
197 | /** |
||
198 | * @inheritDoc |
||
199 | */ |
||
200 | 108 | public function getFirst() : int |
|
204 | |||
205 | /** |
||
206 | * @inheritDoc |
||
207 | */ |
||
208 | 108 | public function getLast() : int |
|
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.