ReplyKeyboardMarkup   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 22
c 1
b 0
f 0
dl 0
loc 59
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A removeRow() 0 7 2
A removeButton() 0 8 2
A addRow() 0 8 2
A addButton() 0 11 3
1
<?php
2
3
4
namespace Sysbot\Telegram\ExtendedTypes;
5
6
7
use Sysbot\Telegram\Exceptions\InvalidArgumentException;
8
use Sysbot\Telegram\Types\KeyboardButton;
9
10
trait ReplyKeyboardMarkup
11
{
12
13
    /**
14
     * @param int $row
15
     * @param KeyboardButton $button
16
     * @param bool $first
17
     * @return $this
18
     */
19
    public function addButton(int $row, KeyboardButton $button, bool $first = false): static
20
    {
21
        if (empty($this->keyboard[$row])) {
22
            throw new InvalidArgumentException('Row not found');
23
        }
24
        if ($first) {
25
            array_unshift($this->keyboard[$row], $button);
26
            return $this;
27
        }
28
        array_push($this->keyboard[$row], $button);
29
        return $this;
30
    }
31
32
    /**
33
     * @param int $row
34
     * @param int $index
35
     * @return $this
36
     */
37
    public function removeButton(int $row, int $index): static
38
    {
39
        if (empty($this->keyboard[$row][$index] ?? null)) {
40
            return $this;
41
        }
42
        unset($this->keyboard[$row][$index]);
43
        $this->keyboard[$row] = array_values($this->keyboard[$row]);
44
        return $this;
45
    }
46
47
    /**
48
     * @param KeyboardButton[] $buttons
49
     * @param bool $first
50
     * @return $this
51
     */
52
    public function addRow(array $buttons, bool $first = false): static
53
    {
54
        if ($first) {
55
            array_unshift($this->keyboard, $buttons);
56
            return $this;
57
        }
58
        array_push($this->keyboard, $buttons);
59
        return $this;
60
    }
61
62
    public function removeRow(int $index): static
63
    {
64
        if (empty($this->keyboard[$index])) {
65
            return $this;
66
        }
67
        unset($this->keyboard[$index]);
68
        return $this;
69
    }
70
71
}