| Conditions | 5 |
| Paths | 9 |
| Total Lines | 58 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 65 | function write($string, $newline = false) |
||
| 66 | { |
||
| 67 | $array = array("000" => self::b_black, |
||
| 68 | "100" => self::red, |
||
| 69 | "010" => self::green, |
||
| 70 | "110" => self::yellow, |
||
| 71 | "001" => self::blue, |
||
| 72 | "011" => self::magenta, |
||
| 73 | "101" => self::cyan, |
||
| 74 | "111" => self::white, |
||
| 75 | "200" => self::b_red, |
||
| 76 | "211" => self::red, |
||
| 77 | "121" => self::green, |
||
| 78 | "020" => self::b_green, |
||
| 79 | "021" => self::green, |
||
| 80 | "012" => self::cyan, |
||
| 81 | "221" => self::b_yellow, |
||
| 82 | "220" => self::b_yellow, |
||
| 83 | "120" => self::green, |
||
| 84 | "210" => self::yellow, |
||
| 85 | "112" => self::b_blue, |
||
| 86 | "002" => self::b_blue, |
||
| 87 | "122" => self::b_cyan, |
||
| 88 | "022" => self::b_cyan, |
||
| 89 | "202" => self::b_magenta, |
||
| 90 | "212" => self::b_magenta, |
||
| 91 | "102" => self::magenta, |
||
| 92 | "201" => self::b_red, |
||
| 93 | "222" => self::b_white, |
||
| 94 | ); |
||
| 95 | $matches = array(); |
||
| 96 | preg_match_all("/\\$[A-Fa-f0-9]{3}/", $string, $matches); |
||
| 97 | $split = preg_split("/\\$[A-Fa-f0-9]{3}/", $string); |
||
| 98 | |||
| 99 | $out = ""; |
||
| 100 | foreach ($matches[0] as $i => $rgb) { |
||
| 101 | $code = $this->fix(hexdec($rgb[1]), hexdec($rgb[2]), hexdec($rgb[3])); |
||
| 102 | if (array_key_exists($code, $array)) { |
||
| 103 | $out .= $array[$code] . $this->stripStyles($split[$i + 1]); |
||
| 104 | } else { |
||
| 105 | $out .= self::white . $this->stripStyles($split[$i + 1]); |
||
| 106 | } |
||
| 107 | $end = $this->stripStyles($split[$i + 1]); |
||
| 108 | } |
||
| 109 | |||
| 110 | |||
| 111 | if (!empty($end)) { |
||
| 112 | if ($end == $this->stripStyles(end($split))) { |
||
| 113 | $end = ""; |
||
| 114 | } |
||
| 115 | } else { |
||
| 116 | $end = ""; |
||
| 117 | } |
||
| 118 | |||
| 119 | $out = self::white . $this->stripStyles(reset($split)) . $out . $end; |
||
| 120 | |||
| 121 | $this->ansiOut($out, $newline); |
||
| 122 | } |
||
| 123 | |||
| 182 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.