Completed
Push — master ( 640da7...eb7508 )
by Danilo
03:54
created

InlineKeyboard::changeRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework\Entities;
20
21
use PhpBotFramework\Exceptions\BotException;
22
23
/**
24
 * \addtogroup Entities Entities
25
 * @{
26
 */
27
28
/** \class InlineKeyboard
29
 * \brief Inline Keyboard handler that create and handle inline keyboard buttons.
30
 * \details It stores the inline keyboard buttons added until get() is called.
31
 * It also provides some basic button to get, like Menu and Back buttons plus the dynamic-keyboard for menu browsing.
32
 */
33
class InlineKeyboard
34
{
35
    /**
36
     * \addtogroup InlineKeyboard InlineKeyboard
37
     * \brief Handle an inline keyboard to send along with messages.
38
     * @{
39
     */
40
41
    /** \brief Store the array of InlineKeyboardButton */
42
    protected $inline_keyboard;
43
44
    /** \brief Store the current row. */
45
    private $row;
46
47
    /** \brief Store the current column. */
48
    private $column;
49
50
    /**
51
     * \brief Create an inline keyboard object.
52
     * @param array $buttons Buttons passed as inizialization.
53
     */
54
    public function __construct(
55
        array $buttons = array()
56
    ) {
57
        // If $buttons is empty, initialize it with an empty array
58
        $this->inline_keyboard = $buttons;
59
60
        // Set up vars
61
        $this->row = 0;
62
        $this->column = 0;
63
    }
64
65
    /**
66
     * \brief Get a JSON-serialized object containg the inline keyboard.
67
     * @param bool $clear_keyboard Remove all the buttons from this object.
68
     * @return string JSON-serialized string with the buttons.
69
     */
70 View Code Duplication
    public function get(bool $clear_keyboard = true) : string
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...
71
    {
72
        // Check if it is empty
73
        if (empty($this->inline_keyboard)) {
74
            throw new BotException("Inline keyboard is empty");
75
        }
76
77
        // Create a new array to put our buttons
78
        $reply_markup = ['inline_keyboard' => $this->inline_keyboard];
79
80
        // Encode the array in a json object
81
        $reply_markup = json_encode($reply_markup);
82
83
        if ($clear_keyboard) {
84
            $this->clearKeyboard();
85
        }
86
87
        return $reply_markup;
88
    }
89
90
    /**
91
     * \brief Get the array containing the buttons. (Use this method when adding keyboard to inline query results)
92
     * @param bool $clean_keyboard Remove all the button from this object.
93
     * @return array An array containing the buttons.
94
     */
95 View Code Duplication
    public function getArray(bool $clean_keyboard = true) : array
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...
96
    {
97
        // Check if it is empty
98
        if (empty($this->inline_keyboard)) {
99
            throw new BotException("Inline keyboard is empty");
100
        }
101
102
        // Create a new array to put the buttons
103
        $reply_markup = ['inline_keyboard' => $this->inline_keyboard];
104
105
        if ($clean_keyboard) {
106
            $this->clearKeyboard();
107
        }
108
109
        return $reply_markup;
110
    }
111
112
    /** \brief Add buttons for the current row of buttons
113
     * \details Each array sent as parameter require a text key
114
     * and one another key (as specified <a href="https://core.telegram.org/bots/api/#inlinekeyboardbutton" target="blank">here</a> between:
115
     * - url
116
     * - callback_data
117
     * - switch_inline_query
118
     * - switch_inline_query_current_chat
119
     * - callback_game
120
     *
121
     * Each call to this function add one or more button to a row. The next call add buttons on the next row.
122
     * Each row allows 8 buttons per row and 12 columns total.
123
     * Use this function with this syntax:
124
     *
125
     *     addLevelButtons(['text' => 'Click me!', 'url' => 'https://telegram.me']);
126
     *
127
     * If you want to add more than a button, use this syntax:
128
     *
129
     *     addLevelButtons(['text' => 'Button 1', 'url' => 'https://telegram.me/gamedev_ita'], ['text' => 'Button 2', 'url' => 'https://telegram.me/animewallpaper']);
130
     *
131
     * @param array ...$buttons One or more arrays, each one represent a button.
132
     */
133
    public function addLevelButtons(array ...$buttons)
134
    {
135
        // If the user has already added a button in this row
136
        if ($this->column != 0) {
137
             // Change row
138
             $this->changeRow();
139
        }
140
141
        // Add buttons to the next row
142
        $this->inline_keyboard[] = $buttons;
143
144
        // Switch to the next row
145
        $this->changeRow();
146
    }
147
148
    /** \brief Add a button.
149
     * \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.
150
     *
151
     * Each row allows 8 buttons per row and 12 columns total.
152
     * Use this function with this syntax:
153
     *
154
     *     addButton('Click me!', 'url', 'https://telegram.me');
155
     *
156
     * @param string $text Text showed on the button.
157
     * @param string $data_type The type of the button data.
158
     * Select one from these types.
159
     * - url
160
     * - callback_data
161
     * - switch_inline_query
162
     * - switch_inline_query_current_chat
163
     * - callback_game
164
     * @param string $data Data for the type selected.
165
     */
166
    public function addButton(string $text, string $data_type, string $data)
167
    {
168
        // If we get the end of the row
169
        if ($this->column == 8) {
170
            $this->changeRow();
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
     * \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
187
        // Reset vars
188
        $this->row++;
189
        $this->column = 0;
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
     * \brief */
205
    public function addListKeyboard(int $index, int $list, $prefix = 'list')
206
    {
207
        if (($list > 0) && ($index >= 0)) {
208
            if ($index == 0) {
209
                if ($list > 1) {
210
                    if ($list > 2) {
211
                        if ($list > 3) {
212
                            if ($list > 4) {
213
                                if ($list > 5) {
214
                                    $buttons = [
215
                                            [
216
                                                'text' => '1',
217
                                                'callback_data' => $prefix . '/1'
218
                                            ],
219
                                            [
220
                                                'text' => '2',
221
                                                'callback_data' => $prefix . '/2'
222
                                            ],
223
                                            [
224
                                                'text' => '3',
225
                                                'callback_data' => $prefix . '/3'
226
                                            ],
227
                                            [
228
                                                'text' => '4 ›',
229
                                                'callback_data' => $prefix . '/4'
230
                                            ],
231
                                            [
232
                                                'text' => "$list ››",
233
                                                'callback_data' => $prefix . "/$list"
234
                                            ]
235
                                        ];
236
                                } else {
237
                                    $buttons = [
238
                                            [
239
                                                'text' => '1',
240
                                                'callback_data' => $prefix . '/1'
241
                                            ],
242
                                            [
243
                                                'text' => '2',
244
                                                'callback_data' => $prefix . '/2'
245
                                            ],
246
                                            [
247
                                                'text' => '3',
248
                                                'callback_data' => $prefix . '/3'
249
                                            ],
250
                                            [
251
                                                'text' => '4',
252
                                                'callback_data' => $prefix . '/4'
253
                                            ],
254
                                            [
255
                                                'text' => '5',
256
                                                'callback_data' => $prefix . '/5'
257
                                            ]
258
                                        ];
259
                                }
260
                            } else {
261
                                $buttons = [
262
                                        [
263
                                            'text' => '1',
264
                                            'callback_data' => $prefix . '/1'
265
                                        ],
266
                                        [
267
                                            'text' => '2',
268
                                            'callback_data' => $prefix . '/2'
269
                                        ],
270
                                        [
271
                                            'text' => '3',
272
                                            'callback_data' => $prefix . '/3'
273
                                        ],
274
                                        [
275
                                            'text' => '4',
276
                                            'callback_data' => $prefix . '/4'
277
                                        ],
278
                                    ];
279
                            }
280
                        } else {
281
                            $buttons = [
282
                                    [
283
                                        'text' => '1',
284
                                        'callback_data' => $prefix . '/1'
285
                                    ],
286
                                    [
287
                                        'text' => '2',
288
                                        'callback_data' => $prefix . '/2'
289
                                    ],
290
                                    [
291
                                        'text' => '3',
292
                                        'callback_data' => $prefix . '/3'
293
                                    ],
294
                                ];
295
                        }
296
                    } elseif ($list == 2) {
297
                        $buttons = [
298
                                [
299
                                    'text' => '1',
300
                                    'callback_data' => $prefix . '/1'
301
                                ],
302
                                [
303
                                    'text' => '2',
304
                                    'callback_data' => $prefix . '/2'
305
                                ],
306
                            ];
307
                    }
308
                } else {
309
                    $buttons = [
310
                            [
311
                                'text' => '1',
312
                                'callback_data' => $prefix . '/1'
313
                            ]
314
                    ];
315
                }
316
            } elseif ($index == 1) {
317
                if ($list > 1) {
318 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...
319
                        if ($list > 3) {
320
                            if ($list > 4) {
321
                                if ($list > 5) {
322
                                    $buttons = [
323
                                            [
324
                                                'text' => '• 1 •',
325
                                                'callback_data' => 'null'
326
                                            ],
327
                                            [
328
                                                'text' => '2',
329
                                                'callback_data' => $prefix . '/2'
330
                                            ],
331
                                            [
332
                                                'text' => '3',
333
                                                'callback_data' => $prefix . '/3'
334
                                            ],
335
                                            [
336
                                                'text' => '4 ›',
337
                                                'callback_data' => $prefix . '/4'
338
                                            ],
339
                                            [
340
                                                'text' => "$list ››",
341
                                                'callback_data' => $prefix . "/$list"
342
                                            ]
343
                                        ];
344
                                } else {
345
                                    $buttons = [
346
                                            [
347
                                                'text' => '• 1 •',
348
                                                'callback_data' => 'null'
349
                                            ],
350
                                            [
351
                                                'text' => '2',
352
                                                'callback_data' => $prefix . '/2'
353
                                            ],
354
                                            [
355
                                                'text' => '3',
356
                                                'callback_data' => $prefix . '/3'
357
                                            ],
358
                                            [
359
                                                'text' => '4',
360
                                                'callback_data' => $prefix . '/4'
361
                                            ],
362
                                            [
363
                                                'text' => '5',
364
                                                'callback_data' => $prefix . '/5'
365
                                            ]
366
                                        ];
367
                                }
368
                            } else {
369
                                $buttons = [
370
                                        [
371
                                            'text' => '• 1 •',
372
                                                'callback_data' => 'null'
373
                                        ],
374
                                        [
375
                                                'text' => '2',
376
                                                'callback_data' => $prefix . '/2'
377
                                        ],
378
                                        [
379
                                                'text' => '3',
380
                                                'callback_data' => $prefix . '/3'
381
                                        ],
382
                                        [
383
                                                'text' => '4',
384
                                                'callback_data' => $prefix . '/4'
385
                                        ]
386
                                    ];
387
                            }
388
                        } else {
389
                            $buttons = [
390
                                    [
391
                                        'text' => '• 1 •',
392
                                        'callback_data' => 'null'
393
                                    ],
394
                                    [
395
                                        'text' => '2',
396
                                        'callback_data' => $prefix . '/2'
397
                                    ],
398
                                    [
399
                                        'text' => '3',
400
                                        'callback_data' => $prefix . '/3'
401
                                    ]
402
                                ];
403
                        }
404
                    } elseif ($list == 2) {
405
                        $buttons = [
406
                                [
407
                                    'text' => '• 1 •',
408
                                    'callback_data' => 'null'
409
                                ],
410
                                [
411
                                    'text' => '2',
412
                                    'callback_data' => $prefix . '/2'
413
                                ]
414
                            ];
415
                    }
416
                } else {
417
                    $buttons = [
418
                            [
419
                                'text' => '• 1 •',
420
                                'callback_data' => 'null'
421
                            ]
422
                        ];
423
                }
424 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...
425
                if ($list > 3) {
426
                    if ($list > 4) {
427
                        if ($list > 5) {
428
                            $buttons = [
429
                                    [
430
                                        'text' => '1',
431
                                        'callback_data' => $prefix . '/1'
432
                                    ],
433
                                    [
434
                                        'text' => '• 2 •',
435
                                        'callback_data' => 'null'
436
                                    ],
437
                                    [
438
                                        'text' => '3',
439
                                        'callback_data' => $prefix . '/3'
440
                                    ],
441
                                    [
442
                                        'text' => '4 ›',
443
                                        'callback_data' => $prefix . '/4'
444
                                    ],
445
                                    [
446
                                        'text' => "$list ››",
447
                                        'callback_data' => $prefix . "/$list"
448
                                    ]
449
                                ];
450
                        } else {
451
                            $buttons = [
452
                                    [
453
                                        'text' => '1',
454
                                        'callback_data' => $prefix . '/1'
455
                                    ],
456
                                    [
457
                                        'text' => '• 2 •',
458
                                        'callback_data' => 'null'
459
                                    ],
460
                                    [
461
                                        'text' => '3',
462
                                        'callback_data' => $prefix . '/3'
463
                                    ],
464
                                    [
465
                                        'text' => '4',
466
                                        'callback_data' => '4'
467
                                    ],
468
                                    [
469
                                        'text' => '5',
470
                                        'callback_data' => $prefix . '/5'
471
                                    ]
472
                                ];
473
                        }
474
                    } else {
475
                        $buttons = [
476
                                [
477
                                    'text' => '1',
478
                                    'callback_data' => $prefix . '/1'
479
                                ],
480
                                [
481
                                    'text' => '• 2 •',
482
                                    'callback_data' => 'null'
483
                                ],
484
                                [
485
                                    'text' => '3',
486
                                    'callback_data' => $prefix . '/3'
487
                                ],
488
                                [
489
                                    'text' => '4',
490
                                    'callback_data' => $prefix . '/4'
491
                                ]
492
                            ];
493
                    }
494
                } elseif ($list == 3) {
495
                    $buttons = [
496
                            [
497
                                'text' => '1',
498
                                'callback_data' => $prefix . '/1'
499
                            ],
500
                            [
501
                                'text' => '• 2 •',
502
                                'callback_data' => 'null'
503
                            ],
504
                            [
505
                                'text' => '3',
506
                                'callback_data' => $prefix . '/3'
507
                            ]
508
                        ];
509
                } else {
510
                    $buttons = [
511
                            [
512
                                'text' => '1',
513
                                'callback_data' => $prefix . '/1'
514
                            ],
515
                            [
516
                                'text' => '• 2 •',
517
                                'callback_data' => 'null'
518
                            ]
519
                        ];
520
                }
521
            } elseif ($index == 3) {
522
                if ($list > 4) {
523
                    if ($list > 5) {
524
                        $buttons = [
525
                                [
526
                                    'text' => '1',
527
                                    'callback_data' => $prefix . '/1'
528
                                ],
529
                                [
530
                                    'text' => '2',
531
                                    'callback_data' => $prefix . '/2'
532
                                ],
533
                                [
534
                                    'text' => '• 3 •',
535
                                    'callback_data' => 'null'
536
                                ],
537
                                [
538
                                    'text' => '4 ›',
539
                                    'callback_data' => $prefix . '/4'
540
                                ],
541
                                [
542
                                    'text' => "$list ››",
543
                                    'callback_data' => $prefix . "/$list"
544
                                ]
545
                            ];
546
                    } else {
547
                        $buttons = [
548
                                [
549
                                    'text' => '1',
550
                                    'callback_data' => $prefix . '/1'
551
                                ],
552
                                [
553
                                    'text' => '2',
554
                                    'callback_data' => $prefix . '/2'
555
                                ],
556
                                [
557
                                    'text' => '• 3 •',
558
                                    'callback_data' => 'null'
559
                                ],
560
                                [
561
                                    'text' => '4',
562
                                    'callback_data' => $prefix . '/4'
563
                                ],
564
                                [
565
                                    'text' => '5',
566
                                    'callback_data' => $prefix . '/5'
567
                                ]
568
                            ];
569
                    }
570
                } elseif ($list == 4) {
571
                    $buttons = [
572
                            [
573
                                'text' => '1',
574
                                'callback_data' => $prefix . '/1'
575
                            ],
576
                            [
577
                                'text' => '2',
578
                                'callback_data' => $prefix . '/2'
579
                            ],
580
                            [
581
                                'text' => '• 3 •',
582
                                'callback_data' => 'null'
583
                            ],
584
                            [
585
                                'text' => '4',
586
                                'callback_data' => $prefix . '/4'
587
                            ]
588
                        ];
589
                } else {
590
                    $buttons = [
591
                            [
592
                                'text' => '1',
593
                                'callback_data' => $prefix . '/1'
594
                            ],
595
                            [
596
                                'text' => '2',
597
                                'callback_data' => $prefix . '/2'
598
                            ],
599
                            [
600
                                'text' => '• 3 •',
601
                                'callback_data' => 'null'
602
                            ]
603
                        ];
604
                }
605
            } elseif ($index == 4 && $list <= 5) {
606
                if ($list == 4) {
607
                    $buttons = [
608
                            [
609
                                'text' => '1',
610
                                'callback_data' => $prefix . '/1'
611
                            ],
612
                            [
613
                                'text' => '2',
614
                                'callback_data' => $prefix . '/2'
615
                            ],
616
                            [
617
                                'text' => '3',
618
                                'callback_data' => $prefix . '/3'
619
                            ],
620
                            [
621
                                'text' => '• 4 •',
622
                                'callback_data' => 'null'
623
                            ]
624
                        ];
625
                } elseif ($list == 5) {
626
                    $buttons = [
627
                            [
628
                                'text' => '1',
629
                                'callback_data' => $prefix . '/1'
630
                            ],
631
                            [
632
                                'text' => '2',
633
                                'callback_data' => $prefix . '/2'
634
                            ],
635
                            [
636
                                'text' => '3',
637
                                'callback_data' => $prefix . '/3'
638
                            ],
639
                            [
640
                                'text' => '• 4 •',
641
                                'callback_data' => 'null'
642
                            ],
643
                            [
644
                                'text' => '5',
645
                                'callback_data' => $prefix . '/5'
646
                            ]
647
                        ];
648
                }
649
            } elseif ($index == 5 && $list == 5) {
650
                $buttons = [
651
                        [
652
                            'text' => '1',
653
                            'callback_data' => $prefix . '/1'
654
                        ],
655
                        [
656
                            'text' => '2',
657
                            'callback_data' => $prefix . '/2'
658
                        ],
659
                        [
660
                            'text' => '3',
661
                            'callback_data' => $prefix . '/3'
662
                        ],
663
                        [
664
                            'text' => '4',
665
                            'callback_data' => $prefix . '/4'
666
                        ],
667
                        [
668
                            'text' => '• 5 •',
669
                            'callback_data' => 'null'
670
                        ]
671
                    ];
672
            } else {
673
                if ($index < $list - 2) {
674
                    $indexm = $index - 1;
675
                    $indexp = $index + 1;
676
                    $buttons = [
677
                            [
678
                                'text' => '‹‹ 1',
679
                                'callback_data' => $prefix . '/1'
680
                            ],
681
                            [
682
                                'text' => '‹ ' . $indexm,
683
                                'callback_data' => $prefix . '/' . $indexm
684
                            ],
685
                            [
686
                                'text' => '• ' . $index . ' •',
687
                                'callback_data' => 'null',
688
                            ],
689
                            [
690
                                'text' => $indexp . ' ›',
691
                                'callback_data' => $prefix . '/' . $indexp
692
                            ],
693
                            [
694
                                'text' => $list . ' ››',
695
                                'callback_data' => $prefix . '/' . $list
696
                            ]
697
                        ];
698
                } elseif ($index == ($list - 2)) {
699
                    $indexm = $index - 1;
700
                    $indexp = $index + 1;
701
                    $buttons = [
702
                            [
703
                                'text' => '‹‹1',
704
                                'callback_data' => $prefix . '/1'
705
                            ],
706
                            [
707
                                'text' => '' . $indexm,
708
                                'callback_data' => $prefix . '/' . $indexm
709
                            ],
710
                            [
711
                                'text' => '• ' . $index . ' •',
712
                                'callback_data' => 'null',
713
                            ],
714
                            [
715
                                'text' => '' . $indexp,
716
                                'callback_data' => $prefix . '/' . $indexp
717
                            ],
718
                            [
719
                                'text' => "$list",
720
                                'callback_data' => $prefix . "/$list"
721
                            ]
722
                        ];
723
                } elseif ($index == ($list - 1)) {
724
                    $indexm = $index - 1;
725
                    $indexmm = $index - 2;
726
                    $buttons = [
727
                            [
728
                                'text' => '‹‹ 1',
729
                                'callback_data' => $prefix . '/1'
730
                            ],
731
                            [
732
                                'text' => '‹ ' . $indexmm,
733
                                'callback_data' => $prefix . '/' . $indexmm
734
                            ],
735
                            [
736
                                'text' => '' . $indexm,
737
                                'callback_data' => $prefix . '/' . $indexm
738
                            ],
739
                            [
740
                                'text' => '• ' . $index . ' •',
741
                                'callback_data' => $prefix . '/' . $index
742
                            ],
743
                            [
744
                                'text' => "$list",
745
                                'callback_data' => $prefix . "/$list"
746
                            ]
747
                        ];
748
                } elseif ($index == $list) {
749
                    $indexm = $index - 1;
750
                    $indexmm = $index - 2;
751
                    $indexmmm = $index - 3;
752
                    $buttons = [
753
                            [
754
                                'text' => '‹‹ 1',
755
                                'callback_data' => $prefix . '/1'
756
                            ],
757
                            [
758
                                'text' => '‹ ' . $indexmmm,
759
                                'callback_data' => $prefix . '/' . $indexmmm
760
                            ],
761
                            [
762
                                'text' => '' . $indexmm,
763
                                'callback_data' => $prefix . '/' . $indexmm,
764
                            ],
765
                            [
766
                                'text' => '' . $indexm,
767
                                'callback_data' => $prefix . '/' . $indexm
768
                            ],
769
                            [
770
                                'text' => '• ' . $index . ' •',
771
                                'callback_data' => $prefix . '/' . $index
772
                            ]
773
                        ];
774
                }
775
            }
776
        }
777
778
        // If there are other buttons in this row (checking the column)
779
        if ($this->column !== 0) {
780
            // Go to the next
781
            $this->changeRow();
782
        }
783
784
        $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...
785
786
        // We added a row
787
        $this->changeRow();
788
    }
789
790
    /** @} */
791
792
    /** @} */
793
}
794