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

Inline   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 105
Duplicated Lines 37.14 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 39
loc 105
rs 10
c 0
b 0
f 0
wmc 6
lcom 2
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A answerCallbackQuery() 0 18 2
A answerInlineQuerySwitchPM() 20 20 2
A answerEmptyInlineQuerySwitchPM() 19 19 2

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 Inline {
6
7
    /**
8
     * \addtogroup Core Core(Internal)
9
     * @{
10
     */
11
12
    /** \brief Store id of the callback query received. */
13
    protected $_callback_query_id;
14
15
    /** \brief Store id of the inline query received. */
16
    protected $_inline_query_id;
17
18
    /** @} */
19
20
    /**
21
     * \addtogroup Api Api Methods
22
     * @{
23
     */
24
25
    /* \brief Answer a callback query
26
     * \details Remove the updating cirle on an inline keyboard button and showing a message/alert to the user.
27
     * It will always answer the current callback query.
28
     * @param $text <i>Optional</i>. Text of the notification. If not specified, nothing will be shown to the user, 0-200 characters.
29
     * @param $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.
30
     * @param $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.
31
     * Otherwise, you may use links like telegram.me/your_bot?start=XXXX that open your bot with a parameter.
32
     * @return True on success.
33
     */
34
    public function answerCallbackQuery($text = '', $show_alert = false, string $url = '') : bool {
35
36
        if (!isset($this->_callback_query_id)) {
37
38
            throw new BotException("Callback query id not set, wrong update");
39
40
        }
41
42
        $parameters = [
43
            'callback_query_id' => $this->_callback_query_id,
44
            'text' => $text,
45
            'show_alert' => $show_alert,
46
            'url' => $url
47
        ];
48
49
        return $this->exec_curl_request('answerCallbackQuery?' . 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...
50
51
    }
52
53
54
    /*
55
     * Answer a inline query (when the user write @botusername "Query") with a button, that will make user switch to the private chat with the bot, on the top of the results (https://core.telegram.org/bots/api#answerinlinequery)
56
     * @param
57
     * $results Array on InlineQueryResult (https://core.telegram.org/bots/api#inlinequeryresult)
58
     * $switch_pm_text Text to show on the button
59
     */
60 View Code Duplication
    public function answerInlineQuerySwitchPM($results, $switch_pm_text, $switch_pm_parameter = '', $is_personal = true, $cache_time = 300) {
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...
61
62
        if (!isset($this->_inline_query_id)) {
63
64
            throw new BotException("Inline query id not set, wrong update");
65
66
        }
67
68
        $parameters = [
69
            'inline_query_id' => $this->_inline_query_id,
70
            'switch_pm_text' => $switch_pm_text,
71
            'is_personal' => $is_personal,
72
            'switch_pm_parameter' => $switch_pm_parameter,
73
            'results' => $results,
74
            'cache_time' => $cache_time
75
        ];
76
77
        return $this->exec_curl_request('answerInlineQuery?' . 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...
78
79
    }
80
81
    /*
82
     * Answer a inline query (when the user write @botusername "Query") with a button, that will make user switch to the private chat with the bot, on the top of the results (https://core.telegram.org/bots/api#answerinlinequery)
83
     * without showing any results to the user
84
     * @param
85
     * $switch_pm_text Text to show on the button
86
     */
87 View Code Duplication
    public function answerEmptyInlineQuerySwitchPM($switch_pm_text, $switch_pm_parameter = '', $is_personal = true, $cache_time = 300) {
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...
88
89
        if (!isset($this->_inline_query_id)) {
90
91
            throw new BotException("Inline query id not set, wrong update");
92
93
        }
94
95
        $parameters = [
96
            'inline_query_id' => $this->_inline_query_id,
97
            'switch_pm_text' => $switch_pm_text,
98
            'is_personal' => $is_personal,
99
            'switch_pm_parameter' => $switch_pm_parameter,
100
            'cache_time' => $cache_time
101
        ];
102
103
        return $this->exec_curl_request('answerInlineQuery?' . 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...
104
105
    }
106
107
    /** @} */
108
109
}
110