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 | * It must contain a '%d' placeholder for the page number. If no placeholder is found, the ID is automatically appended with '_page_%d' |
||
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 = []) |
|
102 | } |
||
103 |
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.