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 |
||
| 11 | class EncodingConverter implements RobotsTxtInterface |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * String to convert |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private $string; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * String encoding |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $encoding; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * CharacterEncodingConvert constructor. |
||
| 27 | * |
||
| 28 | * @param string $string |
||
| 29 | * @param string $encoding |
||
| 30 | */ |
||
| 31 | public function __construct($string, $encoding) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Auto mode |
||
| 39 | * |
||
| 40 | * @return string|false |
||
| 41 | */ |
||
| 42 | public function auto() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * intl |
||
| 64 | * @link http://php.net/manual/en/uconverter.convert.php |
||
| 65 | * |
||
| 66 | * @return string|false |
||
| 67 | */ |
||
| 68 | View Code Duplication | public function intl() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * iconv |
||
| 82 | * @link http://php.net/manual/en/function.iconv.php |
||
| 83 | * |
||
| 84 | * @param string $outSuffix |
||
| 85 | * @return string|false |
||
| 86 | */ |
||
| 87 | View Code Duplication | public function iconv($outSuffix = '//TRANSLIT//IGNORE') |
|
| 97 | |||
| 98 | /** |
||
| 99 | * mbstring |
||
| 100 | * @link http://php.net/manual/en/function.mb-convert-encoding.php |
||
| 101 | * |
||
| 102 | * @param array|string|null $fromOverride |
||
| 103 | * @return string|false |
||
| 104 | */ |
||
| 105 | public function mbstring($fromOverride = null) |
||
| 115 | } |
||
| 116 |
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.