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 |
||
| 11 | class scheme { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * These colors are all close to each other on a color wheel. |
||
| 15 | * |
||
| 16 | * @param float|integer $h The base color hue degree (0 - 359) |
||
| 17 | * @param float|integer $s The base color saturation percentage (0 - 100) |
||
| 18 | * @param float|integer $l The base color lighting percentage (0 - 100) |
||
| 19 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
| 20 | * @return array An array of 5 analogous colors where the first offset is the original input. |
||
| 21 | */ |
||
| 22 | public static function analogous (float $h = 0, float $s = 0, float $l = 0, $is_dark = NULL) :array { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * 2 of these colors are a different shade of the base color. The other 2 are |
||
| 40 | * a weighted opposite of the base color. |
||
| 41 | * |
||
| 42 | * @param float|integer $h The base color hue degree (0 - 359) |
||
| 43 | * @param float|integer $s The base color saturation percentage (0 - 100) |
||
| 44 | * @param float|integer $l The base color lighting percentage (0 - 100) |
||
| 45 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
| 46 | * @return array An array of 5 complementary colors where the first offset is the original input. |
||
| 47 | */ |
||
| 48 | View Code Duplication | public static function complementary (float $h = 0, float $s = 0, float $l = 0, $is_dark = NULL) :array { |
|
| 58 | |||
| 59 | /** |
||
| 60 | * These colors use mathematical offsets that usually complement each other |
||
| 61 | * well, and can highlight the base color. |
||
| 62 | * |
||
| 63 | * @param float|integer $h The base color hue degree (0 - 359) |
||
| 64 | * @param float|integer $s The base color saturation percentage (0 - 100) |
||
| 65 | * @param float|integer $l The base color lighting percentage (0 - 100) |
||
| 66 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
| 67 | * @return array An array of 5 compounding colors where the first offset is the original input. |
||
| 68 | */ |
||
| 69 | public static function compound (float $h = 0, float $s = 0, float $l = 0, $is_dark = NULL) :array { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * 5 complementary shades of one color. |
||
| 87 | * |
||
| 88 | * @param float|integer $h The base color hue degree (0 - 359) |
||
| 89 | * @param float|integer $s The base color saturation percentage (0 - 100) |
||
| 90 | * @param float|integer $l The base color lighting percentage (0 - 100) |
||
| 91 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
| 92 | * @return array An array of 5 complementary shades of colors where the first offset is the original input. |
||
| 93 | */ |
||
| 94 | public static function monochromatic (float $h = 0, float $s = 0, float $l = 0, $is_dark = NULL) :array { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * 5 different shades of one color. |
||
| 112 | * |
||
| 113 | * @param float|integer $h The base color hue degree (0 - 359) |
||
| 114 | * @param float|integer $s The base color saturation percentage (0 - 100) |
||
| 115 | * @param float|integer $l The base color lighting percentage (0 - 100) |
||
| 116 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
| 117 | * @return array An array of 5 shades of a color where the first offset is the original input. |
||
| 118 | */ |
||
| 119 | public static function shades (float $h = 0, float $s = 0, float $l = 0, $is_dark = NULL) :array { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * 3 of these colors are all equally distanced from each other on a color |
||
| 137 | * wheel, plus 1 alternated shade for the base color and the 1 color that is |
||
| 138 | * opposite of the base color. |
||
| 139 | * |
||
| 140 | * @param float|integer $h The base color hue degree (0 - 359) |
||
| 141 | * @param float|integer $s The base color saturation percentage (0 - 100) |
||
| 142 | * @param float|integer $l The base color lighting percentage (0 - 100) |
||
| 143 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
| 144 | * @return array An array of 5 triangular colors where the first offset is the original input. |
||
| 145 | */ |
||
| 146 | View Code Duplication | public static function tetrad (float $h = 0, float $s = 0, float $l = 0, $is_dark = NULL) :array { |
|
| 156 | |||
| 157 | /** |
||
| 158 | * 3 of these colors are all similarly distanced from each other on a color |
||
| 159 | * wheel, the base color has an alternate shade, and there is a weighted |
||
| 160 | * opposite color. These colors are all slightly closer to the base color |
||
| 161 | * than in a normal tetrad. |
||
| 162 | * |
||
| 163 | * @param float|integer $h The base color hue degree (0 - 359) |
||
| 164 | * @param float|integer $s The base color saturation percentage (0 - 100) |
||
| 165 | * @param float|integer $l The base color lighting percentage (0 - 100) |
||
| 166 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
| 167 | * @return array An array of 5 triangular colors where the first offset is the original input. |
||
| 168 | */ |
||
| 169 | View Code Duplication | public static function weighted_tetrad (float $h = 0, float $s = 0, float $l = 0, $is_dark = NULL) :array { |
|
| 179 | |||
| 180 | /** |
||
| 181 | * These colors are all equally distanced from each other on a color wheel, |
||
| 182 | * 2 of which have an alternate shade. |
||
| 183 | * |
||
| 184 | * @param float|integer $h The base color hue degree (0 - 359) |
||
| 185 | * @param float|integer $s The base color saturation percentage (0 - 100) |
||
| 186 | * @param float|integer $l The base color lighting percentage (0 - 100) |
||
| 187 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
| 188 | * @return array An array of 5 triangular colors where the first offset is the original input. |
||
| 189 | */ |
||
| 190 | View Code Duplication | public static function triad (float $h = 0, float $s = 0, float $l = 0, $is_dark = NULL) :array { |
|
| 200 | |||
| 201 | /** |
||
| 202 | * These colors are all similarly distanced from each other on a color wheel, |
||
| 203 | * 2 of which have an alternate shade. These colors are all slightly closer to |
||
| 204 | * the base color than in a normal triad. |
||
| 205 | * |
||
| 206 | * @param float|integer $h The base color hue degree (0 - 359) |
||
| 207 | * @param float|integer $s The base color saturation percentage (0 - 100) |
||
| 208 | * @param float|integer $l The base color lighting percentage (0 - 100) |
||
| 209 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
| 210 | * @return array An array of 5 weighted triangular colors where the first offset is the original input. |
||
| 211 | */ |
||
| 212 | View Code Duplication | public static function weighted_triad (float $h = 0, float $s = 0, float $l = 0, $is_dark = NULL) :array { |
|
| 222 | |||
| 223 | /** |
||
| 224 | * 3 of these colors are all equally distanced from each other on a color |
||
| 225 | * wheel, plus 1 alternated shade for the base color and the 1 color that is |
||
| 226 | * opposite of the base color. |
||
| 227 | * |
||
| 228 | * @param float|integer $h The base color hue degree (0 - 359) |
||
| 229 | * @param float|integer $s The base color saturation percentage (0 - 100) |
||
| 230 | * @param float|integer $l The base color lighting percentage (0 - 100) |
||
| 231 | * @param bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this. |
||
| 232 | * @return array An array of 5 triangular colors where the first offset is the original input. |
||
| 233 | */ |
||
| 234 | public static function rectangular (float $h = 0, float $s = 0, float $l = 0, $is_dark = NULL) :array { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * This allows easy modification of a number while forcing it to fall into a valid range. |
||
| 247 | * |
||
| 248 | * @param float $number The number to modify |
||
| 249 | * @param float $adjustment The amount of change to make to the $number |
||
| 250 | * @param boolean $add TRUE to add $adjustment to $number, FALSE to subtract $adjustment from $number |
||
| 251 | * @param integer $max The maximum value to allow. (Minimum is assumed to be 0) |
||
| 252 | * @return float The resulting number. |
||
| 253 | */ |
||
| 254 | protected static function mod(float $number, float $adjustment, $add = TRUE, $max = 100) :float { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Check if an HSL color is dark (YIQ) |
||
| 263 | * |
||
| 264 | * @param float|integer $h The hue degree (0 - 359) |
||
| 265 | * @param float|integer $s The saturation percentage (0 - 100) |
||
| 266 | * @param float|integer $l The lighting percentage (0 - 100) |
||
| 267 | * @return boolean TRUE if the color is dark, FALSE otherwise. |
||
| 268 | */ |
||
| 269 | protected static function is_dark(&$is_dark, float $h = 0, float $s = 0, float $l = 0) { |
||
| 275 | } |
||
| 276 |
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.