| Conditions | 16 |
| Paths | 13 |
| Total Lines | 77 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 152 | public static function qpContentTransferEncode($input, $max_length = 76) |
||
| 153 | { |
||
| 154 | $qpHexDigits = '0123456789ABCDEF'; |
||
| 155 | $input_length = strlen($input); |
||
| 156 | $line_limit = $max_length; |
||
| 157 | $line_length = 0; |
||
| 158 | $output = ''; |
||
| 159 | $blank = false; |
||
| 160 | |||
| 161 | for ($i=0; $i < $input_length; $i++) { |
||
| 162 | $char = $input[$i]; |
||
| 163 | $ascii = ord($char); |
||
| 164 | |||
| 165 | // No encoding for spaces and tabs |
||
| 166 | if ($ascii == 9 || $ascii == 32) { |
||
| 167 | $blank = true; |
||
| 168 | $replace_length = 1; |
||
| 169 | $replace_char = $char; |
||
| 170 | |||
| 171 | // CR and LF |
||
| 172 | } elseif ($ascii == 13 || $ascii == 10) { |
||
| 173 | // Use existing offset only. |
||
| 174 | if ($i+1 < $input_length) { |
||
| 175 | if (($ascii == 13 && ord($input[$i+1]) == 10) || ($ascii == 10 && ord($input[$i+1]) == 13)) { |
||
| 176 | $i++; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | if ($blank) { |
||
| 181 | /** |
||
| 182 | * Any tab or space characters on an encoded line MUST |
||
| 183 | * be followed on that line by a printable character. |
||
| 184 | * This character may as well be the soft line break |
||
| 185 | * indicator. |
||
| 186 | * |
||
| 187 | * So if the preceding character is a space or a |
||
| 188 | * tab, we may simply insert a soft line break |
||
| 189 | * here, followed by a literal line break. |
||
| 190 | * Basically this means that we are appending |
||
| 191 | * an empty line (nada). |
||
| 192 | */ |
||
| 193 | $output .= "=\r\n\r\n"; |
||
| 194 | } else { |
||
| 195 | $output .= "\r\n"; |
||
| 196 | } |
||
| 197 | |||
| 198 | $blank = false; |
||
| 199 | $line_length = 0; |
||
| 200 | continue; |
||
| 201 | |||
| 202 | // No encoding within ascii range 33 to 126 (exception: 61) |
||
| 203 | } elseif (32 < $ascii && $ascii < 127 && $char !== '=') { |
||
| 204 | $replace_length = 1; |
||
| 205 | $replace_char = $char; |
||
| 206 | $blank = false; |
||
| 207 | |||
| 208 | // Encode |
||
| 209 | } else { |
||
| 210 | $replace_length = 3; |
||
| 211 | // bit operation is around 10 percent faster |
||
| 212 | // than 'strtoupper(dechex($ascii))' |
||
| 213 | $replace_char = '=' |
||
| 214 | . $qpHexDigits[$ascii >> 4] |
||
| 215 | . $qpHexDigits[$ascii & 0x0f]; |
||
| 216 | $blank = false; |
||
| 217 | } |
||
| 218 | // Would the line become too long? |
||
| 219 | if ($line_length + $replace_length > $line_limit - 1) { |
||
| 220 | $output .= "=\r\n"; |
||
| 221 | $line_length = 0; |
||
| 222 | } |
||
| 223 | |||
| 224 | $output .= $replace_char; |
||
| 225 | $line_length += $replace_length; |
||
| 226 | } |
||
| 227 | |||
| 228 | return $output; |
||
| 229 | } |
||
| 266 |