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 main extends main_peripheral { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Instance of color for this object |
||
| 21 | * @var color |
||
| 22 | */ |
||
| 23 | public $color; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Instance of cache for this object |
||
| 27 | * @var cache |
||
| 28 | */ |
||
| 29 | protected $cache; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Setup color and cache for this object |
||
| 33 | * |
||
| 34 | * @param mixed $color A color described as Hex string or int, RGB array, HSL array, HSB array, CMYK array, or instance of color |
||
| 35 | * @param string $type The type of color to process $color as |
||
| 36 | */ |
||
| 37 | 1 | public function __construct($color, string $type = '') { |
|
| 38 | 1 | $this->set($color, $type); |
|
| 39 | 1 | $this->cache = new data\cache; |
|
|
|
|||
| 40 | 1 | } |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Configure the color instance for this object |
||
| 44 | * |
||
| 45 | * @param mixed $color A color described as Hex string or int, RGB array, HSL array, HSB array, CMYK array, or instance of color |
||
| 46 | * @param string $type The type of color to process $color as |
||
| 47 | */ |
||
| 48 | 1 | protected function set($color, string $type = '') { |
|
| 49 | 1 | if (is_a($color, __CLASS__)) { |
|
| 50 | $this->color = clone $color->color; |
||
| 51 | } else { |
||
| 52 | 1 | $this->color = new data\store($color, $type); |
|
| 53 | } |
||
| 54 | 1 | } |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Return the current color as an RGB array |
||
| 58 | * |
||
| 59 | * @return array RGB array |
||
| 60 | */ |
||
| 61 | 1 | public function rgb() :array { |
|
| 62 | 1 | return (array) $this->color->rgb + ['a' => $this->color->alpha()]; |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Return the current color as an HSL array |
||
| 67 | * |
||
| 68 | * @return array HSL array |
||
| 69 | */ |
||
| 70 | 1 | public function hsl(int $accuracy = 0) :array { |
|
| 71 | 1 | $color = []; |
|
| 72 | 1 | foreach($this->color->hsl() as $key => $value) { |
|
| 73 | 1 | $color[$key] = round($value, abs($accuracy)); |
|
| 74 | } |
||
| 75 | 1 | return $color + ['a' => $this->color->alpha()]; |
|
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Return the current color as an CMYK array |
||
| 80 | * |
||
| 81 | * @return array CMYK array |
||
| 82 | */ |
||
| 83 | 1 | public function cmyk() :array { |
|
| 84 | 1 | if (!is_null($cached = $this->cache->get(__FUNCTION__, $this->hex()))) { |
|
| 85 | return $cached; |
||
| 86 | } |
||
| 87 | 1 | $rgb = $this->color->rgb; |
|
| 88 | 1 | $result = convert\rgb::to_cmyk($rgb['r'], $rgb['g'], $rgb['b']) + ['a' => $this->color->alpha()]; |
|
| 89 | 1 | $this->cache->set(__FUNCTION__, $this->hex(), $result); |
|
| 90 | 1 | return $result; |
|
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Return the current color as an HSB array |
||
| 95 | * |
||
| 96 | * @return array HSB array |
||
| 97 | */ |
||
| 98 | 1 | public function hsb(int $accuracy = 0) :array { |
|
| 99 | 1 | if (!is_null($cached = $this->cache->get(__FUNCTION__, $this->hex()))) { |
|
| 100 | return $cached; |
||
| 101 | } |
||
| 102 | 1 | $rgb = $this->color->rgb; |
|
| 103 | 1 | $result = convert\rgb::to_hsb($rgb['r'], $rgb['g'], $rgb['b'], $accuracy) + ['a' => $this->color->alpha()]; |
|
| 104 | 1 | $this->cache->set(__FUNCTION__, $this->hex(), $result); |
|
| 105 | 1 | return $result; |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Return the current color as an hex string |
||
| 110 | * |
||
| 111 | * @return string hex string |
||
| 112 | */ |
||
| 113 | 1 | public function hex() :string { |
|
| 114 | 1 | return $this->color->hex; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Return the current color as an web-safe hex string |
||
| 119 | * |
||
| 120 | * @return string web-safe hex string |
||
| 121 | */ |
||
| 122 | 1 | View Code Duplication | public function web_safe() :string { |
| 123 | 1 | if (!is_null($cached = $this->cache->get(__FUNCTION__, $this->hex()))) { |
|
| 124 | return $cached; |
||
| 125 | } |
||
| 126 | 1 | $rgb = $this->color->rgb; |
|
| 127 | 1 | $result = generate::web_safe($rgb['r'], $rgb['g'], $rgb['b']); |
|
| 128 | 1 | $this->cache->set(__FUNCTION__, $this->hex(), $result); |
|
| 129 | 1 | return $result; |
|
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Return the current color as an CSS value |
||
| 134 | * |
||
| 135 | * @return string CSS value |
||
| 136 | */ |
||
| 137 | 1 | public function css() :string { |
|
| 138 | 1 | return css::best($this->color); |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get (and set) the alpha channel |
||
| 143 | * |
||
| 144 | * @param mixed $new_alpha If numeric, the alpha channel is set to this value |
||
| 145 | * @return float The current alpha value |
||
| 146 | */ |
||
| 147 | public function alpha($new_alpha = NULL) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Check if the current color would be considered visually dark |
||
| 153 | * |
||
| 154 | * @param int $check_score Minimum score to be considered light (0-255; defaults to 128) |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | View Code Duplication | public function is_dark(int $check_score = 128) :bool { |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Adjust the red value of the current color |
||
| 169 | * |
||
| 170 | * @param float $adjustment The amount to adjust the color by |
||
| 171 | * @param bool $as_percentage Whether to treat $amount as a percentage |
||
| 172 | * @param bool $set_absolute TRUE to use "set" mode, where the $adjustment is the absolute value you want for this attribute |
||
| 173 | * @return void |
||
| 174 | */ |
||
| 175 | public function red(float $adjustment = 0.0, bool $as_percentage = FALSE, bool $set_absolute = FALSE) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Adjust the green value of the current color |
||
| 184 | * |
||
| 185 | * @param float $adjustment The amount to adjust the color by |
||
| 186 | * @param bool $as_percentage Whether to treat $amount as a percentage |
||
| 187 | * @param bool $set_absolute TRUE to use "set" mode, where the $adjustment is the absolute value you want for this attribute |
||
| 188 | * @return void |
||
| 189 | */ |
||
| 190 | public function green(float $adjustment = 0.0, bool $as_percentage = FALSE, bool $set_absolute = FALSE) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Adjust the blue value of the current color |
||
| 199 | * |
||
| 200 | * @param float $adjustment The amount to adjust the color by |
||
| 201 | * @param bool $as_percentage Whether to treat $amount as a percentage |
||
| 202 | * @param bool $set_absolute TRUE to use "set" mode, where the $adjustment is the absolute value you want for this attribute |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | public function blue(float $adjustment = 0.0, bool $as_percentage = FALSE, bool $set_absolute = FALSE) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Adjust the hue value of the current color |
||
| 214 | * |
||
| 215 | * @param float $adjustment The amount to adjust the color by |
||
| 216 | * @param bool $as_percentage Whether to treat $amount as a percentage |
||
| 217 | * @param bool $set_absolute TRUE to use "set" mode, where the $adjustment is the absolute value you want for this attribute |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | public function hue(float $adjustment = 0.0, bool $as_percentage = FALSE, bool $set_absolute = FALSE) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Adjust the saturation value of the current color |
||
| 229 | * |
||
| 230 | * @param float $adjustment The amount to adjust the color by |
||
| 231 | * @param bool $as_percentage Whether to treat $amount as a percentage |
||
| 232 | * @param bool $set_absolute TRUE to use "set" mode, where the $adjustment is the absolute value you want for this attribute |
||
| 233 | * @return void |
||
| 234 | */ |
||
| 235 | public function saturation(float $adjustment = 0.0, bool $as_percentage = FALSE, bool $set_absolute = FALSE) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Adjust the light value of the current color |
||
| 244 | * |
||
| 245 | * @param float $adjustment The amount to adjust the color by |
||
| 246 | * @param bool $as_percentage Whether to treat $amount as a percentage |
||
| 247 | * @param bool $set_absolute TRUE to use "set" mode, where the $adjustment is the absolute value you want for this attribute |
||
| 248 | * @return void |
||
| 249 | */ |
||
| 250 | public function light(float $adjustment = 0.0, bool $as_percentage = FALSE, bool $set_absolute = FALSE) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Blend 2 colors together to generate a new color. |
||
| 259 | * |
||
| 260 | * @param main $color2 The second color to blend with this color |
||
| 261 | * @param float $amount The amount as a percentage of the second color to add to the first |
||
| 262 | * @return main The resulting color as an instance of main. |
||
| 263 | */ |
||
| 264 | public function blend(main $color2, float $amount = 50.0) :main { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Create a gradient of colors as an array |
||
| 276 | * |
||
| 277 | * @param main $color2 The "end" gradient. (The current color is the start) |
||
| 278 | * @param int $steps The number of steps to have in the gradient. 0 will make it generate one step for every unique RGB color in the gradient. |
||
| 279 | * @return array The resulting gradient as an array |
||
| 280 | */ |
||
| 281 | public function gradient(main $color2, int $steps = 0, string $return_type = 'rgb', string $mode = 'rgb') :array { |
||
| 282 | $c1 = $this->rgb(); |
||
| 283 | $c2 = $color2->rgb(); |
||
| 284 | return generate::gradient_range( |
||
| 285 | $c1['r'], $c1['g'], $c1['b'], |
||
| 286 | $c2['r'], $c2['g'], $c2['b'], |
||
| 287 | $steps |
||
| 288 | ); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Generate a color scheme based on the current color. |
||
| 293 | * |
||
| 294 | * Available scheme algorithms: |
||
| 295 | * - analogous |
||
| 296 | * - complementary |
||
| 297 | * - compound |
||
| 298 | * - monochromatic |
||
| 299 | * - shades |
||
| 300 | * - tetrad |
||
| 301 | * - weighted_tetrad |
||
| 302 | * - triad |
||
| 303 | * - weighted_triad |
||
| 304 | * - rectangular |
||
| 305 | * |
||
| 306 | * @param string $scheme_name The scheme algorithm to use |
||
| 307 | * @param string $return_type The type of values the scheme will have (hex, rgb, hsl, hsb, cmyk) |
||
| 308 | * @return array The resulting scheme, where offset 0 is the original color |
||
| 309 | */ |
||
| 310 | public function scheme(string $scheme_name, string $return_type = 'hex') :array { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Generate a color scheme, with YIQ weights, based on the current color. |
||
| 316 | * |
||
| 317 | * Available scheme algorithms: |
||
| 318 | * - analogous |
||
| 319 | * - complementary |
||
| 320 | * - compound |
||
| 321 | * - monochromatic |
||
| 322 | * - shades |
||
| 323 | * - tetrad |
||
| 324 | * - weighted_tetrad |
||
| 325 | * - triad |
||
| 326 | * - weighted_triad |
||
| 327 | * - rectangular |
||
| 328 | * |
||
| 329 | * @param string $scheme_name The scheme algorithm to use |
||
| 330 | * @param string $return_type The type of values the scheme will have (hex, rgb, hsl, hsb, cmyk) |
||
| 331 | * @return array The resulting YIQ scheme, where offset 0 is the original color |
||
| 332 | */ |
||
| 333 | public function yiq_scheme(string $scheme_name, string $return_type = 'hex') :array { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Generate a new instance of main with a random RGB color |
||
| 339 | * |
||
| 340 | * @param int $min_r Minimum red value allowed |
||
| 341 | * @param int $max_r Maximum red value allowed |
||
| 342 | * @param int $min_g Minimum green value allowed |
||
| 343 | * @param int $max_g Maximum green value allowed |
||
| 344 | * @param int $min_b Minimum blue value allowed |
||
| 345 | * @param int $max_b Maximum blue value allowed |
||
| 346 | * @return main The resulting instance of main |
||
| 347 | */ |
||
| 348 | public function rgb_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) :main { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Generate a new instance of main with a random HSL color |
||
| 354 | * |
||
| 355 | * @param int $min_h Minimum hue value allowed |
||
| 356 | * @param int $max_h Maximum hue value allowed |
||
| 357 | * @param int $min_s Minimum saturation value allowed |
||
| 358 | * @param int $max_s Maximum saturation value allowed |
||
| 359 | * @param int $min_l Minimum light value allowed |
||
| 360 | * @param int $max_l Maximum light value allowed |
||
| 361 | * @return main The resulting instance of main |
||
| 362 | */ |
||
| 363 | public function hsl_rand(int $min_h = 0, int $max_h = 255, int $min_s = 0, int $max_s = 255, int $min_l = 0, int $max_l = 255) :main { |
||
| 366 | } |
||
| 367 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..