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 generate { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Generate the most contrasting color for any RGB color |
||
| 15 | * |
||
| 16 | * @param int $r The red value |
||
| 17 | * @param int $g The green value |
||
| 18 | * @param int $b The blue value |
||
| 19 | * @return array The most contrasting color as an RGB array |
||
| 20 | */ |
||
| 21 | 1 | public static function rgb_contrast(int $r = 0, int $g = 0, int $b = 0) :array { |
|
| 28 | |||
| 29 | /** |
||
| 30 | * Generate the mathematical opposite color for any RGB color |
||
| 31 | * |
||
| 32 | * @param int $r The red value |
||
| 33 | * @param int $g The green value |
||
| 34 | * @param int $b The blue value |
||
| 35 | * @return array The mathematical opposite color as an RGB array |
||
| 36 | */ |
||
| 37 | 1 | public static function rgb_invert(int $r = 0, int $g = 0, int $b = 0) :array { |
|
| 44 | |||
| 45 | /** |
||
| 46 | * Generate the YIQ score for an RGB color |
||
| 47 | * |
||
| 48 | * @param int $r The red value |
||
| 49 | * @param int $g The green value |
||
| 50 | * @param int $b The blue value |
||
| 51 | * @return float The YIQ score |
||
| 52 | */ |
||
| 53 | 32 | public static function yiq_score(int $r = 0, int $g = 0, int $b = 0) :float { |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Generate a random RGB color |
||
| 59 | * |
||
| 60 | * @param int $min_r The min red to allow |
||
| 61 | * @param int $max_r The max red to allow |
||
| 62 | * @param int $min_g The min green to allow |
||
| 63 | * @param int $max_g The max green to allow |
||
| 64 | * @param int $min_b The min blue to allow |
||
| 65 | * @param int $max_b The max blue to allow |
||
| 66 | * @return array The resulting color as a RGB array |
||
| 67 | */ |
||
| 68 | 2 | public static 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) :array { |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Generate a random HSL color |
||
| 78 | * |
||
| 79 | * @param int $min_r The min hue to allow |
||
|
|
|||
| 80 | * @param int $max_r The max hue to allow |
||
| 81 | * @param int $min_g The min saturation to allow |
||
| 82 | * @param int $max_g The max saturation to allow |
||
| 83 | * @param int $min_b The min light to allow |
||
| 84 | * @param int $max_b The max light to allow |
||
| 85 | * @return array The resulting color as a HSL array |
||
| 86 | */ |
||
| 87 | 1 | public static function hsl_rand(int $min_h = 0, int $max_h = 359, int $min_s = 0, int $max_s = 100, int $min_l = 0, int $max_l = 100) :array { |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Blend 2 RGB colors |
||
| 97 | * |
||
| 98 | * @param float $r1 The red value from color 1 |
||
| 99 | * @param float $g1 The green value from color 1 |
||
| 100 | * @param float $b1 The blue value from color 1 |
||
| 101 | * @param float $a1 The alpha value from color 1 |
||
| 102 | * @param float $r2 The red value from color 2 |
||
| 103 | * @param float $g2 The green value from color 2 |
||
| 104 | * @param float $b2 The blue value from color 2 |
||
| 105 | * @param float $a2 The alpha value from color 2 |
||
| 106 | * @param float $amount The percentage of color 2 to mix in (defaults to 50 for even blending) |
||
| 107 | * @return array The resulting color as a RGB array |
||
| 108 | */ |
||
| 109 | 1 | public static function blend(float $r1, float $g1, float $b1, float $a1, float $r2, float $g2, float $b2, float $a2, float $amount = 50.0) :array { |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Generate a gradient range between 2 RGB colors |
||
| 122 | * |
||
| 123 | * @param int $r1 The red value from color 1 |
||
| 124 | * @param int $g1 The green value from color 1 |
||
| 125 | * @param int $b1 The blue value from color 1 |
||
| 126 | * @param int $r2 The red value from color 2 |
||
| 127 | * @param int $g2 The green value from color 2 |
||
| 128 | * @param int $b2 The blue value from color 2 |
||
| 129 | * @param int $steps The size of array to produce, 0 will dynamically |
||
| 130 | * @return array [TODO] |
||
| 131 | */ |
||
| 132 | 1 | public static function gradient_range(int $r1 = 0, int $g1 = 0, int $b1 = 0, int $r2 = 0, int $g2 = 0, int $b2 = 0, int $steps = 0) :array { |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Round a RGB input to a 'websafe' color hex |
||
| 155 | * |
||
| 156 | * @param int $r The red value |
||
| 157 | * @param int $g The green value |
||
| 158 | * @param int $b The blue value |
||
| 159 | * @return string The resulting color as a hex string |
||
| 160 | */ |
||
| 161 | 2 | public static function web_safe(int $r = 0, int $g = 0, int $b = 0) :string { |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Converts a hue to the "Y" spectrum |
||
| 171 | * |
||
| 172 | * @param float $hue The hue to convert |
||
| 173 | * @return float The resulting Y |
||
| 174 | */ |
||
| 175 | 9 | View Code Duplication | public static function hue_to_yiq(float $hue) :float { |
| 185 | |||
| 186 | /** |
||
| 187 | * Converts a "Y" to the hue spectrum |
||
| 188 | * |
||
| 189 | * @param float $yiq The "Y" to convert |
||
| 190 | * @return float The resulting hue |
||
| 191 | */ |
||
| 192 | 9 | View Code Duplication | public static function yiq_to_hue(float $yiq) :float { |
| 202 | } |
||
| 203 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.