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 | private $items_per_page; |
||
18 | |||
19 | /** |
||
20 | * @var integer |
||
21 | */ |
||
22 | private $max_buttons = 5; |
||
23 | |||
24 | /** |
||
25 | * @var bool |
||
26 | */ |
||
27 | private $force_button_count = false; |
||
28 | |||
29 | /** |
||
30 | * @var integer |
||
31 | */ |
||
32 | private $selected_page; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $items; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $command; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $callback_data_format = 'command={COMMAND}&oldPage={OLD_PAGE}&newPage={NEW_PAGE}'; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | private $labels = [ |
||
53 | 'default' => '%d', |
||
54 | 'first' => '« %d', |
||
55 | 'previous' => '‹ %d', |
||
56 | 'current' => '· %d ·', |
||
57 | 'next' => '%d ›', |
||
58 | 'last' => '%d »', |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * @inheritdoc |
||
63 | * @throws InlineKeyboardPaginationException |
||
64 | */ |
||
65 | public function setMaxButtons(int $max_buttons = 5, bool $force_button_count = false): InlineKeyboardPagination |
||
75 | |||
76 | /** |
||
77 | * Get the current callback format. |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function getCallbackDataFormat(): string |
||
85 | |||
86 | /** |
||
87 | * Set the callback_data format. |
||
88 | * |
||
89 | * @param string $callback_data_format |
||
90 | * |
||
91 | * @return InlineKeyboardPagination |
||
92 | */ |
||
93 | public function setCallbackDataFormat(string $callback_data_format): InlineKeyboardPagination |
||
99 | |||
100 | /** |
||
101 | * Return list of keyboard button labels. |
||
102 | * |
||
103 | * @return array |
||
104 | */ |
||
105 | public function getLabels(): array |
||
109 | |||
110 | /** |
||
111 | * Set the keyboard button labels. |
||
112 | * |
||
113 | * @param array $labels |
||
114 | * |
||
115 | * @return InlineKeyboardPagination |
||
116 | */ |
||
117 | public function setLabels($labels): InlineKeyboardPagination |
||
123 | |||
124 | /** |
||
125 | * @inheritdoc |
||
126 | */ |
||
127 | public function setCommand(string $command = 'pagination'): InlineKeyboardPagination |
||
133 | |||
134 | /** |
||
135 | * @inheritdoc |
||
136 | * @throws InlineKeyboardPaginationException |
||
137 | */ |
||
138 | public function setSelectedPage(int $selected_page): InlineKeyboardPagination |
||
148 | |||
149 | /** |
||
150 | * Get the number of items shown per page. |
||
151 | * |
||
152 | * @return int |
||
153 | */ |
||
154 | public function getItemsPerPage(): int |
||
158 | |||
159 | /** |
||
160 | * Set how many items should be shown per page. |
||
161 | * |
||
162 | * @param int $items_per_page |
||
163 | * |
||
164 | * @return InlineKeyboardPagination |
||
165 | * @throws InlineKeyboardPaginationException |
||
166 | */ |
||
167 | public function setItemsPerPage($items_per_page): InlineKeyboardPagination |
||
176 | |||
177 | /** |
||
178 | * Set the items for the pagination. |
||
179 | * |
||
180 | * @param array $items |
||
181 | * |
||
182 | * @return InlineKeyboardPagination |
||
183 | * @throws InlineKeyboardPaginationException |
||
184 | */ |
||
185 | public function setItems(array $items): InlineKeyboardPagination |
||
194 | |||
195 | /** |
||
196 | * Calculate and return the number of pages. |
||
197 | * |
||
198 | * @return int |
||
199 | */ |
||
200 | public function getNumberOfPages(): int |
||
204 | |||
205 | /** |
||
206 | * TelegramBotPagination constructor. |
||
207 | * |
||
208 | * @inheritdoc |
||
209 | * @throws InlineKeyboardPaginationException |
||
210 | */ |
||
211 | public function __construct(array $items, string $command = 'pagination', int $selected_page = 1, int $items_per_page = 5) |
||
218 | |||
219 | /** |
||
220 | * @inheritdoc |
||
221 | * @throws InlineKeyboardPaginationException |
||
222 | */ |
||
223 | public function getPagination(int $selected_page = null): array |
||
234 | |||
235 | /** |
||
236 | * Generate the keyboard with the correctly labelled buttons. |
||
237 | * |
||
238 | * @return array |
||
239 | */ |
||
240 | protected function generateKeyboard(): array |
||
290 | |||
291 | /** |
||
292 | * Get the range of intermediate buttons for the keyboard. |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | protected function generateRange(): array |
||
328 | |||
329 | /** |
||
330 | * Generate the button for the passed page. |
||
331 | * |
||
332 | * @param int $page |
||
333 | * |
||
334 | * @return array |
||
335 | */ |
||
336 | protected function generateButton(int $page): array |
||
343 | |||
344 | /** |
||
345 | * Generate the callback data for the passed page. |
||
346 | * |
||
347 | * @param int $page |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | protected function generateCallbackData(int $page): string |
||
359 | |||
360 | /** |
||
361 | * Get the prepared items for the selected page. |
||
362 | * |
||
363 | * @return array |
||
364 | */ |
||
365 | protected function getPreparedItems(): array |
||
369 | |||
370 | /** |
||
371 | * Get the items offset for the selected page. |
||
372 | * |
||
373 | * @return int |
||
374 | */ |
||
375 | protected function getOffset(): int |
||
379 | |||
380 | /** |
||
381 | * Get the parameters from the callback query. |
||
382 | * |
||
383 | * @todo Possibly make it work for custon formats too? |
||
384 | * |
||
385 | * @param string $data |
||
386 | * |
||
387 | * @return array |
||
388 | */ |
||
389 | public static function getParametersFromCallbackData($data): array |
||
395 | } |
||
396 |