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 Send { |
||
6 | |||
7 | /** |
||
8 | * \addtogroup Api Api Methods |
||
9 | * @{ |
||
10 | */ |
||
11 | |||
12 | /** |
||
13 | * \brief Send a text message. |
||
14 | * \details Use this method to send text messages. [Api reference](https://core.telegram.org/bots/api#sendmessage) |
||
15 | * @param $text Text of the message. |
||
16 | * @param $reply_markup <i>Optional</i>. Reply_markup of the message. |
||
17 | * @param $parse_mode <i>Optional</i>. Parse mode of the message. |
||
18 | * @param $disable_web_preview <i>Optional</i>. Disables link previews for links in this message. |
||
19 | * @param $disable_notification <i>Optional</i>. Sends the message silently. |
||
20 | * @return On success, the sent message. |
||
21 | */ |
||
22 | public function sendMessage($text, string $reply_markup = null, int $reply_to = null, string $parse_mode = 'HTML', bool $disable_web_preview = true, bool $disable_notification = false) { |
||
37 | |||
38 | /** |
||
39 | * \brief Forward a message. |
||
40 | * \details Use this method to forward messages of any kind. [Api reference](https://core.telegram.org/bots/api#forwardmessage) |
||
41 | * @param $from_chat_id The chat where the original message was sent. |
||
42 | * @param $message_id Message identifier (id). |
||
43 | * @param $disable_notification <i>Optional</i>. Sends the message silently. |
||
44 | * @return On success, the sent message. |
||
45 | */ |
||
46 | public function forwardMessage($from_chat_id, int $message_id, bool $disable_notification = false) { |
||
58 | |||
59 | /** |
||
60 | * \brief Send a photo. |
||
61 | * \details Use this method to send photos. [Api reference](https://core.telegram.org/bots/api#sendphoto) |
||
62 | * @param $photo Photo to send, can be a file_id or a string referencing the location of that image. |
||
63 | * @param $reply_markup <i>Optional</i>. Reply markup of the message. |
||
64 | * @param $caption <i>Optional</i>. Photo caption (may also be used when resending photos by file_id), 0-200 characters. |
||
65 | * @param $disable_notification <i>Optional<i>. Sends the message silently. |
||
66 | * @return On success, the sent message. |
||
67 | */ |
||
68 | public function sendPhoto($photo, string $reply_markup = null, string $caption = '', bool $disable_notification = false) { |
||
81 | |||
82 | /** |
||
83 | * \brief Send an audio. |
||
84 | * \details Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .mp3 format. [Api reference](https://core.telegram.org/bots/api/#sendaudio) |
||
85 | * Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future. |
||
86 | * @param $audio Audio file to send. Pass a file_id as String to send an audio file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an audio file from the Internet, or upload a new one using multipart/form-data. |
||
87 | * @param $caption <i>Optional</i>. Audio caption, 0-200 characters. |
||
88 | * @param $reply_markup <i>Optional</i>. Reply markup of the message. |
||
89 | * @param $duration <i>Optional</i>. Duration of the audio in seconds. |
||
90 | * @param $performer <i>Optional</i>. Performer. |
||
91 | * @param $title <i>Optional</i>. Track name. |
||
92 | * @param $disable_notification <i>Optional</i>. Sends the message silently. |
||
93 | * @param $reply_to_message_id <i>Optional</i>. If the message is a reply, ID of the original message. |
||
94 | * @return On success, the sent message. |
||
95 | */ |
||
96 | public function sendAudio($audio, string $caption = null, string $reply_markup = null, int $duration = null, string $performer, string $title = null, bool $disable_notification = false, int $reply_to_message_id = null) { |
||
113 | |||
114 | /** |
||
115 | * \brief Send a document. |
||
116 | * \details Use this method to send general files. [Api reference](https://core.telegram.org/bots/api/#senddocument) |
||
117 | * @param $document File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. |
||
118 | * @param <i>Optional</i>. Document caption (may also be used when resending documents by file_id), 0-200 characters. |
||
119 | * |
||
120 | * @param $reply_markup <i>Optional</i>. Reply markup of the message. |
||
121 | * @param <i>Optional</i>. Sends the message silently. |
||
122 | * @param <i>Optional</i>. If the message is a reply, ID of the original message. |
||
123 | */ |
||
124 | public function sendDocument($document, string $caption = '', string $reply_markup = null, bool $disable_notification = false, int $reply_to_message_id = null) { |
||
138 | |||
139 | |||
140 | /** |
||
141 | * \brief Send a sticker |
||
142 | * \details Use this method to send .webp stickers. [Api reference](https://core.telegram.org/bots/api/#sendsticker) |
||
143 | * @param $sticker Sticker to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a .webp file from the Internet, or upload a new one using multipart/form-data. |
||
144 | * @param $reply_markup <i>Optional</i>. Reply markup of the message. |
||
145 | * @param $disable_notification Sends the message silently. |
||
146 | * @param <i>Optional</i>. If the message is a reply, ID of the original message. |
||
147 | * @param On success, the sent message. |
||
148 | */ |
||
149 | public function sendSticker($sticker, string $reply_markup = null, bool $disable_notification = false, int $reply_to_message_id = null) { |
||
162 | |||
163 | /** |
||
164 | * \brief Send audio files. |
||
165 | * \details Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document).o |
||
166 | * Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future. |
||
167 | * @param $voice Audio file to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. |
||
168 | * @param $caption <i>Optional</i>. Voice message caption, 0-200 characters |
||
169 | * @param $duration <i>Optional</i>. Duration of the voice message in seconds |
||
170 | * @param $reply_markup <i>Optional</i>. Reply markup of the message. |
||
171 | * @param $disable_notification <i>Optional</i>. Sends the message silently. |
||
172 | * @param $reply_to_message_id <i>Optional</i>. If the message is a reply, ID of the original message. |
||
173 | * @return On success, the sent message is returned. |
||
174 | */ |
||
175 | View Code Duplication | public function sendVoice($voice, string $caption, int $duration, string $reply_markup = null, bool $disable_notification, int $reply_to_message_id = 0) { |
|
190 | |||
191 | /** |
||
192 | * \brief Say the user what action is the bot doing. |
||
193 | * \details Use this method when you need to tell the user that something is happening on the bot's side. The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status). [Api reference](https://core.telegram.org/bots/api#sendchataction) |
||
194 | * @param $action Type of action to broadcast. Choose one, depending on what the user is about to receive: |
||
195 | * - <code>typing</code> for text messages |
||
196 | * - <code>upload_photo</code> for photos |
||
197 | * - <code>record_video</code> or <code>upload_video</code> for videos |
||
198 | * - <code>record_audio</code> or <code>upload_audio</code> for audio files |
||
199 | * - <code>upload_document</code> for general files |
||
200 | * - <code>find_location</code> for location data |
||
201 | * @return True on success. |
||
202 | */ |
||
203 | public function sendChatAction(string $action) : bool { |
||
213 | |||
214 | /** @} */ |
||
215 | |||
216 | } |
||
217 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: