Complex classes like generate 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 generate, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class generate { |
||
8 | |||
9 | /** |
||
10 | * Convert a hex string (no #) to a RGB array |
||
11 | * |
||
12 | * @param string $hex The hex string to convert (no #) |
||
13 | * @return array The RGB array |
||
14 | */ |
||
15 | 1 | public static function hex_to_rgb(string $hex) :array { |
|
22 | |||
23 | 1 | public static function rgb_to_hex(int $r, int $g, int $b) :string { |
|
30 | |||
31 | 1 | public static function rgb_to_cmyk(float $r, float $g, float $b) :array { |
|
32 | 1 | $c = (255 - $r) / 255 * 100; |
|
33 | 1 | $m = (255 - $g) / 255 * 100; |
|
34 | 1 | $y = (255 - $b) / 255 * 100; |
|
35 | 1 | $k = min(array($c,$m,$y)); |
|
36 | 1 | $c -= $k; |
|
37 | 1 | $m -= $k; |
|
38 | 1 | $y -= $k; |
|
39 | return [ |
||
40 | 1 | 'c' => round($c), |
|
41 | 1 | 'm' => round($m), |
|
42 | 1 | 'y' => round($y), |
|
43 | 1 | 'k' => round($k) |
|
44 | ]; |
||
45 | } |
||
46 | |||
47 | 1 | public static function cmyk_to_rgb(float $c, float $m, float $y, float $k) :array { |
|
48 | 1 | $c /= 100; |
|
49 | 1 | $m /= 100; |
|
50 | 1 | $y /= 100; |
|
51 | 1 | $k /= 100; |
|
52 | 1 | $r = 1 - min(1, $c * (1 - $k) + $k); |
|
53 | 1 | $g = 1 - min(1, $m * (1 - $k) + $k); |
|
54 | 1 | $b = 1 - min(1, $y * (1 - $k) + $k); |
|
55 | return [ |
||
56 | 1 | 'r' => round($r * 255), |
|
57 | 1 | 'g' => round($g * 255), |
|
58 | 1 | 'b' => round($b * 255) |
|
59 | ]; |
||
60 | } |
||
61 | |||
62 | 1 | public static function rgb_contrast(int $r = 0, int $g = 0, int $b = 0) :array { |
|
63 | return [ |
||
64 | 1 | 'r' => ($r < 128) ? 255 : 0, |
|
65 | 1 | 'g' => ($g < 128) ? 255 : 0, |
|
66 | 1 | 'b' => ($b < 128) ? 255 : 0 |
|
67 | ]; |
||
68 | } |
||
69 | |||
70 | 1 | public static function rgb_invert(int $r = 0, int $g = 0, int $b = 0) :array { |
|
71 | return [ |
||
72 | 1 | 'r' => 255 - $r, |
|
73 | 1 | 'g' => 255 - $g, |
|
74 | 1 | 'b' => 255 - $b |
|
75 | ]; |
||
76 | } |
||
77 | |||
78 | 2 | public static function yiq_score(int $r = 0, int $g = 0, int $b = 0) :float { |
|
79 | 2 | return (($r * 299) + ($g * 587) + ($b * 114)) / 1000; |
|
80 | } |
||
81 | |||
82 | 1 | public static function rand(int $min_r = 0, int $max_r = 255, int $min_g = 0, int $max_g = 255, int $min_b = 0, int $max_b = 255) :array { |
|
83 | return [ |
||
84 | 1 | 'r' => rand(abs((int) $min_r) % 256, abs((int) $max_r) % 256), |
|
85 | 1 | 'g' => rand(abs((int) $min_g) % 256, abs((int) $max_g) % 256), |
|
86 | 1 | 'b' => rand(abs((int) $min_b) % 256, abs((int) $max_b) % 256) |
|
87 | ]; |
||
88 | } |
||
89 | |||
90 | |||
91 | 2 | public static function rgb_to_hsl(int $r = 0, int $g = 0, int $b = 0, $accuracy = 2) :array { |
|
116 | |||
117 | 2 | protected static function _rgbhsl_delta_rgb(float $rgb, float $max, float $delta) { |
|
120 | |||
121 | 2 | protected static function _rgbhsl_hue(float &$h, float $r, float $g, float $b, float $max, float $delta) { |
|
122 | 2 | $delta_r = static::_rgbhsl_delta_rgb($r, $max, $delta); |
|
123 | 2 | $delta_g = static::_rgbhsl_delta_rgb($g, $max, $delta); |
|
124 | 2 | $delta_b = static::_rgbhsl_delta_rgb($b, $max, $delta); |
|
125 | |||
126 | 2 | $h = (2 / 3) + $delta_g - $delta_r; |
|
136 | |||
137 | 2 | public static function hsl_to_rgb(float $h = 0, float $s = 0, float $l = 0) :array { |
|
159 | |||
160 | 2 | private static function _hslrgb_low(float &$r, float &$g, float &$b, float $c, float $x, float $h) { |
|
175 | |||
176 | 2 | private static function _hslrgb_high(float &$r, float &$g, float &$b, float $c, float $x, float $h) { |
|
187 | |||
188 | 1 | public static function rgb_to_hsb(float $r, float $g, float $b, int $accuracy = 3) { |
|
216 | |||
217 | 1 | public static function hsb_to_rgb(float $h, float $s, float $v, int $accuracy = 3) { |
|
252 | |||
253 | |||
254 | |||
255 | |||
256 | |||
257 | } |
||
258 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.