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 |
||
15 | class scheme { |
||
16 | |||
17 | /** |
||
18 | * A static reference to this class. (needed for child class late static binding) |
||
19 | * @var string |
||
20 | */ |
||
21 | protected static $this_class = __CLASS__; |
||
22 | |||
23 | /** |
||
24 | * These colors are all close to each other on a color wheel. |
||
25 | * |
||
26 | * @param float $h The base color hue degree (0 - 359) |
||
27 | * @param float $s The base color saturation percentage (0 - 100) |
||
28 | * @param float $l The base color lighting percentage (0 - 100) |
||
29 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
30 | * @return array An array of 5 analogous colors where the first offset is the original input. |
||
31 | */ |
||
32 | 2 | public static function analogous_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array { |
|
49 | |||
50 | /** |
||
51 | * 2 of these colors are a different shade of the base color. The other 2 are |
||
52 | * a weighted opposite of the base color. |
||
53 | * |
||
54 | * @param float $h The base color hue degree (0 - 359) |
||
55 | * @param float $s The base color saturation percentage (0 - 100) |
||
56 | * @param float $l The base color lighting percentage (0 - 100) |
||
57 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
58 | * @return array An array of 5 complementary colors where the first offset is the original input. |
||
59 | */ |
||
60 | 2 | View Code Duplication | public static function complementary_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array { |
72 | |||
73 | /** |
||
74 | * These colors use mathematical offsets that usually complement each other |
||
75 | * well, and can highlight the base color. |
||
76 | * |
||
77 | * @param float $h The base color hue degree (0 - 359) |
||
78 | * @param float $s The base color saturation percentage (0 - 100) |
||
79 | * @param float $l The base color lighting percentage (0 - 100) |
||
80 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
81 | * @return array An array of 5 compounding colors where the first offset is the original input. |
||
82 | */ |
||
83 | 2 | public static function compound_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array { |
|
100 | |||
101 | /** |
||
102 | * 5 complementary shades of one color. |
||
103 | * |
||
104 | * @param float $h The base color hue degree (0 - 359) |
||
105 | * @param float $s The base color saturation percentage (0 - 100) |
||
106 | * @param float $l The base color lighting percentage (0 - 100) |
||
107 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
108 | * @return array An array of 5 complementary shades of colors where the first offset is the original input. |
||
109 | */ |
||
110 | 2 | public static function monochromatic_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array { |
|
126 | |||
127 | /** |
||
128 | * 5 different shades of one color. |
||
129 | * |
||
130 | * @param float $h The base color hue degree (0 - 359) |
||
131 | * @param float $s The base color saturation percentage (0 - 100) |
||
132 | * @param float $l The base color lighting percentage (0 - 100) |
||
133 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
134 | * @return array An array of 5 shades of a color where the first offset is the original input. |
||
135 | */ |
||
136 | 12 | public static function shades_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array { |
|
154 | |||
155 | /** |
||
156 | * 3 of these colors are all equally distanced from each other on a color |
||
157 | * wheel, plus 1 alternated shade for the base color and the 1 color that is |
||
158 | * opposite of the base color. |
||
159 | * |
||
160 | * @param float $h The base color hue degree (0 - 359) |
||
161 | * @param float $s The base color saturation percentage (0 - 100) |
||
162 | * @param float $l The base color lighting percentage (0 - 100) |
||
163 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
164 | * @return array An array of 5 triangular colors where the first offset is the original input. |
||
165 | */ |
||
166 | 2 | View Code Duplication | public static function tetrad_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array { |
178 | |||
179 | /** |
||
180 | * 3 of these colors are all similarly distanced from each other on a color |
||
181 | * wheel, the base color has an alternate shade, and there is a weighted |
||
182 | * opposite color. These colors are all slightly closer to the base color |
||
183 | * than in a normal tetrad. |
||
184 | * |
||
185 | * @param float $h The base color hue degree (0 - 359) |
||
186 | * @param float $s The base color saturation percentage (0 - 100) |
||
187 | * @param float $l The base color lighting percentage (0 - 100) |
||
188 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
189 | * @return array An array of 5 triangular colors where the first offset is the original input. |
||
190 | */ |
||
191 | 2 | View Code Duplication | public static function weighted_tetrad_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array { |
203 | |||
204 | /** |
||
205 | * These colors are all equally distanced from each other on a color wheel, |
||
206 | * 2 of which have an alternate shade. |
||
207 | * |
||
208 | * @param float $h The base color hue degree (0 - 359) |
||
209 | * @param float $s The base color saturation percentage (0 - 100) |
||
210 | * @param float $l The base color lighting percentage (0 - 100) |
||
211 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
212 | * @return array An array of 5 triangular colors where the first offset is the original input. |
||
213 | */ |
||
214 | 2 | View Code Duplication | public static function triad_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array { |
226 | |||
227 | /** |
||
228 | * These colors are all similarly distanced from each other on a color wheel, |
||
229 | * 2 of which have an alternate shade. These colors are all slightly closer to |
||
230 | * the base color than in a normal triad. |
||
231 | * |
||
232 | * @param float $h The base color hue degree (0 - 359) |
||
233 | * @param float $s The base color saturation percentage (0 - 100) |
||
234 | * @param float $l The base color lighting percentage (0 - 100) |
||
235 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
236 | * @return array An array of 5 weighted triangular colors where the first offset is the original input. |
||
237 | */ |
||
238 | 2 | View Code Duplication | public static function weighted_triad_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array { |
250 | |||
251 | /** |
||
252 | * 4 of these colors form a rectangle on a color wheel, and 1 color is an |
||
253 | * alternate shade for the base color. |
||
254 | * |
||
255 | * @param float $h The base color hue degree (0 - 359) |
||
256 | * @param float $s The base color saturation percentage (0 - 100) |
||
257 | * @param float $l The base color lighting percentage (0 - 100) |
||
258 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
259 | * @return array An array of 5 rectangular colors where the first offset is the original input. |
||
260 | */ |
||
261 | 2 | View Code Duplication | public static function rectangular_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array { |
273 | |||
274 | /** |
||
275 | * Assigns keys to a hsl scheme array |
||
276 | * |
||
277 | * @param array $scheme The scheme to assign keys in |
||
278 | * @return array The resulting scheme |
||
279 | */ |
||
280 | 30 | protected static function _assign_keys(array $scheme) :array { |
|
287 | |||
288 | /** |
||
289 | * This prevents non-base colors from having either a too high or too low |
||
290 | * light value. If a value is too high or low, the resulting color sets will |
||
291 | * have many duplicate colors. This method doesn't prevent that, but it will |
||
292 | * reduce how often duplicate colors appear. |
||
293 | * |
||
294 | * @param float $light The light value to check |
||
295 | * @return float The alternate light value to use |
||
296 | */ |
||
297 | 30 | protected static function alt_light(float $light = 0.0) { |
|
300 | |||
301 | /** |
||
302 | * This prevents non-base colors from having either a too low saturation value. |
||
303 | * If a value is too low, the resulting color sets will have many duplicate |
||
304 | * colors. This method doesn't prevent that, but it will reduce how often |
||
305 | * duplicate colors appear. |
||
306 | * |
||
307 | * @param float $saturation The saturation value to check |
||
308 | * @return float The alternate saturation value to use |
||
309 | */ |
||
310 | 16 | protected static function alt_saturation(float $saturation = 0.0) { |
|
313 | |||
314 | /** |
||
315 | * This allows easy modification of a number while forcing it to fall into a valid range. |
||
316 | * |
||
317 | * @param float $number The number to modify |
||
318 | * @param float $adjustment The amount of change to make to the $number |
||
319 | * @param boolean $add TRUE to add $adjustment to $number, FALSE to subtract $adjustment from $number |
||
320 | * @param integer $max The maximum value to allow. (Minimum is assumed to be 0) |
||
321 | * @return float The resulting number. |
||
322 | */ |
||
323 | 30 | protected static function mod(float $number, float $adjustment, $add = TRUE, $max = 100) :float { |
|
329 | |||
330 | /** |
||
331 | * Check if an HSL color is dark (YIQ) |
||
332 | * |
||
333 | * @param float $h The hue degree (0 - 359) |
||
334 | * @param float $s The saturation percentage (0 - 100) |
||
335 | * @param float $l The lighting percentage (0 - 100) |
||
336 | * @return void |
||
337 | */ |
||
338 | 30 | protected static function is_dark(&$is_dark, float $h = 0.0, float $s = 0.0, float $l = 0.0) { |
|
345 | |||
346 | /** |
||
347 | * Generate a array of 5 rgb colors using the $scheme algorithm |
||
348 | * |
||
349 | * @param float $h The hue value |
||
350 | * @param float $s The saturation value |
||
351 | * @param float $l The light value |
||
352 | * @param string $scheme The scheme algorithm to use |
||
353 | * @return array Array of RGB colors |
||
354 | */ |
||
355 | 8 | public static function rgb(float $h = 0.0, float $s = 0.0, float $l = 0.0, string $scheme = '') :array { |
|
361 | |||
362 | /** |
||
363 | * Generate a array of 5 hsl colors using the $scheme algorithm |
||
364 | * |
||
365 | * @param float $h The hue value |
||
366 | * @param float $s The saturation value |
||
367 | * @param float $l The light value |
||
368 | * @param string $scheme The scheme algorithm to use |
||
369 | * @return array Array of HSL colors |
||
370 | */ |
||
371 | 12 | public static function hsl(float $h = 0.0, float $s = 0.0, float $l = 0.0, string $scheme = '') :array { |
|
383 | |||
384 | /** |
||
385 | * Generate a array of 5 hsb colors using the $scheme algorithm |
||
386 | * |
||
387 | * @param float $h The hue value |
||
388 | * @param float $s The saturation value |
||
389 | * @param float $l The light value |
||
390 | * @param string $scheme The scheme algorithm to use |
||
391 | * @return array Array of hex colors |
||
392 | */ |
||
393 | 2 | public static function hsb(float $h = 0.0, float $s = 0.0, float $l = 0.0, string $scheme = '') :array { |
|
399 | |||
400 | /** |
||
401 | * Generate a array of 5 hex colors using the $scheme algorithm |
||
402 | * |
||
403 | * @param float $h The hue value |
||
404 | * @param float $s The saturation value |
||
405 | * @param float $l The light value |
||
406 | * @param string $scheme The scheme algorithm to use |
||
407 | * @return array Array of hex colors |
||
408 | */ |
||
409 | 2 | public static function hex(float $h = 0.0, float $s = 0.0, float $l = 0.0, string $scheme = '') :array { |
|
415 | |||
416 | /** |
||
417 | * Generate a array of 5 cmyk colors using the $scheme algorithm |
||
418 | * |
||
419 | * @param float $h The hue value |
||
420 | * @param float $s The saturation value |
||
421 | * @param float $l The light value |
||
422 | * @param string $scheme The scheme algorithm to use |
||
423 | * @return array Array of CMYK colors |
||
424 | */ |
||
425 | 2 | public static function cmyk(float $h = 0.0, float $s = 0.0, float $l = 0.0, string $scheme = '') :array { |
|
431 | |||
432 | /** |
||
433 | * Convert a color scheme to another color space |
||
434 | * |
||
435 | * @param array $scheme The current color scheme |
||
436 | * @param callable $callback The conversion callback to use |
||
437 | * @return array The converted color scheme |
||
438 | */ |
||
439 | 8 | protected static function _convert(array $scheme, callable $callback) { |
|
446 | } |
||
447 |
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.