Completed
Push — master ( 7d96b2...b0a42f )
by Danilo
01:50
created

InlineKeyboard::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace DanySpin97\PhpBotFramework;
4
5
/** \class InlineKeyboard
6
 * \brief Inline Keyboard handler that create and handle inline keyboard buttons.
7
 * \details It stores the inline keyboard buttons added until get() is called.
8
 * It also provides some basic button to get, like Menu and Back buttons plus the dynamic-keyboard for menu browsing.
9
 */
10
class InlineKeyboard {
11
12
    /**
13
     * \addtogroup InlineKeyboard InlineKeyboard
14
     * \brief Handle an inline keyboard to send along with messages.
15
     * @{
16
     */
17
18
    /** \brief Store the array of InlineKeyboardButton */
19
    protected $inline_keyboard;
20
21
    /** \brief Store a reference to the bot that is using this inline keyboard. */
22
    protected $bot;
23
24
    /** \brief Store the current row. */
25
    private $row;
26
27
    /** \brief Store the current column. */
28
    private $column;
29
30
    /**
31
     * \brief Create an inline keyboard object.
32
     * @param $bot The bot that owns this object.
33
     * @param $buttons Buttons passed as inizialization.
34
     * @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...
35
     */
36
    public function __construct(CoreBot &$bot = null, array $buttons = array()) {
37
38
        // Get bot reference
39
        $this->bot = $bot;
40
41
        // If $buttons is empty, initialize it with an empty array
42
        $this->inline_keyboard = $buttons;
43
44
        // Set up vars
45
        $this->row = 0;
46
        $this->column = 0;
47
48
    }
49
50
    /**
51
     * \brief Get a JSON-serialized object containg the inline keyboard.
52
     * @param $clear_keyboard Remove all the buttons from this object.
53
     * @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...
54
     */
55 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...
56
57
        // Check if it is empty
58
        if (empty($this->inline_keyboard)) {
59
60
            throw new BotException("Inline keyboard is empty");
61
62
        }
63
64
        // Create a new array to put our buttons
65
        $reply_markup = ['inline_keyboard' => $this->inline_keyboard];
66
67
        // Encode the array in a json object
68
        $reply_markup = json_encode($reply_markup);
69
70
        if ($clear_keyboard) {
71
72
            $this->clearKeyboard();
73
74
        }
75
76
        return $reply_markup;
77
78
    }
79
80
    /**
81
     * \brief Get the array containing the buttons. (Use this method when adding keyboard to inline query results)
82
     * @param $clean_keyboard Remove all the button from this object.
83
     * @return An array containing the buttons.
84
     */
85 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...
86
87
        // Check if it is empty
88
        if (empty($this->inline_keyboard)) {
89
90
            throw new BotException("Inline keyboard is empty");
91
92
        }
93
94
        // Create a new array to put the buttons
95
        $reply_markup = ['inline_keyboard' => $this->inline_keyboard];
96
97
        if ($clean_keyboard) {
98
99
            $this->clearKeyboard();
100
101
        }
102
103
        return $reply_markup;
104
    }
105
106
    /** \brief Add buttons for the current row of buttons
107
     * \details Each array sent as parameter require a text key
108
     * and one another key (as specified <a href="https://core.telegram.org/bots/api/#inlinekeyboardbutton" target="blank">here</a> between:
109
     * - url
110
     * - callback_data
111
     * - switch_inline_query
112
     * - switch_inline_query_current_chat
113
     * - callback_game
114
     *
115
     * Each call to this function add one or more button to a row. The next call add buttons on the next row.
116
     * Each row allows 8 buttons per row and 12 columns total.
117
     * Use this function with this syntax:
118
     *
119
     *     addLevelButtons(['text' => 'Click me!', 'url' => 'https://telegram.me']);
120
     *
121
     * If you want to add more than a button, use this syntax:
122
     *
123
     *     addLevelButtons(['text' => 'Button 1', 'url' => 'https://telegram.me/gamedev_ita'], ['text' => 'Button 2', 'url' => 'https://telegram.me/animewallpaper']);
124
     *
125
     * @param ...$buttons One or more arrays, each one represent a button.
126
     */
