Completed
Pull Request — master (#104)
by
unknown
02:16
created

ColourUtil::getDefaultColoursNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace PhpSchool\CliMenu\Util;
4
5
use PhpSchool\Terminal\Terminal;
6
7
class ColourUtil
8
{
9
    /**
10
     * @var array
11
     */
12
    private $defaultColoursNames = [
13
        'black',
14
        'red',
15
        'green',
16
        'yellow',
17
        'blue',
18
        'magenta',
19
        'cyan',
20
        'white',
21
    ];
22
23
    /**
24
     * @var array
25
     */
26
    private $coloursMap = [
27
        0 => 'black',
28
        1 => 'red',
29
        2 => 'green',
30
        3 => 'yellow',
31
        4 => 'blue',
32
        5 => 'magenta',
33
        6 => 'cyan',
34
        7 => 'white',
35
        8 => 'black',
36
        9 => 'red',
37
        10 => 'green',
38
        11 => 'yellow',
39
        12 => 'blue',
40
        13 => 'magenta',
41
        14 => 'cyan',
42
        15 => 'white',
43
        16 => 'black',
44
        17 => 'blue',
45
        18 => 'blue',
46
        19 => 'blue',
47
        20 => 'blue',
48
        21 => 'blue',
49
        22 => 'green',
50
        23 => 'cyan',
51
        24 => 'cyan',
52
        25 => 'blue',
53
        26 => 'blue',
54
        27 => 'blue',
55
        28 => 'green',
56
        29 => 'green',
57
        30 => 'cyan',
58
        31 => 'cyan',
59
        32 => 'blue',
60
        33 => 'blue',
61
        34 => 'green',
62
        35 => 'green',
63
        36 => 'green',
64
        37 => 'cyan',
65
        38 => 'cyan',
66
        39 => 'cyan',
67
        40 => 'green',
68
        41 => 'green',
69
        42 => 'green',
70
        43 => 'cyan',
71
        44 => 'cyan',
72
        45 => 'cyan',
73
        46 => 'green',
74
        47 => 'green',
75
        48 => 'green',
76
        49 => 'green',
77
        50 => 'cyan',
78
        51 => 'cyan',
79
        52 => 'red',
80
        53 => 'blue',
81
        54 => 'blue',
82
        55 => 'blue',
83
        56 => 'blue',
84
        57 => 'blue',
85
        58 => 'yellow',
86
        59 => 'black',
87
        60 => 'blue',
88
        61 => 'blue',
89
        62 => 'blue',
90
        63 => 'blue',
91
        64 => 'green',
92
        65 => 'green',
93
        66 => 'cyan',
94
        67 => 'cyan',
95
        68 => 'blue',
96
        69 => 'blue',
97
        70 => 'green',
98
        71 => 'green',
99
        72 => 'green',
100
        73 => 'cyan',
101
        74 => 'cyan',
102
        75 => 'cyan',
103
        76 => 'green',
104
        77 => 'green',
105
        78 => 'green',
106
        79 => 'green',
107
        80 => 'cyan',
108
        81 => 'cyan',
109
        82 => 'green',
110
        83 => 'green',
111
        84 => 'green',
112
        85 => 'green',
113
        86 => 'cyan',
114
        87 => 'cyan',
115
        88 => 'red',
116
        89 => 'magenta',
117
        90 => 'magenta',
118
        91 => 'magenta',
119
        92 => 'blue',
120
        93 => 'blue',
121
        94 => 'yellow',
122
        95 => 'red',
123
        96 => 'magenta',
124
        97 => 'magenta',
125
        98 => 'blue',
126
        99 => 'blue',
127
        100 => 'yellow',
128
        101 => 'yellow',
129
        102 => 'white',
130
        103 => 'blue',
131
        104 => 'blue',
132
        105 => 'blue',
133
        106 => 'green',
134
        107 => 'green',
135
        108 => 'green',
136
        109 => 'cyan',
137
        110 => 'cyan',
138
        111 => 'cyan',
139
        112 => 'green',
140
        113 => 'green',
141
        114 => 'green',
142
        115 => 'green',
143
        116 => 'cyan',
144
        117 => 'cyan',
145
        118 => 'green',
146
        119 => 'green',
147
        120 => 'green',
148
        121 => 'green',
149
        122 => 'green',
150
        123 => 'cyan',
151
        124 => 'red',
152
        125 => 'magenta',
153
        126 => 'magenta',
154
        127 => 'magenta',
155
        128 => 'magenta',
156
        129 => 'magenta',
157
        130 => 'red',
158
        131 => 'red',
159
        132 => 'magenta',
160
        133 => 'magenta',
161
        134 => 'magenta',
162
        135 => 'magenta',
163
        136 => 'yellow',
164
        137 => 'red',
165
        138 => 'red',
166
        139 => 'magenta',
167
        140 => 'magenta',
168
        141 => 'magenta',
169
        142 => 'yellow',
170
        143 => 'yellow',
171
        144 => 'yellow',
172
        145 => 'white',
173
        146 => 'white',
174
        147 => 'white',
175
        148 => 'yellow',
176
        149 => 'green',
177
        150 => 'green',
178
        151 => 'green',
179
        152 => 'cyan',
180
        153 => 'white',
181
        154 => 'green',
182
        155 => 'green',
183
        156 => 'green',
184
        157 => 'green',
185
        158 => 'green',
186
        159 => 'cyan',
187
        160 => 'red',
188
        161 => 'magenta',
189
        162 => 'magenta',
190
        163 => 'magenta',
191
        164 => 'magenta',
192
        165 => 'magenta',
193
        166 => 'red',
194
        167 => 'red',
195
        168 => 'magenta',
196
        169 => 'magenta',
197
        170 => 'magenta',
198
        171 => 'magenta',
199
        172 => 'red',
200
        173 => 'red',
201
        174 => 'red',
202
        175 => 'magenta',
203
        176 => 'magenta',
204
        177 => 'magenta',
205
        178 => 'yellow',
206
        179 => 'yellow',
207
        180 => 'white',
208
        181 => 'white',
209
        182 => 'magenta',
210
        183 => 'magenta',
211
        184 => 'yellow',
212
        185 => 'yellow',
213
        186 => 'yellow',
214
        187 => 'yellow',
215
        188 => 'white',
216
        189 => 'white',
217
        190 => 'yellow',
218
        191 => 'yellow',
219
        192 => 'green',
220
        193 => 'green',
221
        194 => 'green',
222
        195 => 'cyan',
223
        196 => 'red',
224
        197 => 'red',
225
        198 => 'magenta',
226
        199 => 'magenta',
227
        200 => 'magenta',
228
        201 => 'magenta',
229
        202 => 'red',
230
        203 => 'red',
231
        204 => 'magenta',
232
        205 => 'magenta',
233
        206 => 'magenta',
234
        207 => 'magenta',
235
        208 => 'red',
236
        209 => 'red',
237
        210 => 'red',
238
        211 => 'magenta',
239
        212 => 'magenta',
240
        213 => 'magenta',
241
        214 => 'red',
242
        215 => 'white',
243
        216 => 'red',
244
        217 => 'red',
245
        218 => 'magenta',
246
        219 => 'magenta',
247
        220 => 'yellow',
248
        221 => 'yellow',
249
        222 => 'yellow',
250
        223 => 'white',
251
        224 => 'white',
252
        225 => 'magenta',
253
        226 => 'yellow',
254
        227 => 'yellow',
255
        228 => 'yellow',
256
        229 => 'yellow',
257
        230 => 'yellow',
258
        231 => 'white',
259
        232 => 'black',
260
        233 => 'black',
261
        234 => 'black',
262
        235 => 'black',
263
        236 => 'black',
264
        237 => 'black',
265
        238 => 'black',
266
        239 => 'black',
267
        240 => 'black',
268
        241 => 'black',
269
        242 => 'black',
270
        243 => 'black',
271
        244 => 'white',
272
        245 => 'white',
273
        246 => 'white',
274
        247 => 'white',
275
        248 => 'white',
276
        249 => 'white',
277
        250 => 'white',
278
        251 => 'white',
279
        252 => 'white',
280
        253 => 'white',
281
        254 => 'white',
282
        255 => 'white',
283
    ];
284
285
    public static function getDefaultColoursNames() : array
286
    {
287
        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...
288
    }
289
290
    /**
291
     * Simple function to transform a 8-bit (256 colours) colour code
292
     * to one of the default 8 colors available in the terminal
293
     */
294
    public static function map256To8(int $colourCode) : string
295
    {
296
        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...
297
    }
298
299
    /**
300
     * Check if $colour exists
301
     * If it's a 256-colours code and $terminal doesn't support it, returns a fallback value
302
     */
303
    public static function validateColour(Terminal $terminal, $colour, string $fallback = null)
304
    {
305
        if (is_int($colour)) {
306
            if ($colour < 0 || $colour > 255) {
307
                throw new \InvalidArgumentException("Invalid colour code");
308
            }
309
            if ($terminal->getColourSupport() < 256) {
310
                if ($fallback !== null) {
311
                    Assertion::inArray($fallback, static::getDefaultColoursNames());
312
                    return $fallback;
313
                }
314
                return static::map256To8($colour);
315
            }
316
        }
317
        Assertion::inArray($colour, static::getDefaultColoursNames());
318
        return $colour;
319
    }
320
}
321