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:
Complex classes like Paginator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Paginator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Paginator |
||
9 | { |
||
10 | private $currentPage = 1; |
||
11 | private $customNext; |
||
12 | private $customPrev; |
||
13 | private $pageCount; |
||
14 | private $pagerSize = 5; |
||
15 | private $url; |
||
16 | private $urlPart = ':page'; |
||
17 | |||
18 | /** |
||
19 | * @param int $pageNum |
||
20 | * @return string |
||
21 | */ |
||
22 | 1 | private function url(int $pageNum): string |
|
26 | |||
27 | /** |
||
28 | * @param $pageCount |
||
29 | */ |
||
30 | 1 | public function setPageCount(int $pageCount): void |
|
34 | |||
35 | /** |
||
36 | * @param $pageCount |
||
37 | */ |
||
38 | public function setPageCountByTotalRecords(int $rowCount, int $numPerPage): void |
||
42 | |||
43 | /** |
||
44 | * @param $url |
||
45 | */ |
||
46 | 1 | public function setUrl(string $url): void |
|
50 | |||
51 | /** |
||
52 | * @param $replace |
||
53 | */ |
||
54 | 1 | public function setUrlPart(string $replace): void |
|
58 | |||
59 | /** |
||
60 | * @param int $page_no |
||
61 | */ |
||
62 | 1 | public function setCurrentPage(int $page_no): void |
|
66 | |||
67 | |||
68 | /** |
||
69 | * @param $url |
||
70 | */ |
||
71 | public function setCustomPrev($url): void |
||
75 | |||
76 | |||
77 | /** |
||
78 | * @param $url |
||
79 | */ |
||
80 | public function setCustomNext(string $url): void |
||
84 | |||
85 | /** |
||
86 | * @param int $numBoxes an ODD number! |
||
87 | */ |
||
88 | 1 | public function setPagerSize(int $numBoxes): void |
|
95 | |||
96 | /** |
||
97 | * @return int |
||
98 | */ |
||
99 | 1 | public function getPagerSize(): int |
|
106 | |||
107 | /** |
||
108 | * @throws PaginatorException |
||
109 | */ |
||
110 | 2 | private function ensurePageCount() |
|
116 | |||
117 | /** |
||
118 | * @throws PaginatorException |
||
119 | */ |
||
120 | 1 | private function ensureUrl() |
|
126 | |||
127 | /** |
||
128 | * @throws PaginatorException |
||
129 | */ |
||
130 | 1 | private function ensureCurrentPage() |
|
136 | |||
137 | /** |
||
138 | * @return int |
||
139 | */ |
||
140 | 1 | private function calculateStart(int $pages): int |
|
156 | |||
157 | /** |
||
158 | * @return string |
||
159 | * @throws PaginatorException |
||
160 | */ |
||
161 | 2 | public function render(): string |
|
234 | } |
||
235 |
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.