127
    public function addLevelButtons(array ...$buttons) {
128
129
        // If the user has already added a button in this row
130
        if ($this->column != 0) {
131
132
             // Change row
133
             $this->changeRow();
134
135
        }
136
137
        // Add buttons to the next row
138
        $this->inline_keyboard[] = $buttons;
139
140
        // Switch to the next row
141
        $this->changeRow();
142
143
    }
144
145
    /** \brief Add a button.
146
     * \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.
147
     *
148
     * Each row allows 8 buttons per row and 12 columns total.
149
     * Use this function with this syntax:
150
     *
151
     *     addButton('Click me!', 'url', 'https://telegram.me');
152
     *
153
     * @param $text Text showed on the button.
154
     * @param $data_type The type of the button data.
155
     * Select one from these types.
156
     * - url
157
     * - callback_data
158
     * - switch_inline_query
159
     * - switch_inline_query_current_chat
160
     * - callback_game
161
     * @param $data Data for the type selected.
162
     */
163
    public function addButton($text, string $data_type, string $data) {
164
165
        // If we get the end of the row
166
        if ($this->column == 8) {
167
168
            $this->changeRow();
169
170
        }
171
172
        // Add the button
173
        $this->inline_keyboard[$this->row][$this->column] = ['text' => $text, $data_type => $data];
174
175
        // Update column
176
        $this->column++;
177
178
    }
179
180
    /**
181
     * \brief Change row for the current keyboard.
182
     * \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).
183
     */
184
    public function changeRow() {
185
186
        // Reset vars
187
        $this->row++;
188
        $this->column = 0;
189
190
    }
191
192
    /** \brief Remove all the buttons from the current inline keyboard. */
193
    public function clearKeyboard() {
194
195
        // Set the inline keyboard to an empty array
196
        $this->inline_keyboard = [];
197
198
        // Reset vars
199
        $this->row = 0;
200
        $this->column = 0;
201
202
    }
203
204
    /**
205
     * \brief Get a simple Back button with back as callback_data.
206
     * @param $json_serialized return a json serialized string, or an array.
207
     * @return A button with written "back".
208
     */
209
    public function getBackButton($json_serialized = true) {
210
211
        // Create the button
212
        $inline_keyboard = [ 'inline_keyboard' =>
213
            [
214
                [
215
                    [
216
                        'text' => $this->bot->local[$this->bot->language]['Back_Button'],
0 ignored issues
show
Bug introduced by
The property language does not seem to exist in DanySpin97\PhpBotFramework\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 local does not seem to exist in DanySpin97\PhpBotFramework\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...
217
                        'callback_data' => 'back'
218
                    ]
219
                ]
220
            ]
221
        ];
222
223
        // Does we need it as json-serialized?
224
        if ($json_serialized) {
225
226
            return json_encode($inline_keyboard);
227
228
        } else {
229
230
            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 DanySpin97\PhpBotFramewo...Keyboard::getBackButton of type DanySpin97\PhpBotFramework\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...
231
232
        }
233
234
    }
235
236
    /**
237
     * \brief Get a Back and a Skip buttons inthe same row.
238
     * \details Back button has callback_data "back" and Skip button has callback_data "skip".
239
     * @param $json_serialized return a json serialized string, or an array.
240
     * @return A button with written "back" and one with written "Skip".
241
     */
242
    public function getBackSkipKeyboard($json_serialized = true) {
243
244
        // Create the keyboard
245
        $inline_keyboard = [ 'inline_keyboard' =>
246
            [
247
                [
248
                    [
249
                        '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 DanySpin97\PhpBotFramework\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 DanySpin97\PhpBotFramework\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...
250
                        'callback_data' => 'back'
251
                    ],
252
                    [
253
                        'text' => $this->bot->local[$this->bot->language]['Skip_Button'],
254
                        'callback_data' => 'skip'
255
                    ]
256
                ]
257
            ]
258
        ];
259
260
        // Does we need it as json-serialized?
261
        if ($json_serialized) {
262
263
            return json_encode($inline_keyboard);
264
265
        } else {
266
267
            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 DanySpin97\PhpBotFramewo...rd::getBackSkipKeyboard of type DanySpin97\PhpBotFramework\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...
268
269
        }
270
271
    }
