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