Completed
Push — master ( 549b3b...080752 )
by Danilo
15:29
created

InlineKeyboard::getBackButton()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 6
cp 0
rs 9.2
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 6
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\Entities;
20
21
use PhpBotFramework\Exceptions\BotException;
22
23
/**
24
 * \addtogroup Entities Entities
25
 * @{
26
 */
27
28
/** \class InlineKeyboard
29
 * \brief Inline Keyboard handler that create and handle inline keyboard buttons.
30
 * \details It stores the inline keyboard buttons added until get() is called.
31
 * It also provides some basic button to get, like Menu and Back buttons plus the dynamic-keyboard for menu browsing.
32
 */
33
class InlineKeyboard
34
{
35
36
    /**
37
     * \addtogroup InlineKeyboard InlineKeyboard
38
     * \brief Handle an inline keyboard to send along with messages.
39
     * @{
40
     */
41
42
    /** \brief Store the array of InlineKeyboardButton */
43
    protected $inline_keyboard;
44
45
    /** \brief Store a reference to the bot that is using this inline keyboard. */
46
    protected $bot;
47
48
    /** \brief Store the current row. */
49
    private $row;
50
51
    /** \brief Store the current column. */
52
    private $column;
53
54
    /**
55
     * \brief Create an inline keyboard object.
56
     * @param $bot The bot that owns this object.
57
     * @param $buttons Buttons passed as inizialization.
58
     * @return The object created with the buttons passed.
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
59
     */
60 2
    public function __construct(
61
        \PhpBotFramework\Bot &$bot = null,
62
        array $buttons = array()
63
    ) {
64
65
        // Get bot reference
66 2
        $this->bot = $bot;
67
68
        // If $buttons is empty, initialize it with an empty array
69 2
        $this->inline_keyboard = $buttons;
70
71
        // Set up vars
72 2
        $this->row = 0;
73 2
        $this->column = 0;
74 2
    }
75
76
    /**
77
     * \brief Get a JSON-serialized object containg the inline keyboard.
78
     * @param $clear_keyboard Remove all the buttons from this object.
79
     * @return JSON-serialized string with the buttons.
0 ignored issues
show
Documentation introduced by
The doc-type JSON-serialized could not be parsed: Unknown type name "JSON-serialized" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
80
     */
81 1 View Code Duplication
    public function get($clear_keyboard = 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...
82
    {
83
84
        // Check if it is empty
85 1
        if (empty($this->inline_keyboard)) {
86
            throw new BotException("Inline keyboard is empty");
87
        }
88
89
        // Create a new array to put our buttons
90 1
        $reply_markup = ['inline_keyboard' => $this->inline_keyboard];
91
92
        // Encode the array in a json object
93 1
        $reply_markup = json_encode($reply_markup);
94
95 1
        if ($clear_keyboard) {
96
            $this->clearKeyboard();
97
        }
98
99 1
        return $reply_markup;
100
    }
101
102
    /**
103
     * \brief Get the array containing the buttons. (Use this method when adding keyboard to inline query results)
104
     * @param $clean_keyboard Remove all the button from this object.
105
     * @return An array containing the buttons.
106
     */
107 1 View Code Duplication
    public function getArray($clean_keyboard = 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...
108
    {
109
110
        // Check if it is empty
111 1
        if (empty($this->inline_keyboard)) {
112
            throw new BotException("Inline keyboard is empty");
113
        }
114
115
        // Create a new array to put the buttons
116 1
        $reply_markup = ['inline_keyboard' => $this->inline_keyboard];
117
118 1
        if ($clean_keyboard) {
119 1
            $this->clearKeyboard();
120
        }
121
122 1
        return $reply_markup;
123
    }
124
125
    /** \brief Add buttons for the current row of buttons
126
     * \details Each array sent as parameter require a text key
127
     * and one another key (as specified <a href="https://core.telegram.org/bots/api/#inlinekeyboardbutton" target="blank">here</a> between:
128
     * - url
129
     * - callback_data
130
     * - switch_inline_query
131
     * - switch_inline_query_current_chat
132
     * - callback_game
133
     *
134
     * Each call to this function add one or more button to a row. The next call add buttons on the next row.
135
     * Each row allows 8 buttons per row and 12 columns total.
136
     * Use this function with this syntax:
137
     *
138
     *     addLevelButtons(['text' => 'Click me!', 'url' => 'https://telegram.me']);
139
     *
140
     * If you want to add more than a button, use this syntax:
141
     *
142
     *     addLevelButtons(['text' => 'Button 1', 'url' => 'https://telegram.me/gamedev_ita'], ['text' => 'Button 2', 'url' => 'https://telegram.me/animewallpaper']);
143
     *
144
     * @param ...$buttons One or more arrays, each one represent a button.
145
     */
146 1
    public function addLevelButtons(array ...$buttons)
147
    {
148
149
        // If the user has already added a button in this row
150 1
        if ($this->column != 0) {
151
             // Change row
152
             $this->changeRow();
153
        }
154
155
        // Add buttons to the next row
156 1
        $this->inline_keyboard[] = $buttons;
157
158
        // Switch to the next row
159 1
        $this->changeRow();
160 1
    }
161
162
    /** \brief Add a button.
163
     * \details The button will be added next to the last one or in the next row if the bot has reached the limit for the buttons per row.
164
     *
165
     * Each row allows 8 buttons per row and 12 columns total.
166
     * Use this function with this syntax:
167
     *
168
     *     addButton('Click me!', 'url', 'https://telegram.me');
169
     *
170
     * @param $text Text showed on the button.
171
     * @param $data_type The type of the button data.
172
     * Select one from these types.
173
     * - url
174
     * - callback_data
175
     * - switch_inline_query
176
     * - switch_inline_query_current_chat
177
     * - callback_game
178
     * @param $data Data for the type selected.
179
     */
180
    public function addButton($text, string $data_type, string $data)
181
    {
182
183
        // If we get the end of the row
184
        if ($this->column == 8) {
185
            $this->changeRow();
186
        }
187
188
        // Add the button
189
        $this->inline_keyboard[$this->row][$this->column] = ['text' => $text, $data_type => $data];
190
191
        // Update column
192
        $this->column++;
193
    }
194
195
    /**
196
     * \brief Change row for the current keyboard.
197
     * \details Buttons will be added in the next row from now on (until next InlineKeyboard::addLevelButtons() or InlineKeyboard::changeRow() call or InlineKeyboard::addButton() reaches the max).
198
     */
199 1
    public function changeRow()
200
    {
201
202
        // Reset vars
203 1
        $this->row++;
204 1
        $this->column = 0;
205 1
    }
206
207
    /** \brief Remove all the buttons from the current inline keyboard. */
208 1
    public function clearKeyboard()
209
    {
210
211
        // Set the inline keyboard to an empty array
212 1
        $this->inline_keyboard = [];
213
214
        // Reset vars
215 1
        $this->row = 0;
216 1
        $this->column = 0;
217 1
    }
218
219
    /**
220
     * \brief Get a simple Back button with back as callback_data.
221
     * @param $json_serialized return a json serialized string, or an array.
222
     * @return A button with written "back".
223
     */
224
    public function getBackButton($json_serialized = true)
225
    {
226
227
        // Create the button
228
        $inline_keyboard = [ 'inline_keyboard' =>
229
            [
230
                [
231
                    [
232
                        'text' => $this->bot->local[$this->bot->language]['Back_Button'],
233
                        'callback_data' => 'back'
234
                    ]
235
                ]
236
            ]
237
        ];
238
239
        // Does we need it as json-serialized?
240
        if ($json_serialized) {
241
            return json_encode($inline_keyboard);
242
        } else {
243
            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\Entities...Keyboard::getBackButton of type PhpBotFramework\Entities\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...
244
        }
245
    }
246
247
    /**
248
     * \brief Get a Back and a Skip buttons inthe same row.
249
     * \details Back button has callback_data "back" and Skip button has callback_data "skip".
250
     * @param $json_serialized return a json serialized string, or an array.
251
     * @return A button with written "back" and one with written "Skip".
252
     */
253
    public function getBackSkipKeyboard($json_serialized = true)
254
    {
255
256
        // Create the keyboard
257
        $inline_keyboard = [ 'inline_keyboard' =>
258
            [
259
                [
260
                    [
261
                        'text' => $this->bot->local[$this->bot->language]['Back_Button'],
262
                        'callback_data' => 'back'
263
                    ],
264
                    [
265
                        'text' => $this->bot->local[$this->bot->language]['Skip_Button'],
0 ignored issues
show
Bug introduced by
The property local cannot be accessed from this context as it is declared protected in class PhpBotFramework\Localization\File.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
266
                        'callback_data' => 'skip'
267
                    ]
268
                ]
269
            ]
270
        ];
271
272
        // Does we need it as json-serialized?
273
        if ($json_serialized) {
274
            return json_encode($inline_keyboard);
275
        } else {
276
            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\Entities...rd::getBackSkipKeyboard of type PhpBotFramework\Entities\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...
277
        }
278
    }
279
280
    /**
281
     * \brief Get button for each language.
282
     * \details Create a button for each language contained in $localization['languages'] variable of $bot object.
283
     * The button will be one per row.
284
     * The text will be the language and the language localizatated for the current user with a slash between them.
285
     * The callback data for each button will be "cl/key" where key is the key in $localization['languages'].
286
     * @param $prefix Prefix followed by '/' and the language index (en, it..).
287
     * @param $json_serialized Get a JSON-serialized string or an array.
288
     * @return The buttons in the selected type.
289
     */
290
    public function getChooseLanguageKeyboard($prefix = 'cl', $json_serialized = true)
291
    {
292
293
        // Create the empty array
294
        $inline_keyboard = ['inline_keyboard' => array()];
295
296
        foreach ($this->bot->local as $languages => $language_msg) {
297
            // If the language is the same as the one set for the current user in $bot
298
            if (strpos($languages, $this->bot->language) !== false) {
299
                // Just create a button with one language in it
300
                array_push($inline_keyboard['inline_keyboard'], [
301
                    [
302
                        'text' => $language_msg['Language'],
303
                        'callback_data' => 'same/language'
304
                    ]
305
                ]);
306
            } else {
307
                // Create a button with the language on the left and the language localizated for the current user in the right
308
                array_push($inline_keyboard['inline_keyboard'], [
309
                        [
310
                            'text' => $language_msg['Language'] . '/' . $this->bot->local[$this->bot->language][$languages],
0 ignored issues
show
Bug introduced by
The property local cannot be accessed from this context as it is declared protected in class PhpBotFramework\Localization\File.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
311
                            'callback_data' => $prefix . '/' . $languages
312
                        ]
313
                ]);
314
            }
315
        }
316
317
        // Unset the variables from the foreach
318
        unset($languages);
319
        unset($language_msg);
320
321
        array_push($inline_keyboard['inline_keyboard'], [
322
                [
323
                    'text' => $this->bot->local[$this->bot->language]['Back_Button'],
324
                    'callback_data' => 'back'
325
                ]
326
        ]);
327
328
        if ($json_serialized) {
329
            return json_encode($inline_keyboard);
330
        } else {
331
            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\Entities...tChooseLanguageKeyboard of type PhpBotFramework\Entities\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...
332
        }
333
    }
334
335
    /**
336
     * \brief */
337
    public function addListKeyboard(int $index, int $list, $prefix = 'list')
338
    {
339
340
        if (($list > 0) && ($index >= 0)) {
341
            if ($index == 0) {
342
                if ($list > 1) {
343
                    if ($list > 2) {
344
                        if ($list > 3) {
345
                            if ($list > 4) {
346
                                if ($list > 5) {
347
                                    $buttons = [
348
                                            [
349
                                                'text' => '1',
350
                                                'callback_data' => $prefix . '/1'
351
                                            ],
352
                                            [
353
                                                'text' => '2',
354
                                                'callback_data' => $prefix . '/2'
355
                                            ],
356
                                            [
357
                                                'text' => '3',
358
                                                'callback_data' => $prefix . '/3'
359
                                            ],
360
                                            [
361
                                                'text' => '4 ›',
362
                                                'callback_data' => $prefix . '/4'
363
                                            ],
364
                                            [
365
                                                'text' => "$list ››",
366
                                                'callback_data' => $prefix . "/$list"
367
                                            ]
368
                                        ];
369
                                } else {
370
                                    $buttons = [
371
                                            [
372
                                                'text' => '1',
373
                                                'callback_data' => $prefix . '/1'
374
                                            ],
375
                                            [
376
                                                'text' => '2',
377
                                                'callback_data' => $prefix . '/2'
378
                                            ],
379
                                            [
380
                                                'text' => '3',
381
                                                'callback_data' => $prefix . '/3'
382
                                            ],
383
                                            [
384
                                                'text' => '4',
385
                                                'callback_data' => $prefix . '/4'
386
                                            ],
387
                                            [
388
                                                'text' => '5',
389
                                                'callback_data' => $prefix . '/5'
390
                                            ]
391
                                        ];
392
                                }
393
                            } else {
394
                                $buttons = [
395
                                        [
396
                                            'text' => '1',
397
                                            'callback_data' => $prefix . '/1'
398
                                        ],
399
                                        [
400
                                            'text' => '2',
401
                                            'callback_data' => $prefix . '/2'
402
                                        ],
403
                                        [
404
                                            'text' => '3',
405
                                            'callback_data' => $prefix . '/3'
406
                                        ],
407
                                        [
408
                                            'text' => '4',
409
                                            'callback_data' => $prefix . '/4'
410
                                        ],
411
                                    ];
412
                            }
413
                        } else {
414
                            $buttons = [
415
                                    [
416
                                        'text' => '1',
417
                                        'callback_data' => $prefix . '/1'
418
                                    ],
419
                                    [
420
                                        'text' => '2',
421
                                        'callback_data' => $prefix . '/2'
422
                                    ],
423
                                    [
424
                                        'text' => '3',
425
                                        'callback_data' => $prefix . '/3'
426
                                    ],
427
                                ];
428
                        }
429
                    } elseif ($list == 2) {
430
                        $buttons = [
431
                                [
432
                                    'text' => '1',
433
                                    'callback_data' => $prefix . '/1'
434
                                ],
435
                                [
436
                                    'text' => '2',
437
                                    'callback_data' => $prefix . '/2'
438
                                ],
439
                            ];
440
                    }
441
                } else {
442
                    $buttons = [
443
                            [
444
                                'text' => '1',
445
                                'callback_data' => $prefix . '/1'
446
                            ]
447
                    ];
448
                }
449
            } elseif ($index == 1) {
450
                if ($list > 1) {
451 View Code Duplication
                    if ($list > 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
452
                        if ($list > 3) {
453
                            if ($list > 4) {
454
                                if ($list > 5) {
455
                                    $buttons = [
456
                                            [
457
                                                'text' => '• 1 •',
458
                                                'callback_data' => 'null'
459
                                            ],
460
                                            [
461
                                                'text' => '2',
462
                                                'callback_data' => $prefix . '/2'
463
                                            ],
464
                                            [
465
                                                'text' => '3',
466
                                                'callback_data' => $prefix . '/3'
467
                                            ],
468
                                            [
469
                                                'text' => '4 ›',
470
                                                'callback_data' => $prefix . '/4'
471
                                            ],
472
                                            [
473
                                                'text' => "$list ››",
474
                                                'callback_data' => $prefix . "/$list"
475
                                            ]
476
                                        ];
477
                                } else {
478
                                    $buttons = [
479
                                            [
480
                                                'text' => '• 1 •',
481
                                                'callback_data' => 'null'
482
                                            ],
483
                                            [
484
                                                'text' => '2',
485
                                                'callback_data' => $prefix . '/2'
486
                                            ],
487
                                            [
488
                                                'text' => '3',
489
                                                'callback_data' => $prefix . '/3'
490
                                            ],
491
                                            [
492
                                                'text' => '4',
493
                                                'callback_data' => $prefix . '/4'
494
                                            ],
495
                                            [
496
                                                'text' => '5',
497
                                                'callback_data' => $prefix . '/5'
498
                                            ]
499
                                        ];
500
                                }
501
                            } else {
502
                                $buttons = [
503
                                        [
504
                                            'text' => '• 1 •',
505
                                                'callback_data' => 'null'
506
                                        ],
507
                                        [
508
                                                'text' => '2',
509
                                                'callback_data' => $prefix . '/2'
510
                                        ],
511
                                        [
512
                                                'text' => '3',
513
                                                'callback_data' => $prefix . '/3'
514
                                        ],
515
                                        [
516
                                                'text' => '4',
517
                                                'callback_data' => $prefix . '/4'
518
                                        ]
519
                                    ];
520
                            }
521
                        } else {
522
                            $buttons = [
523
                                    [
524
                                        'text' => '• 1 •',
525
                                        'callback_data' => 'null'
526
                                    ],
527
                                    [
528
                                        'text' => '2',
529
                                        'callback_data' => $prefix . '/2'
530
                                    ],
531
                                    [
532
                                        'text' => '3',
533
                                        'callback_data' => $prefix . '/3'
534
                                    ]
535
                                ];
536
                        }
537
                    } elseif ($list == 2) {
538
                        $buttons = [
539
                                [
540
                                    'text' => '• 1 •',
541
                                    'callback_data' => 'null'
542
                                ],
543
                                [
544
                                    'text' => '2',
545
                                    'callback_data' => $prefix . '/2'
546
                                ]
547
                            ];
548
                    }
549
                } else {
550
                    $buttons = [
551
                            [
552
                                'text' => '• 1 •',
553
                                'callback_data' => 'null'
554
                            ]
555
                        ];
556
                }
557 View Code Duplication
            } elseif ($index == 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
558
                if ($list > 3) {
559
                    if ($list > 4) {
560
                        if ($list > 5) {
561
                            $buttons = [
562
                                    [
563
                                        'text' => '1',
564
                                        'callback_data' => $prefix . '/1'
565
                                    ],
566
                                    [
567
                                        'text' => '• 2 •',
568
                                        'callback_data' => 'null'
569
                                    ],
570
                                    [
571
                                        'text' => '3',
572
                                        'callback_data' => $prefix . '/3'
573
                                    ],
574
                                    [
575
                                        'text' => '4 ›',
576
                                        'callback_data' => $prefix . '/4'
577
                                    ],
578
                                    [
579
                                        'text' => "$list ››",
580
                                        'callback_data' => $prefix . "/$list"
581
                                    ]
582
                                ];
583
                        } else {
584
                            $buttons = [
585
                                    [
586
                                        'text' => '1',
587
                                        'callback_data' => $prefix . '/1'
588
                                    ],
589
                                    [
590
                                        'text' => '• 2 •',
591
                                        'callback_data' => 'null'
592
                                    ],
593
                                    [
594
                                        'text' => '3',
595
                                        'callback_data' => $prefix . '/3'
596
                                    ],
597
                                    [
598
                                        'text' => '4',
599
                                        'callback_data' => '4'
600
                                    ],
601
                                    [
602
                                        'text' => '5',
603
                                        'callback_data' => $prefix . '/5'
604
                                    ]
605
                                ];
606
                        }
607
                    } else {
608
                        $buttons = [
609
                                [
610
                                    'text' => '1',
611
                                    'callback_data' => $prefix . '/1'
612
                                ],
613
                                [
614
                                    'text' => '• 2 •',
615
                                    'callback_data' => 'null'
616
                                ],
617
                                [
618
                                    'text' => '3',
619
                                    'callback_data' => $prefix . '/3'
620
                                ],
621
                                [
622
                                    'text' => '4',
623
                                    'callback_data' => $prefix . '/4'
624
                                ]
625
                            ];
626
                    }
627
                } elseif ($list == 3) {
628
                    $buttons = [
629
                            [
630
                                'text' => '1',
631
                                'callback_data' => $prefix . '/1'
632
                            ],
633
                            [
634
                                'text' => '• 2 •',
635
                                'callback_data' => 'null'
636
                            ],
637
                            [
638
                                'text' => '3',
639
                                'callback_data' => $prefix . '/3'
640
                            ]
641
                        ];
642
                } else {
643
                    $buttons = [
644
                            [
645
                                'text' => '1',
646
                                'callback_data' => $prefix . '/1'
647
                            ],
648
                            [
649
                                'text' => '• 2 •',
650
                                'callback_data' => 'null'
651
                            ]
652
                        ];
653
                }
654
            } elseif ($index == 3) {
655
                if ($list > 4) {
656
                    if ($list > 5) {
657
                        $buttons = [
658
                                [
659
                                    'text' => '1',
660
                                    'callback_data' => $prefix . '/1'
661
                                ],
662
                                [
663
                                    'text' => '2',
664
                                    'callback_data' => $prefix . '/2'
665
                                ],
666
                                [
667
                                    'text' => '• 3 •',
668
                                    'callback_data' => 'null'
669
                                ],
670
                                [
671
                                    'text' => '4 ›',
672
                                    'callback_data' => $prefix . '/4'
673
                                ],
674
                                [
675
                                    'text' => "$list ››",
676
                                    'callback_data' => $prefix . "/$list"
677
                                ]
678
                            ];
679
                    } else {
680
                        $buttons = [
681
                                [
682
                                    'text' => '1',
683
                                    'callback_data' => $prefix . '/1'
684
                                ],
685
                                [
686
                                    'text' => '2',
687
                                    'callback_data' => $prefix . '/2'
688
                                ],
689
                                [
690
                                    'text' => '• 3 •',
691
                                    'callback_data' => 'null'
692
                                ],
693
                                [
694
                                    'text' => '4',
695
                                    'callback_data' => $prefix . '/4'
696
                                ],
697
                                [
698
                                    'text' => '5',
699
                                    'callback_data' => $prefix . '/5'
700
                                ]
701
                            ];
702
                    }
703
                } elseif ($list == 4) {
704
                    $buttons = [
705
                            [
706
                                'text' => '1',
707
                                'callback_data' => $prefix . '/1'
708
                            ],
709
                            [
710
                                'text' => '2',
711
                                'callback_data' => $prefix . '/2'
712
                            ],
713
                            [
714
                                'text' => '• 3 •',
715
                                'callback_data' => 'null'
716
                            ],
717
                            [
718
                                'text' => '4',
719
                                'callback_data' => $prefix . '/4'
720
                            ]
721
                        ];
722
                } else {
723
                    $buttons = [
724
                            [
725
                                'text' => '1',
726
                                'callback_data' => $prefix . '/1'
727
                            ],
728
                            [
729
                                'text' => '2',
730
                                'callback_data' => $prefix . '/2'
731
                            ],
732
                            [
733
                                'text' => '• 3 •',
734
                                'callback_data' => 'null'
735
                            ]
736
                        ];
737
                }
738
            } elseif ($index == 4 && $list <= 5) {
739
                if ($list == 4) {
740
                    $buttons = [
741
                            [
742
                                'text' => '1',
743
                                'callback_data' => $prefix . '/1'
744
                            ],
745
                            [
746
                                'text' => '2',
747
                                'callback_data' => $prefix . '/2'
748
                            ],
749
                            [
750
                                'text' => '3',
751
                                'callback_data' => $prefix . '/3'
752
                            ],
753
                            [
754
                                'text' => '• 4 •',
755
                                'callback_data' => 'null'
756
                            ]
757
                        ];
758
                } elseif ($list == 5) {
759
                    $buttons = [
760
                            [
761
                                'text' => '1',
762
                                'callback_data' => $prefix . '/1'
763
                            ],
764
                            [
765
                                'text' => '2',
766
                                'callback_data' => $prefix . '/2'
767
                            ],
768
                            [
769
                                'text' => '3',
770
                                'callback_data' => $prefix . '/3'
771
                            ],
772
                            [
773
                                'text' => '• 4 •',
774
                                'callback_data' => 'null'
775
                            ],
776
                            [
777
                                'text' => '5',
778
                                'callback_data' => $prefix . '/5'
779
                            ]
780
                        ];
781
                }
782
            } elseif ($index == 5 && $list == 5) {
783
                $buttons = [
784
                        [
785
                            'text' => '1',
786
                            'callback_data' => $prefix . '/1'
787
                        ],
788
                        [
789
                            'text' => '2',
790
                            'callback_data' => $prefix . '/2'
791
                        ],
792
                        [
793
                            'text' => '3',
794
                            'callback_data' => $prefix . '/3'
795
                        ],
796
                        [
797
                            'text' => '4',
798
                            'callback_data' => $prefix . '/4'
799
                        ],
800
                        [
801
                            'text' => '• 5 •',
802
                            'callback_data' => 'null'
803
                        ]
804
                    ];
805
            } else {
806
                if ($index < $list - 2) {
807
                    $indexm = $index - 1;
808
                    $indexp = $index + 1;
809
                    $buttons = [
810
                            [
811
                                'text' => '‹‹ 1',
812
                                'callback_data' => $prefix . '/1'
813
                            ],
814
                            [
815
                                'text' => '‹ ' . $indexm,
816
                                'callback_data' => $prefix . '/' . $indexm
817
                            ],
818
                            [
819
                                'text' => '• ' . $index . ' •',
820
                                'callback_data' => 'null',
821
                            ],
822
                            [
823
                                'text' => $indexp . ' ›',
824
                                'callback_data' => $prefix . '/' . $indexp
825
                            ],
826
                            [
827
                                'text' => $list . ' ››',
828
                                'callback_data' => $prefix . '/' . $list
829
                            ]
830
                        ];
831
                } elseif ($index == ($list - 2)) {
832
                    $indexm = $index - 1;
833
                    $indexp = $index + 1;
834
                    $buttons = [
835
                            [
836
                                'text' => '‹‹1',
837
                                'callback_data' => $prefix . '/1'
838
                            ],
839
                            [
840
                                'text' => '' . $indexm,
841
                                'callback_data' => $prefix . '/' . $indexm
842
                            ],
843
                            [
844
                                'text' => '• ' . $index . ' •',
845
                                'callback_data' => 'null',
846
                            ],
847
                            [
848
                                'text' => '' . $indexp,
849
                                'callback_data' => $prefix . '/' . $indexp
850
                            ],
851
                            [
852
                                'text' => "$list",
853
                                'callback_data' => $prefix . "/$list"
854
                            ]
855
                        ];
856
                } elseif ($index == ($list - 1)) {
857
                    $indexm = $index - 1;
858
                    $indexmm = $index - 2;
859
                    $buttons = [
860
                            [
861
                                'text' => '‹‹ 1',
862
                                'callback_data' => $prefix . '/1'
863
                            ],
864
                            [
865
                                'text' => '‹ ' . $indexmm,
866
                                'callback_data' => $prefix . '/' . $indexmm
867
                            ],
868
                            [
869
                                'text' => '' . $indexm,
870
                                'callback_data' => $prefix . '/' . $indexm
871
                            ],
872
                            [
873
                                'text' => '• ' . $index . ' •',
874
                                'callback_data' => $prefix . '/' . $index
875
                            ],
876
                            [
877
                                'text' => "$list",
878
                                'callback_data' => $prefix . "/$list"
879
                            ]
880
                        ];
881
                } elseif ($index == $list) {
882
                    $indexm = $index - 1;
883
                    $indexmm = $index - 2;
884
                    $indexmmm = $index - 3;
885
                    $buttons = [
886
                            [
887
                                'text' => '‹‹ 1',
888
                                'callback_data' => $prefix . '/1'
889
                            ],
890
                            [
891
                                'text' => '‹ ' . $indexmmm,
892
                                'callback_data' => $prefix . '/' . $indexmmm
893
                            ],
894
                            [
895
                                'text' => '' . $indexmm,
896
                                'callback_data' => $prefix . '/' . $indexmm,
897
                            ],
898
                            [
899
                                'text' => '' . $indexm,
900
                                'callback_data' => $prefix . '/' . $indexm
901
                            ],
902
                            [
903
                                'text' => '• ' . $index . ' •',
904
                                'callback_data' => $prefix . '/' . $index
905
                            ]
906
                        ];
907
                }
908
            }
909
        }
910
911
        // If there are other buttons in this row (checking the column)
912
        if ($this->column !== 0) {
913
            // Go to the next
914
            $this->changeRow();
915
        }
916
917
        $this->inline_keyboard[$this->row] = $buttons;
0 ignored issues
show
Bug introduced by
The variable $buttons does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
918
919
        // We added a row
920
        $this->changeRow();
921
    }
922
923
    /** @} */
924
925
    /** @} */
926
}
927