272
273
    /**
274
     * \brief Get button for each language.
275
     * \details Create a button for each language contained in $localization['languages'] variable of $bot object.
276
     * The button will be one per row.
277
     * The text will be the language and the language localizatated for the current user with a slash between them.
278
     * The callback data for each button will be "cl/key" where key is the key in $localization['languages'].
279
     * @param $prefix Prefix followed by '/' and the language index (en, it..).
280
     * @param $json_serialized Get a JSON-serialized string or an array.
281
     * @return The buttons in the selected type.
282
     */
283
    public function getChooseLanguageKeyboard($prefix = 'cl', $json_serialized = true) {
284
285
        // Create the empty array
286
        $inline_keyboard = ['inline_keyboard' => array()];
287
288
        foreach ($this->bot->local as $languages => $language_msg) {
289
290
            // If the language is the same as the one set for the current user in $bot
291
            if (strpos($languages, $this->bot->language) !== false) {
292
293
                // Just create a button with one language in it
294
                array_push($inline_keyboard['inline_keyboard'], [
295
                    [
296
                        'text' => $language_msg['Language'],
297
                        'callback_data' => 'same/language'
298
                    ]
299
                ]);
300
301
            } else {
302
303
                // Create a button with the language on the left and the language localizated for the current user in the right
304
                array_push($inline_keyboard['inline_keyboard'], [
305
                        [
306
                            '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 DanySpin97\PhpBotFramework\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 DanySpin97\PhpBotFramework\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...
307
                            'callback_data' => $prefix . '/' . $languages
308
                        ]
309
                ]);
310
311
            }
312
313
        }
314
315
        // Unset the variables from the foreach
316
        unset($languages);
317
        unset($language_msg);
318
319
        array_push($inline_keyboard['inline_keyboard'], [
320
                [
321
                    'text' => $this->bot->local[$this->bot->language]['Back_Button'],
322
                    'callback_data' => 'back'
323
                ]
324
        ]);
325
326
        if ($json_serialized) {
327
328
            return json_encode($inline_keyboard);
329
330
        } else {
331
332
            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 DanySpin97\PhpBotFramewo...tChooseLanguageKeyboard of type DanySpin97\PhpBotFramework\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...
333
334
        }
335
336
    }
337
338
    /**
339
     * \brief */
