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 ( 7f64b4...3b8bf4 )
by Freek
01:23
created

Completer::appendQuoteIfNeeded()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace Spatie\PhpUnitWatcher\Screens\Completers;
4
5
use Clue\React\Stdio\Readline;
6
7
abstract class Completer
8
{
9
    /**
10
     * @var \Clue\React\Stdio\Readline
11
     */
12
    protected $readline;
13
14
    /**
15
     * @var string
16
     */
17
    protected $word;
18
19
    /**
20
     * @var int
21
     */
22
    protected $wordStartOffset;
23
24
    /**
25
     * @var int
26
     */
27
    protected $wordEndOffset;
28
29
    /**
30
     * @var \Closure|callable
31
     */
32
    protected $filterSuggestionsCallback;
33
34
    /**
35
     * @param \Clue\React\Stdio\Readline $readline
36
     */
37
    public function __construct(Readline $readline)
38
    {
39
        $this->readline = $readline;
40
    }
41
42
    /**
43
     * Start completion
44
     *
45
     * @param string $word
46
     * @param int $startOffset
47
     * @param int $endOffset
48
     * @return array|null
49
     */
50
    public function __invoke($word, $startOffset, $endOffset)
51
    {
52
        $this->word = $word;
53
        $this->wordStartOffset = $startOffset;
54
        $this->wordEndOffset = $endOffset;
55
56
        return $this->handleSearchResults(
57
            $this->search()
58
        );
59
    }
60
61
    /**
62
     * Search for things related to the completed word and return an array of suggestions
63
     *
64
     * Use an array with one element to autocomplete the word
65
     *
66
     * @return array
67
     */
68
    abstract protected function search();
69
70
    /**
71
     * Display suggestions or complete the word depending on number of results
72
     *
73
     * When returning an array reactphp-stdio will display suggestions with the default behavior.
74
     *
75
     * @param array $searchResults
76
     * @return array|null
77
     */
78
    protected function handleSearchResults($searchResults)
79
    {
80
        if (empty($searchResults)) {
81
            return null;
82
        }
83
84
        if (count($searchResults) > 1) {
85
            return $this->filterSuggestions($searchResults);
86
        }
87
88
        $this->completeWord($searchResults[0]);
89
    }
90
91
    /**
92
     * Filter suggestions to display
93
     *
94
     * @param array $suggestions
95
     * @return array|null
96
     */
97
    protected function filterSuggestions($suggestions)
98
    {
99
        if (is_callable($this->filterSuggestionsCallback)) {
100
            $suggestions = ($this->filterSuggestionsCallback)($suggestions);
101
        }
102
        return $suggestions;
103
    }
104
105
    /**
106
     * Register callback called when more than one suggestion is available
107
     *
108
     * You can use this callback to filter suggestions or to abort the
109
     * default display behavior by returning null
110
     *
111
     * @param callable $callback
112
     */
113
    public function onSuggestions($callback)
114
    {
115
        $this->filterSuggestionsCallback = $callback;
116
    }
117
118
    /**
119
     * Refresh the input with the completed word and move cursor to end of the word
120
     *
121
     * @param string $newWord
122
     */
123
    protected function completeWord($newWord)
124
    {
125
        $endQuotedWord = $this->appendQuoteIfNeeded($newWord);
126
127
        $this->readline->setInput(
128
            $this->getInputBeforeWord() . $endQuotedWord . $this->getInputAfterWord()
129
        );
130
131
        $this->readline->moveCursorTo($this->wordStartOffset + mb_strlen($endQuotedWord));
132
    }
133
134
    /**
135
     * Return input string before the word
136
     *
137
     * @return string
138
     */
139
    protected function getInputBeforeWord()
140
    {
141
        return mb_substr($this->getInput(), 0, $this->wordStartOffset);
142
    }
143
144
    /**
145
     * Return input string after the word
146
     *
147
     * @return string
148
     */
149
    protected function getInputAfterWord()
150
    {
151
        return mb_substr($this->getInput(), $this->wordEndOffset);
152
    }
153
154
    /**
155
     * Return the character before the word
156
     *
157
     * @return null|string
158
     */
159
    protected function getCharBeforeWord()
160
    {
161
        return $this->wordStartOffset > 0
162
            ? mb_substr($this->getInput(), $this->wordStartOffset - 1, 1)
163
            : null;
164
    }
165
166
    /**
167
     * Append a quote if word is prefixed with a quote
168
     *
169
     * @param string $word
170
     * @return string
171
     */
172
    protected function appendQuoteIfNeeded($word)
173
    {
174
        $char = $this->getCharBeforeWord();
175
176
        return ($char === '"' || $char === '\'')
177
            ? $word . $char
178
            : $word;
179
    }
180
181
    /**
182
     * Return the input string
183
     *
184
     * @return string
185
     */
186
    protected function getInput()
187
    {
188
        return $this->readline->getInput();
189
    }
190
}
191