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 int |
||
16 | */ |
||
17 | private $itemsPerPage; |
||
18 | |||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | private $maxButtons = 5; |
||
23 | |||
24 | /** |
||
25 | * @var bool |
||
26 | */ |
||
27 | private $forceButtonCount = false; |
||
28 | |||
29 | /** |
||
30 | * @var int |
||
31 | */ |
||
32 | private $selectedPage; |
||
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 $callbackDataFormat = '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 | * @param int $maxButtons |
||
63 | * @param bool $forceButtonCount |
||
64 | * |
||
65 | * @return $this |
||
66 | * @throws InlineKeyboardPaginationException |
||
67 | */ |
||
68 | public function setMaxButtons(int $maxButtons = 5, bool $forceButtonCount = false): InlineKeyboardPagination |
||
79 | |||
80 | /** |
||
81 | * Get the current callback format. |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | public function getCallbackDataFormat(): string |
||
89 | |||
90 | /** |
||
91 | * Set the callback_data format. |
||
92 | * |
||
93 | * @param string $callbackDataFormat |
||
94 | * |
||
95 | * @return InlineKeyboardPagination |
||
96 | */ |
||
97 | public function setCallbackDataFormat(string $callbackDataFormat): InlineKeyboardPagination |
||
103 | |||
104 | /** |
||
105 | * Return list of keyboard button labels. |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public function getLabels(): array |
||
113 | |||
114 | /** |
||
115 | * Set the keyboard button labels. |
||
116 | * |
||
117 | * @param array $labels |
||
118 | * |
||
119 | * @return InlineKeyboardPagination |
||
120 | */ |
||
121 | public function setLabels(array $labels): InlineKeyboardPagination |
||
127 | |||
128 | /** |
||
129 | * @inheritdoc |
||
130 | */ |
||
131 | public function setCommand(string $command = 'pagination'): InlineKeyboardPagination |
||
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | * @throws InlineKeyboardPaginationException |
||
141 | */ |
||
142 | public function setSelectedPage(int $selectedPage): InlineKeyboardPagination |
||
154 | |||
155 | /** |
||
156 | * Get the number of items shown per page. |
||
157 | * |
||
158 | * @return int |
||
159 | */ |
||
160 | public function getItemsPerPage(): int |
||
164 | |||
165 | /** |
||
166 | * Set how many items should be shown per page. |
||
167 | * |
||
168 | * @param int $itemsPerPage |
||
169 | * |
||
170 | * @return InlineKeyboardPagination |
||
171 | * @throws InlineKeyboardPaginationException |
||
172 | */ |
||
173 | public function setItemsPerPage(int $itemsPerPage): InlineKeyboardPagination |
||
182 | |||
183 | /** |
||
184 | * Set the items for the pagination. |
||
185 | * |
||
186 | * @param array $items |
||
187 | * |
||
188 | * @return InlineKeyboardPagination |
||
189 | * @throws InlineKeyboardPaginationException |
||
190 | */ |
||
191 | public function setItems(array $items): InlineKeyboardPagination |
||
201 | |||
202 | /** |
||
203 | * Calculate and return the number of pages. |
||
204 | * |
||
205 | * @return int |
||
206 | */ |
||
207 | public function getNumberOfPages(): int |
||
211 | |||
212 | /** |
||
213 | * TelegramBotPagination constructor. |
||
214 | * |
||
215 | * @inheritdoc |
||
216 | * @throws InlineKeyboardPaginationException |
||
217 | */ |
||
218 | public function __construct( |
||
229 | |||
230 | /** |
||
231 | * @inheritdoc |
||
232 | * @throws InlineKeyboardPaginationException |
||
233 | */ |
||
234 | public function getPagination( |
||
246 | |||
247 | /** |
||
248 | * Generate the keyboard with the correctly labelled buttons. |
||
249 | * |
||
250 | * @return array |
||
251 | */ |
||
252 | protected function generateKeyboard(): array |
||
302 | |||
303 | /** |
||
304 | * Get the range of intermediate buttons for the keyboard. |
||
305 | * |
||
306 | * @return array |
||
307 | */ |
||
308 | protected function generateRange(): array |
||
342 | |||
343 | /** |
||
344 | * Generate the button for the passed page. |
||
345 | * |
||
346 | * @param int $page |
||
347 | * |
||
348 | * @return array |
||
349 | */ |
||
350 | protected function generateButton( |
||
358 | |||
359 | /** |
||
360 | * Generate the callback data for the passed page. |
||
361 | * |
||
362 | * @param int $page |
||
363 | * |
||
364 | * @return string |
||
365 | */ |
||
366 | protected function generateCallbackData(int $page): string |
||
374 | |||
375 | /** |
||
376 | * Get the prepared items for the selected page. |
||
377 | * |
||
378 | * @return array |
||
379 | */ |
||
380 | protected function getPreparedItems(): array |
||
384 | |||
385 | /** |
||
386 | * Get the items offset for the selected page. |
||
387 | * |
||
388 | * @return int |
||
389 | */ |
||
390 | protected function getOffset(): int |
||
394 | |||
395 | /** |
||
396 | * Get the parameters from the callback query. |
||
397 | * |
||
398 | * @param string $data |
||
399 | * |
||
400 | * @return array |
||
401 | * @todo Possibly make it work for custom formats too? |
||
402 | */ |
||
403 | public static function getParametersFromCallbackData(string $data): array |
||
409 | } |
||
410 |