Complex classes like Color often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Color, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | final class Color |
||
18 | { |
||
19 | /** |
||
20 | * HSL and HSV cache. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $hsv, $hsl; |
||
25 | |||
26 | /** |
||
27 | * Luma cache. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $luma; |
||
32 | |||
33 | /** |
||
34 | * Luminance cache. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $luminance; |
||
39 | |||
40 | /** |
||
41 | * The rgb channels. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | public $rgb = []; |
||
46 | |||
47 | /** |
||
48 | * The alpha channel. |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | public $alpha = 1; |
||
53 | |||
54 | /** |
||
55 | * Original format. |
||
56 | * |
||
57 | * @var bool |
||
58 | */ |
||
59 | protected $short = false; |
||
60 | |||
61 | /** |
||
62 | * Created from keyword? |
||
63 | * |
||
64 | * @var bool |
||
65 | */ |
||
66 | public $keyword = false; |
||
67 | |||
68 | /** |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $originalForm; |
||
72 | |||
73 | /** |
||
74 | * Transparent keyword? |
||
75 | * |
||
76 | * @var bool |
||
77 | */ |
||
78 | public $isTransparentKeyword = false; |
||
79 | |||
80 | /** |
||
81 | * Constructor. |
||
82 | * |
||
83 | * @param array|string $rgb The RGB components as an array or string definition |
||
84 | * @param int $alpha The alpha channel |
||
85 | * @param string $originalForm |
||
86 | */ |
||
87 | public function __construct($rgb = [255, 255, 255], $alpha = 1, $originalForm = null) |
||
124 | |||
125 | /** |
||
126 | * Returns the fixed RGB components (fitted into 0 - 255 range). |
||
127 | * |
||
128 | * @return array Array of red, green and blue components |
||
129 | */ |
||
130 | protected function getFixedRGB() |
||
145 | |||
146 | protected function clamp($value, $max) |
||
150 | |||
151 | /** |
||
152 | * Creates new color from the keyword. |
||
153 | * |
||
154 | * @param string $keyword |
||
155 | * |
||
156 | * @return Color |
||
157 | */ |
||
158 | public static function fromKeyword($keyword) |
||
172 | |||
173 | /** |
||
174 | * Returns the red channel. |
||
175 | * |
||
176 | * @return int |
||
177 | */ |
||
178 | public function getRed() |
||
182 | |||
183 | /** |
||
184 | * Returns the green channel. |
||
185 | * |
||
186 | * @return int |
||
187 | */ |
||
188 | public function getGreen() |
||
192 | |||
193 | /** |
||
194 | * Returns the blue channel. |
||
195 | * |
||
196 | * @return int |
||
197 | */ |
||
198 | public function getBlue() |
||
202 | |||
203 | /** |
||
204 | * Returns the alpha channel. |
||
205 | * |
||
206 | * @return int |
||
207 | */ |
||
208 | public function getAlpha() |
||
212 | |||
213 | /** |
||
214 | * Returns the color saturation. |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | public function getSaturation() |
||
224 | |||
225 | /** |
||
226 | * Returns the color hue. |
||
227 | * |
||
228 | * @param bool $round |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | public function getHue() |
||
238 | |||
239 | /** |
||
240 | * Returns the color lightness. |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | public function getLightness() |
||
250 | |||
251 | /** |
||
252 | * Returns the luma. |
||
253 | * |
||
254 | * @return int |
||
255 | */ |
||
256 | public function getLuma() |
||
274 | |||
275 | /** |
||
276 | * @return string |
||
277 | */ |
||
278 | public function getLuminance() |
||
290 | |||
291 | /** |
||
292 | * Converts to HSL. |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | public function toHSL() |
||
335 | |||
336 | /** |
||
337 | * Converts to HSV. |
||
338 | * |
||
339 | * @return array |
||
340 | */ |
||
341 | public function toHSV() |
||
383 | |||
384 | /** |
||
385 | * Returns the string representation in ARGB model. |
||
386 | * |
||
387 | * @return string |
||
388 | */ |
||
389 | public function toARGB() |
||
404 | |||
405 | private function toHex($rgb) |
||
418 | |||
419 | public function toRGB() |
||
423 | |||
424 | /** |
||
425 | * Returns the color as HEX string (when transparency present, in RGBA model). |
||
426 | * |
||
427 | * @param bool $compress Compress the color? |
||
428 | * @param bool $canShorten Can the color be shortened if possible? |
||
429 | * |
||
430 | * @return string |
||
431 | */ |
||
432 | public function toString($compress = false, $canShorten = false) |
||
482 | |||
483 | /** |
||
484 | * Converts the color to string. |
||
485 | * |
||
486 | * @return string |
||
487 | */ |
||
488 | public function __toString() |
||
492 | |||
493 | /** |
||
494 | * Does the color exits? |
||
495 | * |
||
496 | * @param string $color The color name |
||
497 | * |
||
498 | * @return string |
||
499 | */ |
||
500 | public static function isNamedColor($color) |
||
504 | |||
505 | /** |
||
506 | * Returns the color hex representation or false. |
||
507 | * |
||
508 | * @param string $color Color name |
||
509 | * |
||
510 | * @return string|false |
||
511 | */ |
||
512 | public static function color($color) |
||
516 | } |
||
517 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.