Passed
Pull Request — master (#1)
by Aydin
02:24
created

InputCharacter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 115
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getControl() 0 7 2
A __construct() 0 3 1
A get() 0 3 1
A isNotControl() 0 3 1
A isControl() 0 3 1
A __toString() 0 3 1
A getControls() 0 3 1
A fromControlName() 0 7 2
A controlExists() 0 3 1
1
<?php
2
3
namespace PhpSchool\Terminal;
4
5
use function in_array;
6
7
/**
8
 * @author Aydin Hassan <[email protected]>
9
 */
10
class InputCharacter
11
{
12
    /**
13
     * @var string
14
     */
15
    private $data;
16
17
    public const UP = 'UP';
18
    public const DOWN = 'DOWN';
19
    public const RIGHT = 'RIGHT';
20
    public const LEFT = 'LEFT';
21
    public const CTRLA = 'CTRLA';
22
    public const CTRLB = 'CTRLB';
23
    public const CTRLE = 'CTRLE';
24
    public const CTRLF = 'CTRLF';
25
    public const BACKSPACE = 'BACKSPACE';
26
    public const CTRLW = 'CTRLW';
27
    public const ENTER = 'ENTER';
28
    public const TAB = 'TAB';
29
30
    private static $controls = [
31
        "\033[A" => self::UP,
32
        "\033[B" => self::DOWN,
33
        "\033[C" => self::RIGHT,
34
        "\033[D" => self::LEFT,
35
        "\001"   => self::CTRLA,
36
        "\002"   => self::CTRLB,
37
        "\005"   => self::CTRLE,
38
        "\006"   => self::CTRLF,
39
        "\010"   => self::BACKSPACE,
40
        "\177"   => self::BACKSPACE,
41
        "\027"   => self::CTRLW,
42
        "\n"     => self::ENTER,
43
        "\t"     => self::TAB,
44
    ];
45
46
    public function __construct(string $data)
47
    {
48
        $this->data = $data;
49
    }
50
51
    /**
52
     * Is this character a control sequence?
53
     */
54
    public function isControl() : bool
55
    {
56
        return isset(static::$controls[$this->data]);
0 ignored issues
show
Bug introduced by
Since $controls is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $controls to at least protected.
Loading history...
57
    }
58
59
    /**
60
     * Is this character a normal character?
61
     */
62
    public function isNotControl() : bool
63
    {
64
        return ! $this->isControl();
65
    }
66
67
    /**
68
     * Get the raw character or control sequence
69
     */
70
    public function get() : string
71
    {
72
        return $this->data;
73
    }
74
75
    /**
76
     * Get the actual control name that this sequence represents.
77
     * One of the class constants. Eg. self::UP.
78
     *
79
     * Throws an exception if the character is not actually a control sequence
80
     */
81
    public function getControl() : string
82
    {
83
        if (!isset(static::$controls[$this->data])) {
0 ignored issues
show
Bug introduced by
Since $controls is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $controls to at least protected.
Loading history...
84
            throw new \RuntimeException(sprintf('Character "%s" is not a control', $this->data));
85
        }
86
87
        return static::$controls[$this->data];
88
    }
89
90
    /**
91
     * Get the raw character or control sequence
92
     */
93
    public function __toString() : string
94
    {
95
        return $this->get();
96
    }
97
98
    /**
99
     * Does the given control name exist? eg self::UP.
100
     */
101
    public static function controlExists(string $controlName) : bool
102
    {
103
        return in_array($controlName, static::$controls, true);
0 ignored issues
show
Bug introduced by
Since $controls is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $controls to at least protected.
Loading history...
104
    }
105
106
    /**
107
     * Get all of the available control names
108
     */
109
    public static function getControls() : array
110
    {
111
        return array_values(array_unique(static::$controls));
0 ignored issues
show
Bug introduced by
Since $controls is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $controls to at least protected.
Loading history...
112
    }
113
114
    /**
115
     * Create a instance from a given control name. Throws an exception if the
116
     * control name does not exist.
117
     */
118
    public static function fromControlName(string $controlName) : self
119
    {
120
        if (!static::controlExists($controlName)) {
121
            throw new \InvalidArgumentException(sprintf('Control "%s" does not exist', $controlName));
122
        }
123
124
        return new static(array_search($controlName, static::$controls, true));
0 ignored issues
show
Bug introduced by
It seems like array_search($controlName, static::controls, true) can also be of type false; however, parameter $data of PhpSchool\Terminal\InputCharacter::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

124
        return new static(/** @scrutinizer ignore-type */ array_search($controlName, static::$controls, true));
Loading history...
Bug introduced by
Since $controls is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $controls to at least protected.
Loading history...
125
    }
126
}
127