Completed
Pull Request — master (#194)
by
unknown
03:16 queued 01:09
created

ToggableTrait::toggle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace PhpSchool\CliMenu\MenuItem;
4
5
use PhpSchool\CliMenu\MenuStyle;
6
use PhpSchool\CliMenu\Util\StringUtil;
7
8
trait ToggableTrait
9
{
10
    /**
11
     * @var bool
12
     */
13
    private $checked = false;
14
15
    /**
16
     * The output text for the item
17
     *
18
     * @param MenuStyle $style
19
     * @param bool $selected Currently unused in this class
20
     * @return array
21
     */
22
    public function getRows(MenuStyle $style, bool $selected = false) : array
0 ignored issues
show
Unused Code introduced by
The parameter $selected is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

22
    public function getRows(MenuStyle $style, /** @scrutinizer ignore-unused */ bool $selected = false) : array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
23
    {
24
        $markerTypes = [
25
            true => $this instanceof CheckableItem
26
                ? $style->getCheckedMarker()
27
                : $style->getRadioMarker(),
28
            false => $this instanceof CheckableItem
29
                ? $style->getUncheckedMarker()
30
                : $style->getUnradioMarker(),
31
        ];
32
33
        $marker = sprintf("%s", $markerTypes[$this->checked]);
34
35
        $length = $style->getDisplaysExtra()
36
            ? $style->getContentWidth() - (mb_strlen($style->getItemExtra()) + 2)
37
            : $style->getContentWidth();
38
39
        $rows = explode(
40
            "\n",
41
            StringUtil::wordwrap(
42
                sprintf('%s%s', $marker, $this->text),
43
                $length,
44
                sprintf("\n%s", str_repeat(' ', mb_strlen($marker)))
45
            )
46
        );
47
48
        return array_map(function ($row, $key) use ($style, $length) {
49
            $text = $this->disabled ? $style->getDisabledItemText($row) : $row;
50
51
            if ($key === 0) {
52
                return $this->showItemExtra
53
                    ? sprintf('%s%s  %s', $text, str_repeat(' ', $length - mb_strlen($row)), $style->getItemExtra())
54
                    : $text;
55
            }
56
57
            return $text;
58
        }, $rows, array_keys($rows));
59
    }
60
61
    /**
62
     * Toggles checked state
63
     */
64
    public function toggle() : void
65
    {
66
        $this->checked = !$this->checked;
67
    }
68
69
    /**
70
     * Sets checked state to true
71
     */
72
    public function setChecked() : void
73
    {
74
        $this->checked = true;
75
    }
76
77
    /**
78
     * Sets checked state to false
79
     */
80
    public function setUnchecked() : void
81
    {
82
        $this->checked = false;
83
    }
84
85
    public function getChecked() : bool
86
    {
87
        return $this->checked;
88
    }
89
}
90