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:
1 | <?php |
||
18 | class InlineKeyboard extends Keyboard |
||
19 | { |
||
20 | /** |
||
21 | * Get an inline pagination keyboard. |
||
22 | * |
||
23 | * - $callback_data is an ID for the CallbackqueryCommand, to know where the request comes from. |
||
24 | * The ID is automatically appended with '_page_%d' to filter out the selected page number. |
||
25 | * |
||
26 | * - $labels allows for custom button labels, using '%d' placeholders. |
||
27 | * Default: |
||
28 | * ``` |
||
29 | * [ |
||
30 | * 'first' => '« %d', |
||
31 | * 'previous' => '‹ %d', |
||
32 | * 'current' => '· %d ·', |
||
33 | * 'next' => '%d ›', |
||
34 | * 'last' => '%d »', |
||
35 | * ] |
||
36 | * `` |
||
37 | *` |
||
38 | * initial idea from: https://stackoverflow.com/a/42879866 |
||
39 | * |
||
40 | * @param string $callback_data |
||
41 | * @param int $current_page |
||
42 | * @param int $max_pages |
||
43 | * @param array $labels |
||
44 | * |
||
45 | * @return \Longman\TelegramBot\Entities\InlineKeyboard |
||
46 | */ |
||
47 | 1 | public static function getPagination($callback_data, $current_page, $max_pages, array $labels = []) |
|
100 | |||
101 | /** |
||
102 | * Extract the page number from the passed callback data. |
||
103 | * |
||
104 | * @param string $callback_data |
||
105 | * |
||
106 | * @return int |
||
107 | */ |
||
108 | public static function getPageFromCallbackData($callback_data) |
||
112 | } |
||
113 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.