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 |
||
27 | trait LongPolling |
||
28 | { |
||
29 | abstract public function getUpdates(int $offset = 0, int $limit = 100, int $timeout = 60); |
||
30 | |||
31 | abstract protected function initCommands(); |
||
32 | |||
33 | /** |
||
34 | * \addtogroup Database |
||
35 | * @{ |
||
36 | */ |
||
37 | |||
38 | /** |
||
39 | * @internal |
||
40 | * \brief Get first update offset in Redis. |
||
41 | * \details Called by getUpdatesRedis in order to get the saved offset in Redis or retrieve it from Telegram and save it. |
||
42 | * @param string $offset_key Name of the variable where the offset is saved on Redis. |
||
43 | * @return int Id of the first update to process. |
||
44 | */ |
||
45 | protected function getUpdateOffsetRedis(string $offset_key) : int |
||
62 | |||
63 | /** |
||
64 | * \brief Get updates received by the bot, and use Redis to save and get the last offset. |
||
65 | * \details It check if an offset exists on Redis: then get it or call getUpdates to set it. |
||
66 | * Then it start a loop where it process updates and update the offset on Redis. |
||
67 | * Each update is surrounded by a try/catch. |
||
68 | * @see getUpdates |
||
69 | * @param int $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
||
70 | * @param int $timeout <i>Optional</i>. Timeout (in seconds) for long polling. |
||
71 | * @param string $offset_key <i>Optional</i>. Name of the variable where the offset is saved on Redis. |
||
72 | */ |
||
73 | public function getUpdatesRedis(int $limit = 100, int $timeout = 60, string $offset_key = 'offset') |
||
97 | |||
98 | /** |
||
99 | * @internal |
||
100 | * \brief Get first update offset in database. |
||
101 | * \details Called by getUpdatesDatabase to get the offset saved in database. |
||
102 | * If the offset is not saved: it retrieve the offset from Telegram and save it on the database. |
||
103 | * @param string $table_name Name of the table where offset is saved in the database. |
||
104 | * @param string $column_name Name of the column where the offset is saved in the database. |
||
105 | * @return int Id of the first update to process. |
||
106 | */ |
||
107 | protected function getUpdateOffsetDatabase(string $table_name, string $column_name) : int |
||
132 | |||
133 | /** |
||
134 | * \brief Get updates received by the bot, using the SQL database to store and get the last offset. |
||
135 | * \details It check if an offset exists on redis, then get it, or call getUpdates to set it. |
||
136 | * Then it start a loop where it process updates and update the offset on Redis. |
||
137 | * Each update is surrounded by a try/catch. |
||
138 | * @see getUpdates |
||
139 | * @param int $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted. |
||
140 | * @param int $timeout <i>Optional</i>. Timeout (in seconds) for long polling. |
||
141 | * @param string $table_name <i>Optional</i>. Name of the table where offset is saved in the database. |
||
142 | * @param string $column_name <i>Optional</i>. Name of the column where the offset is saved in the database. |
||
143 | */ |
||
144 | public function getUpdatesDatabase(int $limit = 100, int $timeout = 0, string $table_name = 'telegram', string $column_name = 'bot_offset') |
||
174 | |||
175 | /** @} */ |
||
176 | } |
||
177 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.