340
    public function addListKeyboard(int $index, int $list, $prefix = 'list') {
341
342
        if (($list > 0) && ($index >= 0)) {
343
            if ($index == 0) {
344
                if ($list > 1) {
345
                    if ($list > 2) {
346
                        if ($list > 3) {
347
                            if ($list > 4) {
348
                                if ($list > 5) {
349
                                    $buttons = [
350
                                            [
351
                                                'text' => '1',
352
                                                'callback_data' => $prefix . '/1'
353
                                            ],
354
                                            [
355
                                                'text' => '2',
356
                                                'callback_data' => $prefix . '/2'
357
                                            ],
358
                                            [
359
                                                'text' => '3',
360
                                                'callback_data' => $prefix . '/3'
361
                                            ],
362
                                            [
363
                                                'text' => '4 ›',
364
                                                'callback_data' => $prefix . '/4'
365
                                            ],
366
                                            [
367
                                                'text' => "$list ››",
368
                                                'callback_data' => $prefix . "/$list"
369
                                            ]
370
                                        ];
371
                                } else {
372
                                    $buttons = [
373
                                            [
374
                                                'text' => '1',
375
                                                'callback_data' => $prefix . '/1'
376
                                            ],
377
                                            [
378
                                                'text' => '2',
379
                                                'callback_data' => $prefix . '/2'
380
                                            ],
381
                                            [
382
                                                'text' => '3',
383
                                                'callback_data' => $prefix . '/3'
384
                                            ],
385
                                            [
386
                                                'text' => '4',
387
                                                'callback_data' => $prefix . '/4'
388
                                            ],
389
                                            [
390
                                                'text' => '5',
391
                                                'callback_data' => $prefix . '/5'
392
                                            ]
393
                                        ];
394
                                }
395
                            } else {
396
                                $buttons = [
397
                                        [
398
                                            'text' => '1',
399
                                            'callback_data' => $prefix . '/1'
400
                                        ],
401
                                        [
402
                                            'text' => '2',
403
                                            'callback_data' => $prefix . '/2'
404
                                        ],
405
                                        [
406
                                            'text' => '3',
407
                                            'callback_data' => $prefix . '/3'
408
                                        ],
409
                                        [
410
                                            'text' => '4',
411
                                            'callback_data' => $prefix . '/4'
412
                                        ],
413
                                    ];
414
                            }
415
                        } else {
416
                            $buttons = [
417
                                    [
418
                                        'text' => '1',
419
                                        'callback_data' => $prefix . '/1'
420
                                    ],
421
                                    [
422
                                        'text' => '2',
423
                                        'callback_data' => $prefix . '/2'
424
                                    ],
425
                                    [
426
                                        'text' => '3',
427
                                        'callback_data' => $prefix . '/3'
428
                                    ],
429
                                ];
430
                        }
431
                    } elseif ($list == 2) {
432
                        $buttons = [
433
                                [
434
                                    'text' => '1',
435
                                    'callback_data' => $prefix . '/1'
436
                                ],
437
                                [
438
                                    'text' => '2',
439
                                    'callback_data' => $prefix . '/2'
440
                                ],
441
                            ];
442
                    }
443
                } else {
444
                    $buttons = [
445
                            [
446
                                'text' => '1',
447
                                'callback_data' => $prefix . '/1'
448
                            ]
449
                    ];
450
                }
451
            } else if ($index == 1) {
452
                if ($list > 1) {
453 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...
454
                        if ($list > 3) {
455
                            if ($list > 4) {
456
                                if ($list > 5) {
457
                                    $buttons = [
458
                                            [
459
                                                'text' => '• 1 •',
460
                                                'callback_data' => 'null'
461
                                            ],
462
                                            [
463
                                                'text' => '2',
464
                                                'callback_data' => $prefix . '/2'
465
                                            ],
466
                                            [
467
                                                'text' => '3',
468
                                                'callback_data' => $prefix . '/3'
469
                                            ],
470
                                            [
471
                                                'text' => '4 ›',
472
                                                'callback_data' => $prefix . '/4'
473
                                            ],
474
                                            [
475
                                                'text' => "$list ››",
476
                                                'callback_data' => $prefix . "/$list"
477
                                            ]
478
                                        ];
479
                                } else {
480
                                    $buttons = [
481
                                            [
482
                                                'text' => '• 1 •',
483
                                                'callback_data' => 'null'
484
                                            ],
485
                                            [
486
                                                'text' => '2',
487
                                                'callback_data' => $prefix . '/2'
488
                                            ],
489
                                            [
490
                                                'text' => '3',
491
                                                'callback_data' => $prefix . '/3'
492
                                            ],
493
                                            [
494
                                                'text' => '4',
495
                                                'callback_data' => $prefix . '/4'
496
                                            ],
497
                                            [
498
                                                'text' => '5',
499
                                                'callback_data' => $prefix . '/5'
500
                                            ]
501
                                        ];
502
                                }
503
                            } else {
504
                                $buttons = [
505
                                        [
506
                                            'text' => '• 1 •',
507
                                                'callback_data' => 'null'
508
                                        ],
509
                                        [
510
                                                'text' => '2',
511
                                                'callback_data' => $prefix . '/2'
512
                                        ],
513
                                        [
514
                                                'text' => '3',
515
                                                'callback_data' => $prefix . '/3'
516
                                        ],
517
                                        [
518
                                                'text' => '4',
519
                                                'callback_data' => $prefix . '/4'
520
                                        ]
521
                                    ];
522
                            }
523
                        } else {
524
                            $buttons = [
525
                                    [
526
                                        'text' => '• 1 •',
527
                                        'callback_data' => 'null'
528
                                    ],
529
                                    [
530
                                        'text' => '2',
531
                                        'callback_data' => $prefix . '/2'
532
                                    ],
533
                                    [
534
                                        'text' => '3',
535
                                        'callback_data' => $prefix . '/3'
536
                                    ]
537
                                ];
538
                        }
539
                    } elseif ($list == 2) {
540
                        $buttons = [
541
                                [
542
                                    'text' => '• 1 •',
543
                                    'callback_data' => 'null'
544
                                ],
545
                                [
546
                                    'text' => '2',
547
                                    'callback_data' => $prefix . '/2'
548
                                ]
549
                            ];
550
                    }
551
                } else {
552
                    $buttons = [
553
                            [
554
                                'text' => '• 1 •',
555
                                'callback_data' => 'null'
556
                            ]
557
                        ];
558
                }
559 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...
560
                if ($list > 3) {
561
                    if ($list > 4) {
562
                        if ($list > 5) {
563
                            $buttons = [
564
                                    [
565
                                        'text' => '1',
566
                                        'callback_data' => $prefix . '/1'
567
                                    ],
568
                                    [
569
                                        'text' => '• 2 •',
570
                                        'callback_data' => 'null'
571
                                    ],
572
                                    [
573
                                        'text' => '3',
574
                                        'callback_data' => $prefix . '/3'
575
                                    ],
576
                                    [
577
                                        'text' => '4 ›',
578
                                        'callback_data' => $prefix . '/4'
579
                                    ],
580
                                    [
581
                                        'text' => "$list ››",
582
                                        'callback_data' => $prefix . "/$list"
583
                                    ]
584
                                ];
585
                        } else {
586
                            $buttons = [
587
                                    [
588
                                        'text' => '1',
589
                                        'callback_data' => $prefix . '/1'
590
                                    ],
591
                                    [
592
                                        'text' => '• 2 •',
593
                                        'callback_data' => 'null'
594
                                    ],
595
                                    [
596
                                        'text' => '3',
597
                                        'callback_data' => $prefix . '/3'
598
                                    ],
599
                                    [
600
                                        'text' => '4',
601
                                        'callback_data' => '4'
602
                                    ],
603
                                    [
604
                                        'text' => '5',
605
                                        'callback_data' => $prefix . '/5'
606
                                    ]
607
                                ];
608
                        }
609
                    } else {
610
                        $buttons = [
611
                                [
612
                                    'text' => '1',
613
                                    'callback_data' => $prefix . '/1'
614
                                ],
615
                                [
616
                                    'text' => '• 2 •',
617
                                    'callback_data' => 'null'
618
                                ],
619
                                [
620
                                    'text' => '3',
621
                                    'callback_data' => $prefix . '/3'
622
                                ],
623
                                [
624
                                    'text' => '4',
625
                                    'callback_data' => $prefix . '/4'
626
                                ]
627
                            ];
628
                    }
629
                } elseif ($list == 3) {
630
                    $buttons = [
631
                            [
632
                                'text' => '1',
633
                                'callback_data' => $prefix . '/1'
634
                            ],
635
                            [
636
                                'text' => '• 2 •',
637
                                'callback_data' => 'null'
638
                            ],
639
                            [
640
                                'text' => '3',
641
                                'callback_data' => $prefix . '/3'
642
                            ]
643
                        ];
644
                } else {
645
                    $buttons = [
646
                            [
647
                                'text' => '1',
648
                                'callback_data' => $prefix . '/1'
649
                            ],
650
                            [
651
                                'text' => '• 2 •',
652
                                'callback_data' => 'null'
653
                            ]
654
                        ];
655
                }
656
            } elseif ($index == 3) {
657
                if ($list > 4) {
658
                    if ($list > 5) {
659
                        $buttons = [
660
                                [
661
                                    'text' => '1',
662
                                    'callback_data' => $prefix . '/1'
663
                                ],
664
                                [
665
                                    'text' => '2',
666
                                    'callback_data' => $prefix . '/2'
667
                                ],
668
                                [
669
                                    'text' => '• 3 •',
670
                                    'callback_data' => 'null'
671
                                ],
672
                                [
673
                                    'text' => '4 ›',
674
                                    'callback_data' => $prefix . '/4'
675
                                ],
676
                                [
677
                                    'text' => "$list ››",
678
                                    'callback_data' => $prefix . "/$list"
679
                                ]
680
                            ];
681
                    } else {
682
                        $buttons = [
683
                                [
684
                                    'text' => '1',
685
                                    'callback_data' => $prefix . '/1'
686
                                ],
687
                                [
688
                                    'text' => '2',
689
                                    'callback_data' => $prefix . '/2'
690
                                ],
691
                                [
692
                                    'text' => '• 3 •',
693
                                    'callback_data' => 'null'
694
                                ],
695
                                [
696
                                    'text' => '4',
697
                                    'callback_data' => $prefix . '/4'
698
                                ],
699
                                [
700
                                    'text' => '5',
701
                                    'callback_data' => $prefix . '/5'
702
                                ]
703
                            ];
704
                    }
705
                } elseif ($list == 4) {
706
                    $buttons = [
707
                            [
708
                                'text' => '1',
709
                                'callback_data' => $prefix . '/1'
710
                            ],
711
                            [
712
                                'text' => '2',
713
                                'callback_data' => $prefix . '/2'
714
                            ],
715
                            [
716
                                'text' => '• 3 •',
717
                                'callback_data' => 'null'
718
                            ],
719
                            [
720
                                'text' => '4',
721
                                'callback_data' => $prefix . '/4'
722
                            ]
723
                        ];
724
                } else {
725
                    $buttons = [
726
                            [
727
                                'text' => '1',
728
                                'callback_data' => $prefix . '/1'
729
                            ],
730
                            [
731
                                'text' => '2',
732
                                'callback_data' => $prefix . '/2'
733
                            ],
734
                            [
735
                                'text' => '• 3 •',
736
                                'callback_data' => 'null'
737
                            ]
738
                        ];
739
                }
740
            } elseif ($index == 4 && $list <= 5) {
741
                if ($list == 4) {
742
                    $buttons = [
743
                            [
744
                                'text' => '1',
745
                                'callback_data' => $prefix . '/1'
746
                            ],
747
                            [
748
                                'text' => '2',
749
                                'callback_data' => $prefix . '/2'
750
                            ],
751
                            [
752
                                'text' => '3',
753
                                'callback_data' => $prefix . '/3'
754
                            ],
755
                            [
756
                                'text' => '• 4 •',
757
                                'callback_data' => 'null'
758
                            ]
759
                        ];
760
                } else if ($list == 5) {
761
                    $buttons = [
762
                            [
763
                                'text' => '1',
764
                                'callback_data' => $prefix . '/1'
765
                            ],
766
                            [
767
                                'text' => '2',
768
                                'callback_data' => $prefix . '/2'
769
                            ],
770
                            [
771
                                'text' => '3',
772
                                'callback_data' => $prefix . '/3'
773
                            ],
774
                            [
775
                                'text' => '• 4 •',
776
                                'callback_data' => 'null'
777
                            ],
778
                            [
779
                                'text' => '5',
780
                                'callback_data' => $prefix . '/5'
781
                            ]
782
                        ];
783
                }
784
            } else if ($index == 5 && $list == 5) {
785
                $buttons = [
786
                        [
787
                            'text' => '1',
788
                            'callback_data' => $prefix . '/1'
789
                        ],
790
                        [
791
                            'text' => '2',
792
                            'callback_data' => $prefix . '/2'
793
                        ],
794
                        [
795
                            'text' => '3',
796
                            'callback_data' => $prefix . '/3'
797
                        ],
798
                        [
799
                            'text' => '4',
800
                            'callback_data' => $prefix . '/4'
801
                        ],
802
                        [
803
                            'text' => '• 5 •',
804
                            'callback_data' => 'null'
805
                        ]
806
                    ];
807
            } else {
808
                if ($index < $list - 2) {
809
                    $indexm = $index - 1;
810
                    $indexp = $index + 1;
811
                    $buttons = [
812
                            [
813
                                'text' => '‹‹ 1',
814
                                'callback_data' => $prefix . '/1'
815
                            ],
816
                            [
817
                                'text' => '‹ ' . $indexm,
818
                                'callback_data' => $prefix . '/' . $indexm
819
                            ],
820
                            [
821
                                'text' => '• ' . $index . ' •',
822
                                'callback_data' => 'null',
823
                            ],
824
                            [
825
                                'text' => $indexp . ' ›',
826
                                'callback_data' => $prefix . '/' . $indexp
827
                            ],
828
                            [
829
                                'text' => $list . ' ››',
830
                                'callback_data' => $prefix . '/' . $list
831
                            ]
832
                        ];
833
                } elseif ($index == ($list - 2)) {
834
                    $indexm = $index - 1;
835
                    $indexp = $index + 1;
836
                    $buttons = [
837
                            [
838
                                'text' => '‹‹1',
839
                                'callback_data' => $prefix . '/1'
840
                            ],
841
                            [
842
                                'text' => '' . $indexm,
843
                                'callback_data' => $prefix . '/' . $indexm
844
                            ],
845
                            [
846
                                'text' => '• ' . $index . ' •',
847
                                'callback_data' => 'null',
848
                            ],
849
                            [
850
                                'text' => '' . $indexp,
851
                                'callback_data' => $prefix . '/' . $indexp
852
                            ],
853
                            [
854
                                'text' => "$list",
855
                                'callback_data' => $prefix . "/$list"
856
                            ]
857
                        ];
858
                } elseif ($index == ($list - 1)) {
859
                    $indexm = $index - 1;
860
                    $indexmm = $index - 2;
861
                    $buttons = [
862
                            [
863
                                'text' => '‹‹ 1',
864
                                'callback_data' => $prefix . '/1'
865
                            ],
866
                            [
867
                                'text' => '‹ ' . $indexmm,
868
                                'callback_data' => $prefix . '/' . $indexmm
869
                            ],
870
                            [
871
                                'text' => '' . $indexm,
872
                                'callback_data' => $prefix . '/' . $indexm
873
                            ],
874
                            [
875
                                'text' => '• ' . $index . ' •',
876
                                'callback_data' => $prefix . '/' . $index
877
                            ],
878
                            [
879
                                'text' => "$list",
880
                                'callback_data' => $prefix . "/$list"
881
                            ]
882
                        ];
883
                } else if ($index == $list) {
884
                    $indexm = $index - 1;
885
                    $indexmm = $index - 2;
886
                    $indexmmm = $index - 3;
887
                    $buttons = [
888
                            [
889
                                'text' => '‹‹ 1',
890
                                'callback_data' => $prefix . '/1'
891
                            ],
892
                            [
893
                                'text' => '‹ ' . $indexmmm,
894
                                'callback_data' => $prefix . '/' . $indexmmm
895
                            ],
896
                            [
897
                                'text' => '' . $indexmm,
898
                                'callback_data' => $prefix . '/' . $indexmm,
899
                            ],
900
                            [
901
                                'text' => '' . $indexm,
902
                                'callback_data' => $prefix . '/' . $indexm
903
                            ],
904
                            [
905
                                'text' => '• ' . $index . ' •',
906
                                'callback_data' => $prefix . '/' . $index
907
                            ]
908
                        ];
909
                }
910
            }
911
        }
912
913
        // If there are other buttons in this row (checking the column)
914
        if ($this->column !== 0) {
915
916
            // Go to the next
917
            $this->changeRow();
918
919
        }
920
921
        $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...
922
923
        // We added a row
924
        $this->changeRow();
925
926
    }
927
928
}
929
930
/** @} */
931