Completed
Push — master ( 16129c...d528e9 )
by Danilo
05:45
created

InlineKeyboard   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 763
Duplicated Lines 31.45 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 12.21%

Importance

Changes 0
Metric Value
wmc 50
lcom 1
cbo 2
dl 240
loc 763
ccs 32
cts 262
cp 0.1221
rs 8
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A get() 19 19 3
A getArray() 16 16 3
A addLevelButtons() 0 14 2
A addButton() 0 13 2
A changeRow() 0 7 1
A clearKeyboard() 0 9 1
D addListKeyboard() 196 584 37

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like InlineKeyboard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use InlineKeyboard, and based on these observations, apply Extract Interface, too.

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