GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 3657b2...7baa11 )
by Freek
01:28
created

Terminal   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 5
dl 0
loc 145
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A on() 0 8 1
A onKeyPress() 0 9 1
A emptyLine() 0 6 1
A comment() 0 6 1
A write() 0 15 2
A displayScreen() 0 20 2
A goBack() 0 12 2
A getPreviousScreen() 0 4 1
A refreshScreen() 0 10 2
A isDisplayingScreen() 0 8 2
A removeAllListeners() 0 6 1
A prompt() 0 6 1
A clearPrompt() 0 6 1
1
<?php
2
3
namespace Spatie\PhpUnitWatcher;
4
5
use Clue\React\Stdio\Stdio;
6
use Spatie\PhpUnitWatcher\Screens\Screen;
7
use Symfony\Component\Console\Formatter\OutputFormatter;
8
9
class Terminal
10
{
11
    /** @var \Clue\React\Stdio\Stdio */
12
    protected $io;
13
14
    /** @var \Spatie\PhpUnitWatcher\Screens\Screen */
15
    protected $previousScreen = null;
16
17
    /** @var \Spatie\PhpUnitWatcher\Screens\Screen */
18
    protected $currentScreen = null;
19
20
    public function __construct(Stdio $io)
21
    {
22
        $this->io = $io;
23
    }
24
25
    public function on(string $eventName, callable $callable)
26
    {
27
        $this->io->on($eventName, function ($line) use ($callable) {
28
            $callable(trim($line));
29
        });
30
31
        return $this;
32
    }
33
34
    public function onKeyPress(callable $callable)
35
    {
36
        $this->io->getInput()->once('data', function ($line) use ($callable) {
37
            $this->io->getReadline()->deleteChar(0);
38
            $callable(trim($line));
39
        });
40
41
        return $this;
42
    }
43
44
    public function emptyLine()
45
    {
46
        $this->write('');
47
48
        return $this;
49
    }
50
51
    public function comment(string $message)
52
    {
53
        $this->write($message, 'comment');
54
55
        return $this;
56
    }
57
58
    public function write(string $message = '', $level = null)
59
    {
60
        if ($level != '') {
61
            $message = "<{$level}>$message</{$level}>";
62
        }
63
64
        $formattedMessage = (new OutputFormatter(true))->format($message);
65
66
        $formattedMessage = str_replace('<dim>', "\e[2m", $formattedMessage);
67
        $formattedMessage = str_replace('</dim>', "\e[22m", $formattedMessage);
68
69
        $this->io->writeln($formattedMessage);
70
71
        return $this;
72
    }
73
74
    public function displayScreen(Screen $screen, $clearScreen = true)
75
    {
76
        $this->previousScreen = $this->currentScreen;
77
78
        $this->currentScreen = $screen;
79
80
        $screen
81
            ->useTerminal($this)
82
            ->clearPrompt()
83
            ->removeAllListeners()
84
            ->registerListeners();
85
86
        if ($clearScreen) {
87
            $screen->clear();
88
        }
89
90
        $screen->draw();
91
92
        return $this;
93
    }
94
95
    public function goBack()
96
    {
97
        if (is_null($this->previousScreen)) {
98
            return;
99
        }
100
101
        $this->currentScreen = $this->previousScreen;
102
103
        $this->displayScreen($this->currentScreen);
104
105
        return $this;
106
    }
107
108
    public function getPreviousScreen(): Screen
109
    {
110
        return $this->previousScreen;
111
    }
112
113
    public function refreshScreen()
114
    {
115
        if (is_null($this->currentScreen)) {
116
            return;
117
        }
118
119
        $this->displayScreen($this->currentScreen);
120
121
        return $this;
122
    }
123
124
    public function isDisplayingScreen(string $screenClassName): bool
125
    {
126
        if (is_null($this->currentScreen)) {
127
            return false;
128
        }
129
130
        return $screenClassName === get_class($this->currentScreen);
131
    }
132
133
    public function removeAllListeners()
134
    {
135
        $this->io->removeAllListeners();
136
137
        return $this;
138
    }
139
140
    public function prompt(string $prompt)
141
    {
142
        $this->io->getReadline()->setPrompt($prompt);
143
144
        return $this;
145
    }
146
147
    public function clearPrompt()
148
    {
149
        $this->io->getReadline()->setPrompt('');
150
151
        return $this;
152
    }
153
}
154