Completed
Push — master ( 23095d...54d687 )
by Danilo
02:12
created

Send   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 212
Duplicated Lines 7.08 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 15
loc 212
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A sendMessage() 0 15 1
A forwardMessage() 0 12 1
A sendPhoto() 0 13 1
A sendAudio() 0 17 1
A sendDocument() 0 14 1
A sendSticker() 0 13 1
A sendVoice() 15 15 1
A sendChatAction() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace PhpBotFramework\Core;
4
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) {
23
24
        $parameters = [
25
            'chat_id' => $this->_chat_id,
0 ignored issues
show
Bug introduced by
The property _chat_id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
            'text' => $text,
27
            'parse_mode' => $parse_mode,
28
            'disable_web_page_preview' => $disable_web_preview,
29
            'reply_markup' => $reply_markup,
30
            'reply_to_message_id' => $reply_to,
31
            'disable_notification' => $disable_notification
32
        ];
33
34
        return $this->exec_curl_request('sendMessage?' . http_build_query($parameters));
0 ignored issues
show
Bug introduced by
It seems like exec_curl_request() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
35
36
    }
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) {
47
48
        $parameters = [
49
            'chat_id' => $this->_chat_id,
50
            'message_id' => $message_id,
51
            'from_chat_id' => $from_chat_id,
52
            'disable_notification' => $disable_notification
53
        ];
54
55
        return $this->exec_curl_request('forwardMessage?' . http_build_query($parameters));
0 ignored issues
show
Bug introduced by
It seems like exec_curl_request() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
56
57
    }
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) {
69
70
        $parameters = [
71
            'chat_id' => $this->_chat_id,
72
            'photo' => $photo,
73
            'caption' => $caption,
74
            'reply_markup' => $reply_markup,
75
            'disable_notification' => $disable_notification,
76
        ];
77
78
        return $this->exec_curl_request('sendPhoto?' . http_build_query($parameters));
0 ignored issues
show
Bug introduced by
It seems like exec_curl_request() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
79
80
    }
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) {
97
98
        $parameters = [
99
            'chat_id' => $this->_chat_id,
100
            'audio' => $audio,
101
            'caption' => $caption,
102
            'duration' => $duration,
103
            'performer' => $performer,
104
            'title' => $title,
105
            'reply_to_message_id' => $reply_to_message_id,
106
            'reply_markup' => $reply_markup,
107
            'disable_notification' => $disable_notification,
108
        ];
109
110
        return $this->exec_curl_request('sendAudio?' . http_build_query($parameters));
0 ignored issues
show
Bug introduced by
It seems like exec_curl_request() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
111
112
    }
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) {
125
126
        $parameters = [
127
            'chat_id' => $this->_chat_id,
128
            'document' => $document,
129
            'caption' => $caption,
130
            'reply_to_message_id' => $reply_to_message_id,
131
            'reply_markup' => $reply_markup,
132
            'disable_notification' => $disable_notification,
133
        ];
134
135
        return $this->exec_curl_request('sendAudio?' . http_build_query($parameters));
0 ignored issues
show
Bug introduced by
It seems like exec_curl_request() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
136
137
    }
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) {
150
151
        $parameters = [
152
            'chat_id' => $this->_chat_id,
153
            'sticker' => $sticker,
154
            'disable_notification' => $disable_notification,
155
            'reply_to_message_id' => $reply_to_message_id,
156
            'reply_markup' => $reply_markup
157
        ];
158
159
        return $this->exec_curl_request('sendSticker?' . http_build_query($parameters));
0 ignored issues
show
Bug introduced by
It seems like exec_curl_request() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
160
161
    }
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
176
177
        $parameters = [
178
            'chat_id' => $this->_chat_id,
179
            'voice' => $voice,
180
            'caption' => $caption,
181
            'duration' => $duration,
182
            'disable_notification', $disable_notification,
183
            'reply_to_message_id' => $reply_to_message_id,
184
            'reply_markup' => $reply_markup
185
        ];
186
187
        return $this->exec_curl_request('sendVoice?' . http_build_query($parameters));
0 ignored issues
show
Bug introduced by
It seems like exec_curl_request() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
188
189
    }
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 {
204
205
        $parameters = [
206
            'chat_id' => $this->_chat_id,
207
            'action' => $action
208
        ];
209
210
        return $this->exec_curl_request('sendChatAction?' . http_build_query($parameters));
0 ignored issues
show
Bug introduced by
It seems like exec_curl_request() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
211
212
    }
213
214
    /** @} */
215
216
}
217