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 InlineKeyboardPagination 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 InlineKeyboardPagination, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class InlineKeyboardPagination implements InlineKeyboardPaginator |
||
13 | { |
||
14 | /** |
||
15 | * @var integer |
||
16 | */ |
||
17 | protected $items_per_page; |
||
18 | |||
19 | /** |
||
20 | * @var integer |
||
21 | */ |
||
22 | protected $max_buttons = 5; |
||
23 | |||
24 | /** |
||
25 | * @var bool |
||
26 | */ |
||
27 | protected $force_button_count = false; |
||
28 | |||
29 | /** |
||
30 | * @var integer |
||
31 | */ |
||
32 | protected $selected_page; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $items; |
||
38 | |||
39 | /** |
||
40 | * @var integer |
||
41 | */ |
||
42 | protected $range_offset = 1; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $command; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $callback_data_format = 'command={COMMAND}&oldPage={OLD_PAGE}&newPage={NEW_PAGE}'; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $labels = [ |
||
58 | 'default' => '%d', |
||
59 | 'first' => '« %d', |
||
60 | 'previous' => '‹ %d', |
||
61 | 'current' => '· %d ·', |
||
62 | 'next' => '%d ›', |
||
63 | 'last' => '%d »', |
||
64 | ]; |
||
65 | |||
66 | /** |
||
67 | * @inheritdoc |
||
68 | * @throws InlineKeyboardPaginationException |
||
69 | */ |
||
70 | public function setMaxButtons(int $max_buttons = 5, bool $force_button_count = false): InlineKeyboardPagination |
||
80 | |||
81 | /** |
||
82 | * Get the current callback format. |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | public function getCallbackDataFormat(): string |
||
90 | |||
91 | /** |
||
92 | * Set the callback_data format. |
||
93 | * |
||
94 | * @param string $callback_data_format |
||
95 | * |
||
96 | * @return InlineKeyboardPagination |
||
97 | */ |
||
98 | public function setCallbackDataFormat(string $callback_data_format): InlineKeyboardPagination |
||
104 | |||
105 | /** |
||
106 | * Return list of keyboard button labels. |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | public function getLabels(): array |
||
114 | |||
115 | /** |
||
116 | * Set the keyboard button labels. |
||
117 | * |
||
118 | * @param array $labels |
||
119 | * |
||
120 | * @return InlineKeyboardPagination |
||
121 | */ |
||
122 | public function setLabels($labels): InlineKeyboardPagination |
||
128 | |||
129 | /** |
||
130 | * @inheritdoc |
||
131 | */ |
||
132 | public function setCommand(string $command = 'pagination'): InlineKeyboardPagination |
||
138 | |||
139 | /** |
||
140 | * @inheritdoc |
||
141 | * @throws InlineKeyboardPaginationException |
||
142 | */ |
||
143 | public function setSelectedPage(int $selected_page): InlineKeyboardPagination |
||
164 | |||
165 | /** |
||
166 | * Get the number of items shown per page. |
||
167 | * |
||
168 | * @return int |
||
169 | */ |
||
170 | public function getItemsPerPage(): int |
||
174 | |||
175 | /** |
||
176 | * Set how many items should be shown per page. |
||
177 | * |
||
178 | * @param int $items_per_page |
||
179 | * |
||
180 | * @return InlineKeyboardPagination |
||
181 | * @throws InlineKeyboardPaginationException |
||
182 | */ |
||
183 | public function setItemsPerPage($items_per_page): InlineKeyboardPagination |
||
192 | |||
193 | /** |
||
194 | * Set the items for the pagination. |
||
195 | * |
||
196 | * @param array $items |
||
197 | * |
||
198 | * @return InlineKeyboardPagination |
||
199 | * @throws InlineKeyboardPaginationException |
||
200 | */ |
||
201 | public function setItems(array $items): InlineKeyboardPagination |
||
210 | |||
211 | /** |
||
212 | * Set max number of pages based on labels which user defined |
||
213 | * |
||
214 | * @return InlineKeyboardPagination |
||
215 | * @throws InlineKeyboardPaginationException |
||
216 | */ |
||
217 | public function setMaxPageBasedOnLabels(): InlineKeyboardPagination |
||
250 | |||
251 | /** |
||
252 | * Set offset of range |
||
253 | * |
||
254 | * @return InlineKeyboardPagination |
||
255 | * @throws InlineKeyboardPaginationException |
||
256 | */ |
||
257 | public function setRangeOffset($offset): InlineKeyboardPagination |
||
267 | |||
268 | /** |
||
269 | * Calculate and return the number of pages. |
||
270 | * |
||
271 | * @return int |
||
272 | */ |
||
273 | public function getNumberOfPages(): int |
||
277 | |||
278 | /** |
||
279 | * TelegramBotPagination constructor. |
||
280 | * |
||
281 | * @inheritdoc |
||
282 | * @throws InlineKeyboardPaginationException |
||
283 | */ |
||
284 | public function __construct( |
||
295 | |||
296 | /** |
||
297 | * @inheritdoc |
||
298 | * @throws InlineKeyboardPaginationException |
||
299 | */ |
||
300 | public function getPagination(int $selected_page = null): array |
||
311 | |||
312 | /** |
||
313 | * Generate the keyboard with the correctly labelled buttons. |
||
314 | * |
||
315 | * @return array |
||
316 | */ |
||
317 | protected function generateKeyboard(): array |
||
382 | |||
383 | /** |
||
384 | * Get the range of intermediate buttons for the keyboard. |
||
385 | * |
||
386 | * @return array |
||
387 | */ |
||
388 | protected function generateRange(): array |
||
404 | |||
405 | /** |
||
406 | * Generate the button for the passed page. |
||
407 | * |
||
408 | * @param int $page |
||
409 | * @param string $label |
||
410 | * |
||
411 | * @return array |
||
412 | */ |
||
413 | protected function generateButton(int $page, string $label): array |
||
421 | |||
422 | /** |
||
423 | * Generate the callback data for the passed page. |
||
424 | * |
||
425 | * @param int $page |
||
426 | * |
||
427 | * @return string |
||
428 | */ |
||
429 | protected function generateCallbackData(int $page): string |
||
437 | |||
438 | /** |
||
439 | * Get the prepared items for the selected page. |
||
440 | * |
||
441 | * @return array |
||
442 | */ |
||
443 | protected function getPreparedItems(): array |
||
447 | |||
448 | /** |
||
449 | * Get the items offset for the selected page. |
||
450 | * |
||
451 | * @return int |
||
452 | */ |
||
453 | protected function getOffset(): int |
||
457 | |||
458 | /** |
||
459 | * Get the parameters from the callback query. |
||
460 | * |
||
461 | * @todo Possibly make it work for custom formats too? |
||
462 | * |
||
463 | * @param string $data |
||
464 | * |
||
465 | * @return array |
||
466 | */ |
||
467 | public static function getParametersFromCallbackData($data): array |
||
473 | } |
||
474 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.