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 |
||
21 | trait LongPolling |
||
22 | { |
||
23 | |||
24 | abstract public function getUpdates(int $offset = 0, int $limit = 100, int $timeout = 60); |
||
25 | |||
26 | abstract protected function initCommands(); |
||
27 | |||
28 | /** |
||
29 | * \addtogroup Bot |
||
30 | * @{ |
||
31 | */ |
||
32 | |||
33 | /** Redis connection. */ |
||
34 | public $redis; |
||
35 | |||
36 | /** Pdo connection to the database. */ |
||
37 | public $pdo; |
||
38 | |||
39 | /** |
||
40 | * \addtogroup LongPollingDatabase Long polling With Database |
||
41 | * \brief Use getUpdates saving and getting offset in redis/sql-database. |
||
42 | * @{ |
||
43 | */ |
||
44 | |||
45 | /** |
||
46 | * \brief (<i>Internal</i>)Get first update offset in redis. |
||
47 | * \details Called by getUpdatesRedis to get the offset saved in redis or to get it from telegram and save it in redis. |
||
48 | * @param $offset_key Name of the variable where the offset is saved on Redis |
||
49 | * @return Id of the first update to process. |
||
50 | */ |
||
51 | protected function getUpdateOffsetRedis(string $offset_key) : int |
||
72 | |||
73 | /** |
||
74 | * \brief Get updates received by the bot, using redis to save and get the last offset. |
||
75 | * \details It check if an offset exists on redis, then get it, or call getUpdates to set it. |
||
76 | * Then it start an infinite loop where it process updates and update the offset on redis. |
||
77 | * Each update is surrounded by a try/catch. |
||
78 | * @see getUpdates |
||
79 | * @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
||
80 | * @param $timeout <i>Optional</i>. Timeout in seconds for long polling. |
||
81 | * @param $offset_key <i>Optional</i>. Name of the variable where the offset is saved on Redis |
||
82 | */ |
||
83 | public function getUpdatesRedis(int $limit = 100, int $timeout = 60, string $offset_key = 'offset') |
||
112 | |||
113 | /** |
||
114 | * \brief (<i>Internal</i>) Get first update offset in database. |
||
115 | * \details Called by getUpdatesDatabase to get the offset saved in database or to get it from telegram and save it in database. |
||
116 | * @param $table_name Name of the table where offset is saved in the database |
||
117 | * @param $column_name Name of the column where the offset is saved in the database |
||
118 | * @return Id of the first update to process. |
||
119 | */ |
||
120 | protected function getUpdateOffsetDatabase(string $table_name, string $column_name) : int |
||
147 | |||
148 | /** |
||
149 | * \brief Get updates received by the bot, using the sql database to store and get the last offset. |
||
150 | * \details It check if an offset exists on redis, then get it, or call getUpdates to set it. |
||
151 | * Then it start an infinite loop where it process updates and update the offset on redis. |
||
152 | * Each update is surrounded by a try/catch. |
||
153 | * @see getUpdates |
||
154 | * @param $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
||
155 | * @param $timeout <i>Optional</i>. Timeout in seconds for long polling. |
||
156 | * @param $table_name <i>Optional</i>. Name of the table where offset is saved in the database |
||
157 | * @param $column_name <i>Optional</i>. Name of the column where the offset is saved in the database |
||
158 | */ |
||
159 | public function getUpdatesDatabase(int $limit = 100, int $timeout = 0, string $table_name = 'telegram', string $column_name = 'bot_offset') |
||
190 | |||
191 | /** @} */ |
||
192 | |||
193 | /** @} */ |
||
194 | } |
||
195 |
This check looks for function calls that miss required arguments.