Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
17 | class regulate { |
||
18 | |||
19 | /** |
||
20 | * Forces a float to be within the range of 0 and 100. |
||
21 | * |
||
22 | * @param float &$value The value to modify |
||
23 | * @return void |
||
24 | */ |
||
25 | 35 | public static function percent(float &$value) { |
|
26 | 35 | $value = static::max(abs($value), 100); |
|
27 | 35 | } |
|
28 | |||
29 | /** |
||
30 | * Forces a float to be within the range of 0 and 255. |
||
31 | * |
||
32 | * @param float &$value The value to modify |
||
33 | * @return void |
||
34 | */ |
||
35 | 12 | public static function rgb(int &$value) { |
|
36 | 12 | $value = static::max(abs($value), 255); |
|
37 | 12 | } |
|
38 | |||
39 | /** |
||
40 | * Forces and RGB array to have specific offsets, and max values. |
||
41 | * |
||
42 | * @param array &$rgb_array The RGB array to modify |
||
43 | * @return void |
||
44 | */ |
||
45 | 11 | View Code Duplication | public static function rgb_array(array &$rgb_array) { |
|
|||
46 | 11 | static::standardize_array($rgb_array, ['r', 'g', 'b']); |
|
47 | 11 | static::rgb($rgb_array['r']); |
|
48 | 11 | static::rgb($rgb_array['g']); |
|
49 | 11 | static::rgb($rgb_array['b']); |
|
50 | 11 | } |
|
51 | |||
52 | /** |
||
53 | * Forces a float to be within the range of 0 and 100 or if the offset is |
||
54 | * 'h' 0 and 359. |
||
55 | * |
||
56 | * @param float &$value The value to modify |
||
57 | * @param string $offset The offset that is being modified |
||
58 | * @return void |
||
59 | */ |
||
60 | 29 | public static function hsl(float &$value, string $offset) { |
|
61 | 29 | if (strtolower($offset) == 'h') { |
|
62 | 29 | $value = static::max(abs($value), 359); |
|
63 | } else { |
||
64 | 29 | static::percent($value); |
|
65 | } |
||
66 | 29 | } |
|
67 | |||
68 | /** |
||
69 | * Forces and HSL array to have specific offsets, and max values. |
||
70 | * |
||
71 | * @param array &$hsl_array The HSL array to modify |
||
72 | * @return void |
||
73 | */ |
||
74 | 25 | View Code Duplication | public static function hsl_array(array &$hsl_array) { |
75 | 25 | static::standardize_array($hsl_array, ['h', 's', 'l']); |
|
76 | 25 | static::hsl($hsl_array['h'], 'h'); |
|
77 | 25 | static::hsl($hsl_array['s'], 's'); |
|
78 | 25 | static::hsl($hsl_array['l'], 'l'); |
|
79 | 25 | } |
|
80 | |||
81 | /** |
||
82 | * Forces and HSL array to have specific offsets, and max values. |
||
83 | * |
||
84 | * @param array &$hsl_array The HSL array to modify |
||
85 | * @return void |
||
86 | */ |
||
87 | 5 | View Code Duplication | public static function hsb_array(array &$hsl_array) { |
93 | |||
94 | /** |
||
95 | * Forces a float to be within the range of 0 and 100. |
||
96 | * |
||
97 | * @param float &$value The value to modify |
||
98 | * @return void |
||
99 | */ |
||
100 | 6 | public static function cmyk(float &$value) { |
|
101 | 6 | static::percent($value); |
|
102 | 6 | } |
|
103 | |||
104 | |||
105 | /** |
||
106 | * Forces a float to be within the range of 0 and 100. |
||
107 | * |
||
108 | * @param float &$value The value to modify |
||
109 | * @return void |
||
110 | */ |
||
111 | 1 | public static function alpha(float &$value) { |
|
114 | |||
115 | /** |
||
116 | * Forces and CMYK array to have specific offsets, and max values. |
||
117 | * |
||
118 | * @param array &$cmyk_array The CMYK array to modify |
||
119 | * @return void |
||
120 | */ |
||
121 | 5 | public static function cmyk_array(array &$cmyk_array) { |
|
128 | |||
129 | /** |
||
130 | * Forces hexadecimal strings to be valid, without a "#", and not to be |
||
131 | * shorthand. |
||
132 | * |
||
133 | * @param string &$color The color string to modify |
||
134 | * @return void |
||
135 | */ |
||
136 | 7 | public static function hex(string &$color) { |
|
141 | |||
142 | /** |
||
143 | * Forces hexadecimal integers to be within the range of 0x0 and 0xFFFFFF. |
||
144 | * |
||
145 | * @param int &$value The value to modify |
||
146 | * @return void |
||
147 | */ |
||
148 | 3 | public static function hex_int(int &$value) { |
|
151 | |||
152 | /** |
||
153 | * Strips the hash mark (#) off the beginning of a sting if it has one. |
||
154 | * |
||
155 | * @param string &$hex The hex string |
||
156 | * @return void |
||
157 | */ |
||
158 | 8 | public static function _strip_hash(string &$hex) { |
|
163 | |||
164 | /** |
||
165 | * Expand a hex strings in short notation (e.g. 000 => 000000) |
||
166 | * |
||
167 | * @param string &$hex_str The hex string to modify |
||
168 | * @return void |
||
169 | */ |
||
170 | 8 | public static function _expand_shorthand(string &$hex_str) { |
|
178 | |||
179 | /** |
||
180 | * Verify that the hex string is actually a hex string. If not force it to |
||
181 | * '000000' |
||
182 | * |
||
183 | * @param string &$hex_str The hex string to modify |
||
184 | * @return void |
||
185 | */ |
||
186 | 8 | public static function _validate_hex_str(string &$hex_str, bool $check_only = FALSE) :bool { |
|
200 | |||
201 | /** |
||
202 | * Force an array to have specific lower-case keys. If the array is an indexed |
||
203 | * array and the keys are provided, convert it to an associative array. |
||
204 | * |
||
205 | 41 | * @param array &$array The array to be modified |
|
206 | 41 | * @return void |
|
207 | 2 | */ |
|
208 | public static function standardize_array(array &$array, array $keys = []) { |
||
215 | |||
216 | /** |
||
217 | * Absolute and divide any number so it fits between 0 and $max |
||
218 | * |
||
219 | * @param float $number The original number |
||
220 | 46 | * @param float $max The maximum value $number should be |
|
221 | 46 | * @return float The resulting number as a float |
|
222 | 46 | */ |
|
223 | 16 | public static function max(float $number, float $max) :float { |
|
230 | |||
231 | /** |
||
232 | * Simple dividing method to handle division by 0 |
||
233 | * |
||
234 | * @param float $number The number to divide |
||
235 | 15 | * @param float $divisor The number to divide by |
|
236 | 15 | * @return float The result |
|
237 | 5 | */ |
|
238 | public static function div(float $number, float $divisor) :float { |
||
244 | } |
||
245 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.