Completed
Push — master ( 3845e2...a95f7d )
by Danilo
09:37
created

Button   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 145
ccs 0
cts 79
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getBackButton() 0 21 2
B getBackSkipKeyboard() 0 24 2
B getChooseLanguageKeyboard() 0 41 4
1
<?php
2
3
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework\Localization;
20
21
use PhpBotFramework\Core\CoreBot;
22
use PhpBotFramework\Entities\InlineKeyboard;
23
24
/** \class Button Button Inline keyboard with localizated buttons. */
25
class Button extends InlineKeyboard
26
{
27
    /**
28
     * \addtogroup InlineKeyboard InlineKeyboard
29
     * \brief Handle an inline keyboard to send along with messages.
30
     * @{
31
     */
32
33
    /** \brief Stores a reference to the bot that's using the inline keyboard. */
34
    protected $bot;
35
36
    /**
37
     * \brief Create an inline keyboard object with localizated buttons.
38
     * @param CoreBot $bot References to the bot that's using the inline keyboard.
39
     * @param array $buttons Buttons passed as inizialization.
40
     */
41
    public function __construct(CoreBot &$bot, array $buttons = array())
42
    {
43
        $this->bot = $bot;
44
45
        // Call parent constructor passing array
46
        parent::__construct($buttons);
47
    }
48
49
50
    /**
51
     * \brief Get a simple Back button with back as <code>callback_data</code>.
52
     * @param $json_serialized return a json serialized string, or an array.
53
     * @return A button with written "back".
54
     */
55
    public function getBackButton(bool $json_serialized = true)
56
    {
57
        // Create the button
58
        $inline_keyboard = [ 'inline_keyboard' =>
59
            [
60
                [
61
                    [
62
                        'text' => $this->bot->local[$this->bot->language]['Back_Button'],
0 ignored issues
show
Bug introduced by
The property local does not seem to exist in PhpBotFramework\Core\CoreBot.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property language does not seem to exist in PhpBotFramework\Core\CoreBot.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
63
                        'callback_data' => 'back'
64
                    ]
65
                ]
66
            ]
67
        ];
68
69
        // Serialize everything as JSON if necessary
70
        if ($json_serialized) {
71
            return json_encode($inline_keyboard);
72
        } else {
73
            return $inline_keyboard;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $inline_keyboard; (array<string,array[][]>) is incompatible with the return type documented by PhpBotFramework\Localization\Button::getBackButton of type PhpBotFramework\Localization\A.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
74
        }
75
    }
76
77
    /**
78
<<<<<<< HEAD
79
     * \brief Get 'Back' and 'Skip' buttons on the same row.
80
     * \details Back button has <code>callback_data</code> "back"; Skip button has "skip".
81
     * @param $json_serialized return a JSON-serialized string, or an array.
82
     * @return A button labeled 'Back' and another one labeled 'Skip'.
83
=======
84
     * \brief Get a Back and a Skip buttons inthe same row.
85
     * \details Back button has callback_data "back" and Skip button has callback_data "skip".
86
     * @param bool $json_serialized return a json serialized string, or an array.
87
     * @return string|array A button with written "back" and one with written "Skip".
88
>>>>>>> master
89
     */
90
    public function getBackSkipKeyboard(bool $json_serialized = true)
91
    {
92
        // Create the keyboard
93
        $inline_keyboard = [ 'inline_keyboard' =>
94
            [
95
                [
96
                    [
97
                        'text' => $this->bot->local[$this->bot->language]['Back_Button'],
0 ignored issues
show
Bug introduced by
The property local does not seem to exist in PhpBotFramework\Core\CoreBot.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property language does not seem to exist in PhpBotFramework\Core\CoreBot.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
98
                        'callback_data' => 'back'
99
                    ],
100
                    [
101
                        'text' => $this->bot->local[$this->bot->language]['Skip_Button'],
102
                        'callback_data' => 'skip'
103
                    ]
104
                ]
105
            ]
106
        ];
107
108
        if ($json_serialized) {
109
            return json_encode($inline_keyboard);
110
        } else {
111
            return $inline_keyboard;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $inline_keyboard; (array<string,array[][]>) is incompatible with the return type documented by PhpBotFramework\Localiza...on::getBackSkipKeyboard of type PhpBotFramework\Localization\A.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
112
        }
113
    }
114
115
    /**
116
     * \brief Get various buttons for set various languages.
117
     * \details Create a button for each language contained in <code>$localization['languages']</code> variable of $bot object.
118
     * There will be a button per row.
119
     *
120
     * The button's label will include the target language and the current one.
121
     * The callback data for each button will be "cl/key" where key is the key in <code>$localization['languages']</code>.
122
     * @param $prefix Prefix followed by '/' and the language index (en, it..).
123
     * @param $json_serialized Get a JSON-serialized string or an array.
124
     * @return The buttons in the selected type.
125
     */
126
    public function getChooseLanguageKeyboard(string $prefix = 'cl', bool $json_serialized = true)
127
    {
128
        $inline_keyboard = ['inline_keyboard' => array()];
129
130
        foreach ($this->bot->local as $languages => $language_msg) {
131
            // If the language is the same as the one set for the current user in $bot
132
            if (strpos($languages, $this->bot->language) !== false) {
133
                // Just create a button with one language in it
134
                array_push($inline_keyboard['inline_keyboard'], [
135
                    [
136
                        'text' => $language_msg['Language'],
137
                        'callback_data' => 'same/language'
138
                    ]
139
                ]);
140
            } else {
141
                array_push($inline_keyboard['inline_keyboard'], [
142
                        [
143
                            'text' => $language_msg['Language'] . '/' . $this->bot->local[$this->bot->language][$languages],
0 ignored issues
show
Bug introduced by
The property local does not seem to exist in PhpBotFramework\Core\CoreBot.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property language does not seem to exist in PhpBotFramework\Core\CoreBot.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
144
                            'callback_data' => $prefix . '/' . $languages
145
                        ]
146
                ]);
147
            }
148
        }
149
150
        // Unset the variables from the foreach
151
        unset($languages);
152
        unset($language_msg);
153
154
        array_push($inline_keyboard['inline_keyboard'], [
155
                [
156
                    'text' => $this->bot->local[$this->bot->language]['Back_Button'],
157
                    'callback_data' => 'back'
158
                ]
159
        ]);
160
161
        if ($json_serialized) {
162
            return json_encode($inline_keyboard);
163
        } else {
164
            return $inline_keyboard;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $inline_keyboard; (array<string,array>) is incompatible with the return type documented by PhpBotFramework\Localiza...tChooseLanguageKeyboard of type PhpBotFramework\Localization\The.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
165
        }
166
    }
167
168
    /** @} */
169
}
170