| Conditions | 40 |
| Paths | 298 |
| Total Lines | 129 |
| Code Lines | 94 |
| Lines | 32 |
| Ratio | 24.81 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 107 | protected function setValues($value) |
||
| 108 | { |
||
| 109 | $value = strtolower(trim($value)); |
||
| 110 | // Find out which variant of color input it is |
||
| 111 | if (isset(ColorObject::$color_map[$value])) { |
||
| 112 | $variant = self::COLOR_NAME; |
||
| 113 | } elseif ($value[0] === '#') { |
||
| 114 | $value = substr($value, 1); |
||
| 115 | |||
| 116 | if (dechex(hexdec($value)) !== $value) { |
||
| 117 | return; |
||
| 118 | } |
||
| 119 | |||
| 120 | View Code Duplication | switch (strlen($value)) { |
|
| 121 | case 3: |
||
| 122 | $variant = self::COLOR_HEX_3; |
||
| 123 | break; |
||
| 124 | case 6: |
||
| 125 | $variant = self::COLOR_HEX_6; |
||
| 126 | break; |
||
| 127 | case 4: |
||
| 128 | $variant = self::COLOR_HEX_4; |
||
| 129 | break; |
||
| 130 | case 8: |
||
| 131 | $variant = self::COLOR_HEX_8; |
||
| 132 | break; |
||
| 133 | default: |
||
| 134 | return; |
||
| 135 | } |
||
| 136 | } else { |
||
| 137 | if (!preg_match('/^((?:rgb|hsl)a?)\s*\(([0-9\.%,\s]+)\)$/i', $value, $match)) { |
||
| 138 | return; |
||
| 139 | } |
||
| 140 | |||
| 141 | View Code Duplication | switch ($match[1]) { |
|
| 142 | case 'rgb': |
||
| 143 | $variant = self::COLOR_RGB; |
||
| 144 | break; |
||
| 145 | case 'rgba': |
||
| 146 | $variant = self::COLOR_RGBA; |
||
| 147 | break; |
||
| 148 | case 'hsl': |
||
| 149 | $variant = self::COLOR_HSL; |
||
| 150 | break; |
||
| 151 | case 'hsla': |
||
| 152 | $variant = self::COLOR_HSLA; |
||
| 153 | break; |
||
| 154 | default: |
||
| 155 | return; |
||
| 156 | } |
||
| 157 | |||
| 158 | $value = explode(',', $match[2]); |
||
| 159 | |||
| 160 | if ($this->hasAlpha($variant)) { |
||
| 161 | if (count($value) !== 4) { |
||
| 162 | return; |
||
| 163 | } |
||
| 164 | } elseif (count($value) !== 3) { |
||
| 165 | return; |
||
| 166 | } |
||
| 167 | |||
| 168 | foreach ($value as $i => &$color) { |
||
| 169 | $color = trim($color); |
||
| 170 | |||
| 171 | if (strpos($color, '%') !== false) { |
||
| 172 | $color = str_replace('%', '', $color); |
||
| 173 | |||
| 174 | if ($i === 3) { |
||
| 175 | $color = $color / 100; |
||
| 176 | } elseif (in_array($variant, array(self::COLOR_RGB, self::COLOR_RGBA))) { |
||
| 177 | $color = round($color / 100 * 255); |
||
| 178 | } elseif ($i === 0 && in_array($variant, array(self::COLOR_HSL, self::COLOR_HSLA))) { |
||
| 179 | $color = $color / 100 * 360; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | if ($i === 0 && in_array($variant, array(self::COLOR_HSL, self::COLOR_HSLA))) { |
||
| 184 | $color = ($color % 360 + 360) % 360; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | // Assign the correct properties based on the variant |
||
| 190 | switch ($variant) { |
||
| 191 | case self::COLOR_HEX_4: |
||
| 192 | $this->a = hexdec($value[3]) / 0xF; |
||
| 193 | // Fallthrough |
||
| 194 | case self::COLOR_HEX_3: |
||
| 195 | $this->r = hexdec($value[0]) * 0x11; |
||
| 196 | $this->g = hexdec($value[1]) * 0x11; |
||
| 197 | $this->b = hexdec($value[2]) * 0x11; |
||
| 198 | break; |
||
| 199 | case self::COLOR_NAME: |
||
| 200 | $value = ColorObject::$color_map[$value].'FF'; |
||
| 201 | // Fallthrough |
||
| 202 | case self::COLOR_HEX_8: |
||
| 203 | $this->a = hexdec(substr($value, 6, 2)) / 0xFF; |
||
| 204 | // Fallthrough |
||
| 205 | case self::COLOR_HEX_6: |
||
| 206 | $value = str_split($value, 2); |
||
| 207 | $this->r = hexdec($value[0]); |
||
| 208 | $this->g = hexdec($value[1]); |
||
| 209 | $this->b = hexdec($value[2]); |
||
| 210 | break; |
||
| 211 | case self::COLOR_RGBA: |
||
| 212 | $this->a = $value[3]; |
||
| 213 | // Fallthrough |
||
| 214 | case self::COLOR_RGB: |
||
| 215 | list($this->r, $this->g, $this->b) = $value; |
||
| 216 | break; |
||
| 217 | case self::COLOR_HSLA: |
||
| 218 | $this->a = $value[3]; |
||
| 219 | // Fallthrough |
||
| 220 | case self::COLOR_HSL: |
||
| 221 | if (min($value) < 0 || $value[0] > 360 || max($value[1], $value[2]) > 100) { |
||
| 222 | return; |
||
| 223 | } |
||
| 224 | $value = ColorObject::hslToRgb($value[0], $value[1], $value[2]); |
||
| 225 | list($this->r, $this->g, $this->b) = $value; |
||
| 226 | break; |
||
| 227 | } |
||
| 228 | |||
| 229 | // If something has gone horribly wrong |
||
| 230 | if ($this->r > 0xFF || $this->g > 0xFF || $this->b > 0xFF || $this->a > 1) { |
||
| 231 | $this->variant = null; |
||
| 232 | } else { |
||
| 233 | $this->variant = $variant; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.