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 |
||
5 | trait LongPolling { |
||
6 | |||
7 | /** |
||
8 | * \brief Get updates received by the bot, using redis to save and get the last offset. |
||
9 | * \details It check if an offset exists on redis, then get it, or call getUpdates to set it. |
||
10 | * Then it start an infinite loop where it process updates and update the offset on redis. |
||
11 | * Each update is surrounded by a try/catch. |
||
12 | * @see getUpdates |
||
13 | * @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
||
14 | * @param $timeout <i>Optional</i>. Timeout in seconds for long polling. |
||
15 | * @param $offset_key <i>Optional</i>. Name of the variable where the offset is saved on Redis |
||
16 | */ |
||
17 | public function getUpdatesRedis(int $limit = 100, int $timeout = 60, string $offset_key = 'offset') { |
||
76 | |||
77 | /** |
||
78 | * \brief Get updates received by the bot, and hold the offset in $offset. |
||
79 | * \details Get the update_id of the first update to parse, set it in $offset and |
||
80 | * then it start an infinite loop where it processes updates and keep $offset on the update_id of the last update received. |
||
81 | * Each processUpdate() method call is surrounded by a try/catch. |
||
82 | * @see getUpdates |
||
83 | * @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
||
84 | * @param $timeout <i>Optional</i>. Timeout in seconds for long polling. |
||
85 | */ |
||
86 | public function getUpdatesLocal(int $limit = 100, int $timeout = 60) { |
||
140 | |||
141 | /** |
||
142 | * \brief Get updates received by the bot, using the sql database to store and get the last offset. |
||
143 | * \details It check if an offset exists on redis, then get it, or call getUpdates to set it. |
||
144 | * Then it start an infinite loop where it process updates and update the offset on redis. |
||
145 | * Each update is surrounded by a try/catch. |
||
146 | * @see getUpdates |
||
147 | * @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
||
148 | * @param $timeout <i>Optional</i>. Timeout in seconds for long polling. |
||
149 | * @param $table_name <i>Optional</i>. Name of the table where offset is saved in the database |
||
150 | * @param $column_name <i>Optional</i>. Name of the column where the offset is saved in the database |
||
151 | */ |
||
152 | public function getUpdatesDatabase(int $limit = 100, int $timeout = 0, string $table_name = 'telegram', string $column_name = 'bot_offset') { |
||
220 | |||
221 | } |
||
222 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.