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 Inline |
||
28 | { |
||
29 | abstract protected function execRequest(string $url); |
||
30 | |||
31 | /** @internal |
||
32 | * \brief Store ID of the callback query received. */ |
||
33 | protected $_callback_query_id; |
||
34 | |||
35 | /** @internal |
||
36 | * \brief Store ID of the PreCheckout query received. */ |
||
37 | protected $_pre_checkout_query_id; |
||
38 | |||
39 | /** @internal |
||
40 | * \brief Store ID of the Shipping query received. */ |
||
41 | protected $_shipping_query_id; |
||
42 | |||
43 | /** @internal |
||
44 | * \brief Store ID of the inline query received. */ |
||
45 | protected $_inline_query_id; |
||
46 | |||
47 | /** |
||
48 | * \addtogroup Api API Methods |
||
49 | * @{ |
||
50 | */ |
||
51 | |||
52 | /** \brief Answer a callback query. |
||
53 | * \details Remove the 'updating' circle icon on an inline keyboard button showing a message/alert to the user. |
||
54 | * It'll always answer the current callback query. [Api reference](https://core.telegram.org/bots/api#answercallbackquery) |
||
55 | * @param string $text <i>Optional</i>. Text of the notification. If not specified, nothing will be shown to the user, 0-200 characters. |
||
56 | * @param bool $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. |
||
57 | * @param string $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. |
||
58 | * Otherwise, you may use links like telegram.me/your_bot?start=XXXX that open your bot with a parameter. |
||
59 | * @return bool True on success. |
||
60 | */ |
||
61 | public function answerCallbackQuery($text = '', $show_alert = false, string $url = '') : bool |
||
76 | |||
77 | /** |
||
78 | * \brief Send an update once the user has confirmed their payment. |
||
79 | * @param bool $ok True is everything is alright, elsewhere False. |
||
80 | * @param string $error The error message if something went wrong. |
||
81 | * @return bool True on success. |
||
82 | */ |
||
83 | View Code Duplication | public function answerPreCheckoutQuery(bool $ok = true, string $error = null) { |
|
96 | |||
97 | /** |
||
98 | * \brief Send an update once the user has confirmed their shipping details. |
||
99 | * @param bool $ok True is everything is alright, elsewhere False. |
||
100 | * @param string $error The error message if something went wrong. |
||
101 | * @param array $shipping_options The various shipping options available. |
||
102 | * For example: array('FedEx' => ['Dispatch' => 15.90, 'Italy Taxes' => 2.50]) |
||
103 | * @return bool True on success. |
||
104 | */ |
||
105 | View Code Duplication | public function answerShippingQuery(bool $ok = true, string $error = null, array $shipping_options) { |
|
119 | |||
120 | /** |
||
121 | * \brief Generate shipping options to pass to 'answerShippingQuery'. |
||
122 | * @param array $shipping_options The various shipping options represented like PHP data types. |
||
123 | * @return string A JSON string representation of the shipping options. |
||
124 | */ |
||
125 | 1 | private function generateShippingOptions(array $shipping_options) { |
|
151 | |||
152 | /** |
||
153 | * \brief Answer a inline query (when the user write @botusername "Query") with a button, that will make user switch to the |
||
154 | * private chat with the bot, on the top of the results. [Api reference](https://core.telegram.org/bots/api#answerinlinequery) |
||
155 | * @param string $results A JSON-serialized array of results for the inline query. Use PhpBotFramework\Entities\InlineQueryResult class to create and get them. |
||
156 | * @param string $switch_pm_text If passed, clients will display a button with specified text that switches the user to a private chat with the bot and sends the bot a start message with the parameter $switch_pm_parameter. |
||
157 | * @param bool $is_personal Pass True, if results may be cached on the server side only for the user that sent the query. By default, results may be returned to any user who sends the same query. |
||
158 | * @param int $cache_time The maximum amount of time in seconds that the result of the inline query may be cached on the server. Defaults to 300. |
||
159 | * @return bool True on success. |
||
160 | */ |
||
161 | public function answerInlineQuery(string $results = '', string $switch_pm_text = '', $switch_pm_parameter = '', bool $is_personal = true, int $cache_time = 300) : bool |
||
181 | |||
182 | /** @} */ |
||
183 | } |
||
184 |
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.