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 |
||
| 24 | class TUtf8Converter |
||
| 25 | { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Convert strings to UTF-8 via iconv. NB, the result may not by UTF-8 |
||
| 29 | * if the conversion failed. |
||
| 30 | * @param string $string string to convert to UTF-8 |
||
| 31 | * @param string $from source encoding |
||
| 32 | * @return string UTF-8 encoded string, original string if iconv failed. |
||
| 33 | */ |
||
| 34 | View Code Duplication | public static function toUTF8($string, $from) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Convert UTF-8 strings to a different encoding. NB. The result |
||
| 45 | * may not have been encoded if iconv fails. |
||
| 46 | * @param string $string the UTF-8 string for conversion |
||
| 47 | * @param string $to destination encoding |
||
| 48 | * @return string encoded string. |
||
| 49 | */ |
||
| 50 | View Code Duplication | public static function fromUTF8($string, $to) |
|
| 58 | } |
||
| 59 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.