| Conditions | 12 |
| Paths | 138 |
| Total Lines | 36 |
| Code Lines | 22 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 46 | public static function hex_invert(string $color): string |
||
| 47 | { |
||
| 48 | $color = trim($color); |
||
| 49 | $prependHash = false; |
||
| 50 | if (false !== strpos($color, '#')) { |
||
| 51 | $prependHash = true; |
||
| 52 | $color = str_replace('#', '', $color); |
||
| 53 | } |
||
| 54 | |||
| 55 | $len = strlen($color); |
||
| 56 | if (3 === $len || 6 === $len || 8 === $len) { |
||
| 57 | if (8 === $len) { |
||
| 58 | $color = substr($color, 0, 6); |
||
| 59 | } |
||
| 60 | |||
| 61 | if (3 === $len) { |
||
| 62 | $color = preg_replace('#(.)(.)(.)#', '\\1\\1\\2\\2\\3\\3', $color); |
||
| 63 | } |
||
| 64 | } else { |
||
| 65 | throw new \Exception("Invalid hex length ({$len}). Length must be 3 or 6 characters - colour is" . $color); |
||
| 66 | } |
||
| 67 | |||
| 68 | if (! preg_match('#^[a-f0-9]{6}$#i', $color)) { |
||
| 69 | throw new \Exception(sprintf('Invalid hex string #%s', htmlspecialchars($color, ENT_QUOTES))); |
||
| 70 | } |
||
| 71 | |||
| 72 | $r = dechex(255 - hexdec(substr($color, 0, 2))); |
||
|
|
|||
| 73 | $r = (strlen($r) > 1) ? $r : '0' . $r; |
||
| 74 | |||
| 75 | $g = dechex(255 - hexdec(substr($color, 2, 2))); |
||
| 76 | $g = (strlen($g) > 1) ? $g : '0' . $g; |
||
| 77 | |||
| 78 | $b = dechex(255 - hexdec(substr($color, 4, 2))); |
||
| 79 | $b = (strlen($b) > 1) ? $b : '0' . $b; |
||
| 80 | |||
| 81 | return ($prependHash ? '#' : '') . $r . $g . $b; |
||
| 82 | } |
||
| 117 |