Completed
Pull Request — master (#20)
by
unknown
03:12
created

InlineKeyboard::getArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 7

Duplication

Lines 20
Ratio 100 %

Importance

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