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

Edit   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 75
Duplicated Lines 18.67 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 14
loc 75
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A editMessageText() 14 14 1
A editInlineMessageText() 0 13 1
A editMessageReplyMarkup() 0 11 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 Edit {
6
7
    /**
8
     * \addtogroup Api Api Methods
9
     * @{
10
     */
11
12
    /**
13
     * \brief Edit text of a message sent by the bot.
14
     * \details Use this method to edit text and game messages sent by the bot. [Api reference](https://core.telegram.org/bots/api#editmessagetext)
15
     * @param $message_id Unique identifier of the sent message.
16
     * @param $text New text of the message.
17
     * @param $reply_markup Reply markup of the message will have (will be removed if this is null).
18
     * @param $parse_mode <i>Optional</i>. Send Markdown or HTML.
19
     * @param $disable_web_preview <i>Optional</i>. Disables link previews for links in this message.
20
     */
21 View Code Duplication
    public function editMessageText(int $message_id, $text, $reply_markup = null, string $parse_mode = 'HTML', bool $disable_web_preview = true) {
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...
22
23
        $parameters = [
24
            '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...
25
            'message_id' => $message_id,
26
            'text' => $text,
27
            'reply_markup' => $reply_markup,
28
            'parse_mode' => $parse_mode,
29
            'disable_web_page_preview' => $disable_web_preview,
30
        ];
31
32
        return $this->exec_curl_request('editMessageText?' . 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...
33
34
    }
35
36
    /**
37
     * \brief Edit text of a message sent via the bot.
38
     * \details Use this method to edit text messages sent via the bot (for inline queries). [Api reference](https://core.telegram.org/bots/api#editmessagetext)
39
     * @param $inline_message_id  Identifier of the inline message.
40
     * @param $text New text of the message.
41
     * @param $reply_markup Reply markup of the message will have (will be removed if this is null).
42
     * @param $parse_mode <i>Optional</i>. Send Markdown or HTML.
43
     * @param $disable_web_preview <i>Optional</i>. Disables link previews for links in this message.
44
     */
45
    public function editInlineMessageText(string $inline_message_id, $text, string $reply_markup = null, string $parse_mode = 'HTML', bool $disable_web_preview = false) {
46
47
        $parameters = [
48
            'inline_message_id' => $inline_message_id,
49
            'text' => $text,
50
            'reply_markup' => $reply_markup,
51
            'parse_mode' => $parse_mode,
52
            'disable_web_page_preview' => $disable_web_preview,
53
        ];
54
55
        return $this->exec_curl_request('editMessageText?' . 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
     * Edit only the inline keyboard of a message (https://core.telegram.org/bots/api#editmessagereplymarkup)ù
61
     * @param
62
     * $message_id Identifier of the message to edit
63
     * $inline_keyboard Inlike keyboard array (https://core.telegram.org/bots/api#inlinekeyboardmarkup)
64
     */
65
    public function editMessageReplyMarkup($message_id, $inline_keyboard) {
66
67
        $parameters = [
68
            'chat_id' => $this->_chat_id,
69
            'message_id' => $message_id,
70
            'reply_markup' => $inline_keyboard,
71
        ];
72
73
        return $this->exec_curl_request('editMessageReplyMarkup?' . 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...
74
75
    }
76
77
    /** @} */
78
79
}
80