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 integer |
||
| 26 | */ |
||
| 27 | private $selected_page; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private $items; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $command; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | private $labels = [ |
||
| 43 | 'default' => '%d', |
||
| 44 | 'first' => '« %d', |
||
| 45 | 'previous' => '‹ %d', |
||
| 46 | 'current' => '· %d ·', |
||
| 47 | 'next' => '%d ›', |
||
| 48 | 'last' => '%d »', |
||
| 49 | ]; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @inheritdoc |
||
| 53 | * @throws InlineKeyboardPaginationException |
||
| 54 | */ |
||
| 55 | public function setMaxButtons(int $max_buttons = 5): InlineKeyboardPagination |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Return list of keyboard button labels. |
||
| 67 | * |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | public function getLabels(): array |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Set the keyboard button labels. |
||
| 77 | * |
||
| 78 | * @param array $labels |
||
| 79 | * |
||
| 80 | * @return InlineKeyboardPagination |
||
| 81 | */ |
||
| 82 | public function setLabels($labels): InlineKeyboardPagination |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @inheritdoc |
||
| 91 | */ |
||
| 92 | public function setCommand(string $command = 'pagination'): InlineKeyboardPagination |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @inheritdoc |
||
| 101 | * @throws InlineKeyboardPaginationException |
||
| 102 | */ |
||
| 103 | public function setSelectedPage(int $selected_page): InlineKeyboardPagination |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return int |
||
| 116 | */ |
||
| 117 | public function getItemsPerPage(): int |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param int $items_per_page |
||
| 124 | * |
||
| 125 | * @return InlineKeyboardPagination |
||
| 126 | * @throws InlineKeyboardPaginationException |
||
| 127 | */ |
||
| 128 | public function setItemsPerPage($items_per_page): InlineKeyboardPagination |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param array $items |
||
| 140 | * |
||
| 141 | * @return InlineKeyboardPagination |
||
| 142 | * @throws InlineKeyboardPaginationException |
||
| 143 | */ |
||
| 144 | public function setItems(array $items): InlineKeyboardPagination |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Calculate and return the number of pages. |
||
| 156 | * |
||
| 157 | * @return int |
||
| 158 | */ |
||
| 159 | public function getNumberOfPages(): int |
||
| 163 | |||
| 164 | /** |
||
| 165 | * TelegramBotPagination constructor. |
||
| 166 | * |
||
| 167 | * @inheritdoc |
||
| 168 | * @throws InlineKeyboardPaginationException |
||
| 169 | */ |
||
| 170 | public function __construct(array $items, string $command = 'pagination', int $selected_page = 1, int $items_per_page = 5) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @inheritdoc |
||
| 180 | * @throws InlineKeyboardPaginationException |
||
| 181 | */ |
||
| 182 | public function getPagination(int $selected_page = null): array |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Generate the keyboard with the correctly labelled buttons. |
||
| 196 | * |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | protected function generateKeyboard(): array |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get the range of intermediate buttons for the keyboard. |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | protected function generateRange(): array |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Generate the button for the passed page. |
||
| 284 | * |
||
| 285 | * @param int $page |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | protected function generateButton(int $page): array |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Generate the callback data for the passed page. |
||
| 299 | * |
||
| 300 | * @param int $page |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | protected function generateCallbackData(int $page): string |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get the prepared items for the selected page. |
||
| 311 | * |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | protected function getPreparedItems(): array |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return int |
||
| 321 | */ |
||
| 322 | protected function getOffset(): int |
||
| 326 | } |
||
| 327 |