Completed
Pull Request — master (#104)
by
unknown
07:24
created

ColourUtil::map256To8()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace PhpSchool\CliMenu\Util;
4
5
use Assert\Assertion;
6
use PhpSchool\Terminal\Terminal;
7
8
class ColourUtil
9
{
10
    /**
11
     * @var array
12
     */
13
    private static $defaultColoursNames = [
14
        'black',
15
        'red',
16
        'green',
17
        'yellow',
18
        'blue',
19
        'magenta',
20
        'cyan',
21
        'white',
22
    ];
23
24
    /**
25
     * @var array
26
     */
27
    private static $coloursMap = [
28
        0 => 'black',
29
        1 => 'red',
30
        2 => 'green',
31
        3 => 'yellow',
32
        4 => 'blue',
33
        5 => 'magenta',
34
        6 => 'cyan',
35
        7 => 'white',
36
        8 => 'black',
37
        9 => 'red',
38
        10 => 'green',
39
        11 => 'yellow',
40
        12 => 'blue',
41
        13 => 'magenta',
42
        14 => 'cyan',
43
        15 => 'white',
44
        16 => 'black',
45
        17 => 'blue',
46
        18 => 'blue',
47
        19 => 'blue',
48
        20 => 'blue',
49
        21 => 'blue',
50
        22 => 'green',
51
        23 => 'cyan',
52
        24 => 'cyan',
53
        25 => 'blue',
54
        26 => 'blue',
55
        27 => 'blue',
56
        28 => 'green',
57
        29 => 'green',
58
        30 => 'cyan',
59
        31 => 'cyan',
60
        32 => 'blue',
61
        33 => 'blue',
62
        34 => 'green',
63
        35 => 'green',
64
        36 => 'green',
65
        37 => 'cyan',
66
        38 => 'cyan',
67
        39 => 'cyan',
68
        40 => 'green',
69
        41 => 'green',
70
        42 => 'green',
71
        43 => 'cyan',
72
        44 => 'cyan',
73
        45 => 'cyan',
74
        46 => 'green',
75
        47 => 'green',
76
        48 => 'green',
77
        49 => 'green',
78
        50 => 'cyan',
79
        51 => 'cyan',
80
        52 => 'red',
81
        53 => 'blue',
82
        54 => 'blue',
83
        55 => 'blue',
84
        56 => 'blue',
85
        57 => 'blue',
86
        58 => 'yellow',
87
        59 => 'black',
88
        60 => 'blue',
89
        61 => 'blue',
90
        62 => 'blue',
91
        63 => 'blue',
92
        64 => 'green',
93
        65 => 'green',
94
        66 => 'cyan',
95
        67 => 'cyan',
96
        68 => 'blue',
97
        69 => 'blue',
98
        70 => 'green',
99
        71 => 'green',
100
        72 => 'green',
101
        73 => 'cyan',
102
        74 => 'cyan',
103
        75 => 'cyan',
104
        76 => 'green',
105
        77 => 'green',
106
        78 => 'green',
107
        79 => 'green',
108
        80 => 'cyan',
109
        81 => 'cyan',
110
        82 => 'green',
111
        83 => 'green',
112
        84 => 'green',
113
        85 => 'green',
114
        86 => 'cyan',
115
        87 => 'cyan',
116
        88 => 'red',
117
        89 => 'magenta',
118
        90 => 'magenta',
119
        91 => 'magenta',
120
        92 => 'blue',
121
        93 => 'blue',
122
        94 => 'yellow',
123
        95 => 'red',
124
        96 => 'magenta',
125
        97 => 'magenta',
126
        98 => 'blue',
127
        99 => 'blue',
128
        100 => 'yellow',
129
        101 => 'yellow',
130
        102 => 'white',
131
        103 => 'blue',
132
        104 => 'blue',
133
        105 => 'blue',
134
        106 => 'green',
135
        107 => 'green',
136
        108 => 'green',
137
        109 => 'cyan',
138
        110 => 'cyan',
139
        111 => 'cyan',
140
        112 => 'green',
141
        113 => 'green',
142
        114 => 'green',
143
        115 => 'green',
144
        116 => 'cyan',
145
        117 => 'cyan',
146
        118 => 'green',
147
        119 => 'green',
148
        120 => 'green',
149
        121 => 'green',
150
        122 => 'green',
151
        123 => 'cyan',
152
        124 => 'red',
153
        125 => 'magenta',
154
        126 => 'magenta',
155
        127 => 'magenta',
156
        128 => 'magenta',
157
        129 => 'magenta',
158
        130 => 'red',
159
        131 => 'red',
160
        132 => 'magenta',
161
        133 => 'magenta',
162
        134 => 'magenta',
163
        135 => 'magenta',
164
        136 => 'yellow',
165
        137 => 'red',
166
        138 => 'red',
167
        139 => 'magenta',
168
        140 => 'magenta',
169
        141 => 'magenta',
170
        142 => 'yellow',
171
        143 => 'yellow',
172
        144 => 'yellow',
173
        145 => 'white',
174
        146 => 'white',
175
        147 => 'white',
176
        148 => 'yellow',
177
        149 => 'green',
178
        150 => 'green',
179
        151 => 'green',
180
        152 => 'cyan',
181
        153 => 'white',
182
        154 => 'green',
183
        155 => 'green',
184
        156 => 'green',
185
        157 => 'green',
186
        158 => 'green',
187
        159 => 'cyan',
188
        160 => 'red',
189
        161 => 'magenta',
190
        162 => 'magenta',
191
        163 => 'magenta',
192
        164 => 'magenta',
193
        165 => 'magenta',
194
        166 => 'red',
195
        167 => 'red',
196
        168 => 'magenta',
197
        169 => 'magenta',
198
        170 => 'magenta',
199
        171 => 'magenta',
200
        172 => 'red',
201
        173 => 'red',
202
        174 => 'red',
203
        175 => 'magenta',
204
        176 => 'magenta',
205
        177 => 'magenta',
206
        178 => 'yellow',
207
        179 => 'yellow',
208
        180 => 'white',
209
        181 => 'white',
210
        182 => 'magenta',
211
        183 => 'magenta',
212
        184 => 'yellow',
213
        185 => 'yellow',
214
        186 => 'yellow',
215
        187 => 'yellow',
216
        188 => 'white',
217
        189 => 'white',
218
        190 => 'yellow',
219
        191 => 'yellow',
220
        192 => 'green',
221
        193 => 'green',
222
        194 => 'green',
223
        195 => 'cyan',
224
        196 => 'red',
225
        197 => 'red',
226
        198 => 'magenta',
227
        199 => 'magenta',
228
        200 => 'magenta',
229
        201 => 'magenta',
230
        202 => 'red',
231
        203 => 'red',
232
        204 => 'magenta',
233
        205 => 'magenta',
234
        206 => 'magenta',
235
        207 => 'magenta',
236
        208 => 'red',
237
        209 => 'red',
238
        210 => 'red',
239
        211 => 'magenta',
240
        212 => 'magenta',
241
        213 => 'magenta',
242
        214 => 'red',
243
        215 => 'white',
244
        216 => 'red',
245
        217 => 'red',
246
        218 => 'magenta',
247
        219 => 'magenta',
248
        220 => 'yellow',
249
        221 => 'yellow',
250
        222 => 'yellow',
251
        223 => 'white',
252
        224 => 'white',
253
        225 => 'magenta',
254
        226 => 'yellow',
255
        227 => 'yellow',
256
        228 => 'yellow',
257
        229 => 'yellow',
258
        230 => 'yellow',
259
        231 => 'white',
260
        232 => 'black',
261
        233 => 'black',
262
        234 => 'black',
263
        235 => 'black',
264
        236 => 'black',
265
        237 => 'black',
266
        238 => 'black',
267
        239 => 'black',
268
        240 => 'black',
269
        241 => 'black',
270
        242 => 'black',
271
        243 => 'black',
272
        244 => 'white',
273
        245 => 'white',
274
        246 => 'white',
275
        247 => 'white',
276
        248 => 'white',
277
        249 => 'white',
278
        250 => 'white',
279
        251 => 'white',
280
        252 => 'white',
281
        253 => 'white',
282
        254 => 'white',
283
        255 => 'white',
284
    ];
285
286
    public static function getDefaultColoursNames() : array
287
    {
288
        return static::$defaultColoursNames;
0 ignored issues
show
Bug introduced by
Since $defaultColoursNames is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $defaultColoursNames to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
289
    }
290
291
    /**
292
     * Simple function to transform a 8-bit (256 colours) colour code
293
     * to one of the default 8 colors available in the terminal
294
     */
295
    public static function map256To8(int $colourCode) : string
296
    {
297
        if (!isset(static::$coloursMap[$colourCode])) {
0 ignored issues
show
Bug introduced by
Since $coloursMap is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $coloursMap to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
298
            throw new \InvalidArgumentException("Invalid colour code");
299
        }
300
301
        return static::$coloursMap[$colourCode];
0 ignored issues
show
Bug introduced by
Since $coloursMap is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $coloursMap to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
302
    }
303
304
    /**
305
     * Check if $colour exists
306
     * If it's a 256-colours code and $terminal doesn't support it, returns a fallback value
307
     */
308
    public static function validateColour(Terminal $terminal, $colour, string $fallback = null)
309
    {
310
        if (is_int($colour)) {
311
            if ($colour < 0 || $colour > 255) {
312
                throw new \InvalidArgumentException("Invalid colour code");
313
            }
314
            if ($terminal->getColourSupport() < 256) {
315
                if ($fallback !== null) {
316
                    Assertion::inArray($fallback, static::getDefaultColoursNames());
317
                    return $fallback;
318
                }
319
                return static::map256To8($colour);
320
            }
321
        }
322
        Assertion::inArray($colour, static::getDefaultColoursNames());
323
        return $colour;
324
    }
325
}
326