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 Inline { |
||
6 | |||
7 | /** |
||
8 | * \addtogroup Core Core(Internal) |
||
9 | * @{ |
||
10 | */ |
||
11 | |||
12 | /** \brief Store id of the callback query received. */ |
||
13 | protected $_callback_query_id; |
||
14 | |||
15 | /** \brief Store id of the inline query received. */ |
||
16 | protected $_inline_query_id; |
||
17 | |||
18 | /** @} */ |
||
19 | |||
20 | /** |
||
21 | * \addtogroup Api Api Methods |
||
22 | * @{ |
||
23 | */ |
||
24 | |||
25 | /* \brief Answer a callback query |
||
26 | * \details Remove the updating cirle on an inline keyboard button and showing a message/alert to the user. |
||
27 | * It will always answer the current callback query. |
||
28 | * @param $text <i>Optional</i>. Text of the notification. If not specified, nothing will be shown to the user, 0-200 characters. |
||
29 | * @param $show_alert <i>Optional</i>. If true, an alert will be shown by the client instead of a notification at the top of the chat screen. |
||
30 | * @param $url <i>Optional</i>. URL that will be opened by the user's client. If you have created a Game and accepted the conditions via @Botfather, specify the URL that opens your game – note that this will only work if the query comes from a callback_game button. |
||
31 | * Otherwise, you may use links like telegram.me/your_bot?start=XXXX that open your bot with a parameter. |
||
32 | * @return True on success. |
||
33 | */ |
||
34 | public function answerCallbackQuery($text = '', $show_alert = false, string $url = '') : bool { |
||
52 | |||
53 | |||
54 | /* |
||
55 | * Answer a inline query (when the user write @botusername "Query") with a button, that will make user switch to the private chat with the bot, on the top of the results (https://core.telegram.org/bots/api#answerinlinequery) |
||
56 | * @param |
||
57 | * $results Array on InlineQueryResult (https://core.telegram.org/bots/api#inlinequeryresult) |
||
58 | * $switch_pm_text Text to show on the button |
||
59 | */ |
||
60 | View Code Duplication | public function answerInlineQuerySwitchPM($results, $switch_pm_text, $switch_pm_parameter = '', $is_personal = true, $cache_time = 300) { |
|
80 | |||
81 | /* |
||
82 | * Answer a inline query (when the user write @botusername "Query") with a button, that will make user switch to the private chat with the bot, on the top of the results (https://core.telegram.org/bots/api#answerinlinequery) |
||
83 | * without showing any results to the user |
||
84 | * @param |
||
85 | * $switch_pm_text Text to show on the button |
||
86 | */ |
||
87 | View Code Duplication | public function answerEmptyInlineQuerySwitchPM($switch_pm_text, $switch_pm_parameter = '', $is_personal = true, $cache_time = 300) { |
|
106 | |||
107 | /** @} */ |
||
108 | |||
109 | } |
||
110 |
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.