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 | * @return int |
||
| 151 | */ |
||
| 152 | public function getItemsPerPage(): int |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param int $items_per_page |
||
| 159 | * |
||
| 160 | * @return InlineKeyboardPagination |
||
| 161 | * @throws InlineKeyboardPaginationException |
||
| 162 | */ |
||
| 163 | public function setItemsPerPage($items_per_page): InlineKeyboardPagination |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param array $items |
||
| 175 | * |
||
| 176 | * @return InlineKeyboardPagination |
||
| 177 | * @throws InlineKeyboardPaginationException |
||
| 178 | */ |
||
| 179 | public function setItems(array $items): InlineKeyboardPagination |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Calculate and return the number of pages. |
||
| 191 | * |
||
| 192 | * @return int |
||
| 193 | */ |
||
| 194 | public function getNumberOfPages(): int |
||
| 198 | |||
| 199 | /** |
||
| 200 | * TelegramBotPagination constructor. |
||
| 201 | * |
||
| 202 | * @inheritdoc |
||
| 203 | * @throws InlineKeyboardPaginationException |
||
| 204 | */ |
||
| 205 | public function __construct(array $items, string $command = 'pagination', int $selected_page = 1, int $items_per_page = 5) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @inheritdoc |
||
| 215 | * @throws InlineKeyboardPaginationException |
||
| 216 | */ |
||
| 217 | public function getPagination(int $selected_page = null): array |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Generate the keyboard with the correctly labelled buttons. |
||
| 231 | * |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | protected function generateKeyboard(): array |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get the range of intermediate buttons for the keyboard. |
||
| 287 | * |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | protected function generateRange(): array |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Generate the button for the passed page. |
||
| 325 | * |
||
| 326 | * @param int $page |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | protected function generateButton(int $page): array |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Generate the callback data for the passed page. |
||
| 340 | * |
||
| 341 | * @param int $page |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | protected function generateCallbackData(int $page): string |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Get the prepared items for the selected page. |
||
| 356 | * |
||
| 357 | * @return array |
||
| 358 | */ |
||
| 359 | protected function getPreparedItems(): array |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return int |
||
| 366 | */ |
||
| 367 | protected function getOffset(): int |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get the parameters from the callback query. |
||
| 374 | * |
||
| 375 | * @param string $data |
||
| 376 | * |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | public static function getParametersFromCallbackData($data): array |
||
| 385 | } |
||
| 386 |