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 |
||
29 | trait Send |
||
30 | { |
||
31 | abstract protected function execRequest(string $url); |
||
32 | |||
33 | abstract protected function processRequest(string $method, string $class, $file); |
||
34 | |||
35 | abstract protected function checkCurrentFile(TelegramFile $file); |
||
36 | |||
37 | /** @internal |
||
38 | * \brief Contains parameters of the next request. */ |
||
39 | protected $parameters; |
||
40 | |||
41 | /** @internal |
||
42 | * \brief Represents the provider token for payments. */ |
||
43 | protected $_provider_token; |
||
44 | |||
45 | /** @internal |
||
46 | * \brief Represents currency used for payments. */ |
||
47 | protected $_payment_currency; |
||
48 | |||
49 | /** |
||
50 | * \addtogroup Api Api Methods |
||
51 | * @{ |
||
52 | */ |
||
53 | |||
54 | /** |
||
55 | * \brief Set data for bot payments used across 'sendInvoice'. |
||
56 | * @param string $provider_token The token for the payment provider got using BotFather. |
||
57 | * @param string $currency The payment currency (represented with 'ISO 4217 currency mode'). |
||
58 | */ |
||
59 | public function setPayment(string $provider_token, string $currency = 'EUR') |
||
64 | |||
65 | /** |
||
66 | * \brief Set currency for bot payments. |
||
67 | * \details It's used in place of 'setPayment' in order to specify only the currency. |
||
68 | * @param string $currency The payment currency (represented with 'ISO 4217 currency mode'). |
||
69 | */ |
||
70 | public function setPaymentCurrency(string $currency = 'EUR') |
||
74 | |||
75 | /** |
||
76 | * \brief Send an invoice. |
||
77 | * \details Send an invoice in order receive real money. [API reference](https://core.telegram.org/bots/api#sendinvoice). |
||
78 | * @param $title The title of product or service to pay (e.g. Free Donation to Telegram). |
||
79 | * @param $description A description of product or service to pay. |
||
80 | * @param $payload Bot-defined invoice payload. |
||
81 | * @param $start_parameter Unique deep-linking parameter used to generate this invoice. |
||
82 | * @param $prices The various prices to pay (e.g array('Donation' => 14.50, 'Taxes' => 0.50)). |
||
83 | * @return string The payload for that specific invoice. |
||
84 | * @return Message|false message sent on success, false otherwise. |
||
85 | */ |
||
86 | public function sendInvoice(string $title, string $description, string $start_parameter, $prices, $optionals = []) |
||
126 | |||
127 | /** |
||
128 | * \brief Generate a secure and unique payload string. |
||
129 | * @return string The generated payload. |
||
130 | */ |
||
131 | private function generateSecurePayload() |
||
135 | |||
136 | /** |
||
137 | * \brief Convert a matrix of prices in a JSON string object accepted by 'sendInvoice'. |
||
138 | * @param $prices The matrix of prices. |
||
139 | * @return string The JSON string response. |
||
140 | */ |
||
141 | 1 | private function generateLabeledPrices(array $prices) |
|
160 | |||
161 | /** |
||
162 | * \brief Send a text message. |
||
163 | * \details Use this method to send text messages. [API reference](https://core.telegram.org/bots/api#sendmessage) |
||
164 | * @param $text Text of the message. |
||
165 | * @param $reply_markup <i>Optional</i>. Reply_markup of the message. |
||
166 | * @param $parse_mode <i>Optional</i>. Parse mode of the message. |
||
167 | * @param $disable_web_preview <i>Optional</i>. Disables link previews for links in this message. |
||
168 | * @param $disable_notification <i>Optional</i>. Sends the message silently. |
||
169 | * @return Message|false Message sent on success, false otherwise. |
||
170 | */ |
||
171 | 9 | 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) |
|
185 | |||
186 | /** |
||
187 | * \brief Forward a message. |
||
188 | * \details Use this method to forward messages of any kind. [API reference](https://core.telegram.org/bots/api#forwardmessage) |
||
189 | * @param $from_chat_id The chat where the original message was sent. |
||
190 | * @param $message_id Message identifier (id). |
||
191 | * @param $disable_notification <i>Optional</i>. Sends the message silently. |
||
192 | * @return Message|false Message sent on success, false otherwise. |
||
193 | */ |
||
194 | public function forwardMessage($from_chat_id, int $message_id, bool $disable_notification = false) |
||
205 | |||
206 | /** |
||
207 | * \brief Send a photo. |
||
208 | * \details Use this method to send photos. [API reference](https://core.telegram.org/bots/api#sendphoto) |
||
209 | * @param $photo Photo to send, can be a file_id or a string referencing the location of that image(both local or remote path). |
||
210 | * @param $reply_markup <i>Optional</i>. Reply markup of the message. |
||
211 | * @param $caption <i>Optional</i>. Photo caption (may also be used when resending photos by file_id), 0-200 characters. |
||
212 | * @param $disable_notification <i>Optional<i>. Sends the message silently. |
||
213 | * @return Message|false Message sent on success, false otherwise. |
||
214 | */ |
||
215 | 4 | View Code Duplication | public function sendPhoto(&$photo, string $reply_markup = null, string $caption = '', bool $disable_notification = false) |
228 | |||
229 | /** |
||
230 | * \brief Send an audio. |
||
231 | * \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) |
||
232 | * Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future. |
||
233 | * @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 give the local path of an audio to upload. |
||
234 | * @param $caption <i>Optional</i>. Audio caption, 0-200 characters. |
||
235 | * @param $reply_markup <i>Optional</i>. Reply markup of the message. |
||
236 | * @param $duration <i>Optional</i>. Duration of the audio in seconds. |
||
237 | * @param $performer <i>Optional</i>. Performer. |
||
238 | * @param $title <i>Optional</i>. Track name. |
||
239 | * @param $disable_notification <i>Optional</i>. Sends the message silently. |
||
240 | * @param $reply_to_message_id <i>Optional</i>. If the message is a reply, ID of the original message. |
||
241 | * @return Message|false Message sent on success, false otherwise. |
||
242 | */ |
||
243 | 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) |
||
260 | |||
261 | /** |
||
262 | * \brief Send a document. |
||
263 | * \details Use this method to send general files. [API reference](https://core.telegram.org/bots/api/#senddocument) |
||
264 | * @param string $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 give the local path of an document to upload it. |
||
265 | * Only some document are allowed through url sending (this is a Telegram limitation). |
||
266 | * @param string $caption <i>Optional</i>. Document caption (may also be used when resending documents by file_id), 0-200 characters. |
||
267 | * |
||
268 | * @param string $reply_markup <i>Optional</i>. Reply markup of the message. |
||
269 | * @param bool $disable_notification <i>Optional</i>. Sends the message silently. |
||
270 | * @param int $reply_to_message_id <i>Optional</i>. If the message is a reply, ID of the original message. |
||
271 | * @return Message|false Message sent on success, false otherwise. |
||
272 | */ |
||
273 | 3 | View Code Duplication | public function sendDocument(string $document, string $caption = '', string $reply_markup = null, bool $disable_notification = false, int $reply_to_message_id = null) |
287 | |||
288 | |||
289 | /** |
||
290 | * \brief Send a sticker |
||
291 | * \details Use this method to send .webp stickers. [API reference](https://core.telegram.org/bots/api/#sendsticker) |
||
292 | * @param mixed $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. |
||
293 | * @param string $reply_markup <i>Optional</i>. Reply markup of the message. |
||
294 | * @param bool $disable_notification Sends the message silently. |
||
295 | * @param int $reply_to_message_id <i>Optional</i>. If the message is a reply, ID of the original message. |
||
296 | * @param bool On success, the sent message. |
||
297 | * @return Message|false Message sent on success, false otherwise. |
||
298 | */ |
||
299 | View Code Duplication | public function sendSticker($sticker, string $reply_markup = null, bool $disable_notification = false, int $reply_to_message_id = null) |
|
311 | |||
312 | /** |
||
313 | * \brief Send audio files. |
||
314 | * \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 |
||
315 | * Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future. |
||
316 | * @param mixed $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 the local path of a voice to upload. |
||
317 | * @param string $caption <i>Optional</i>. Voice message caption, 0-200 characters |
||
318 | * @param int $duration <i>Optional</i>. Duration of the voice message in seconds |
||
319 | * @param string $reply_markup <i>Optional</i>. Reply markup of the message. |
||
320 | * @param bool $disable_notification <i>Optional</i>. Sends the message silently. |
||
321 | * @param int $reply_to_message_id <i>Optional</i>. If the message is a reply, ID of the original message. |
||
322 | * @return Message|false Message sent on success, false otherwise. |
||
323 | */ |
||
324 | public function sendVoice($voice, string $caption, int $duration, string $reply_markup = null, bool $disable_notification, int $reply_to_message_id = 0) |
||
339 | |||
340 | /** |
||
341 | * \brief Say the user what action bot's going to do. |
||
342 | * \details Use this method when you need to tell the user that something is happening on the bot's side. |
||
343 | * The status is set for 5 seconds or less (when a message arrives from your bot, |
||
344 | * Telegram clients clear its typing status). [API reference](https://core.telegram.org/bots/api#sendchataction) |
||
345 | * |
||
346 | * @param string $action Type of action to broadcast. Choose one, depending on what the user is about to receive: |
||
347 | * - <code>typing</code> for text messages |
||
348 | * - <code>upload_photo</code> for photos |
||
349 | * - <code>record_video</code> or <code>upload_video</code> for videos |
||
350 | * - <code>record_audio</code> or <code>upload_audio</code> for audio files |
||
351 | * - <code>upload_document</code> for general files |
||
352 | * - <code>find_location</code> for location data |
||
353 | * @return bool True on success. |
||
354 | */ |
||
355 | public function sendChatAction(string $action) : bool |
||
365 | |||
366 | /** @} */ |
||
367 | } |
||
368 |
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: