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 |
||
| 26 | class Pinyin |
||
| 27 | { |
||
| 28 | const NONE = 'none'; |
||
| 29 | const ASCII = 'ascii'; |
||
| 30 | const UNICODE = 'unicode'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Dict loader. |
||
| 34 | * |
||
| 35 | * @var \Overtrue\Pinyin\DictLoaderInterface |
||
| 36 | */ |
||
| 37 | protected $loader; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Punctuations map. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $punctuations = array( |
||
| 45 | ',' => ',', |
||
| 46 | '。' => '.', |
||
| 47 | '!' => '!', |
||
| 48 | '?' => '?', |
||
| 49 | ':' => ':', |
||
| 50 | '“' => '"', |
||
| 51 | '”' => '"', |
||
| 52 | '‘' => "'", |
||
| 53 | '’' => "'", |
||
| 54 | ); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Constructor. |
||
| 58 | * |
||
| 59 | * @param \Overtrue\Pinyin\DictLoaderInterface $loader |
||
| 60 | */ |
||
| 61 | 9 | public function __construct(DictLoaderInterface $loader = null) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Convert string to pinyin. |
||
| 68 | * |
||
| 69 | * @param string $string |
||
| 70 | * @param string $option |
||
| 71 | * |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | 4 | View Code Duplication | public function convert($string, $option = self::NONE) |
| 88 | |||
| 89 | /** |
||
| 90 | * Convert string (person name) to pinyin. |
||
| 91 | * |
||
| 92 | * @param string $stringName |
||
| 93 | * @param string $option |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | View Code Duplication | public function convertName($stringName, $option = self::NONE) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Return a pinyin permlink from string. |
||
| 114 | * |
||
| 115 | * @param string $string |
||
| 116 | * @param string $delimiter |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | 1 | public function permlink($string, $delimiter = '-') |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Return first letters. |
||
| 131 | * |
||
| 132 | * @param string $string |
||
| 133 | * @param string $delimiter |
||
| 134 | * |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | 1 | public function abbr($string, $delimiter = '') |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Chinese to pinyin sentense. |
||
| 146 | * |
||
| 147 | * @param string $sentence |
||
| 148 | * @param string $option |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | 5 | public function sentence($sentence, $withTone = false) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Loader setter. |
||
| 168 | * |
||
| 169 | * @param \Overtrue\Pinyin\DictLoaderInterface $loader |
||
| 170 | * |
||
| 171 | * @return $this |
||
| 172 | */ |
||
| 173 | 1 | public function setLoader(DictLoaderInterface $loader) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Return dict loader,. |
||
| 182 | * |
||
| 183 | * @return \Overtrue\Pinyin\DictLoaderInterface |
||
| 184 | */ |
||
| 185 | 9 | public function getLoader() |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Preprocess. |
||
| 192 | * |
||
| 193 | * @param string $string |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | 9 | protected function prepare($string) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Convert Chinese to pinyin. |
||
| 208 | * |
||
| 209 | * @param string $string |
||
| 210 | * @param bool $isName |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | 9 | protected function romanize($string, $isName = false) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Convert Chinese Surname to pinyin. |
||
| 233 | * |
||
| 234 | * @param string $string |
||
| 235 | * @param \Overtrue\Pinyin\DictLoaderInterface $dictLoader |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | protected function convertSurname($string, $dictLoader) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Format. |
||
| 254 | * |
||
| 255 | * @param string $pinyin |
||
| 256 | * @param bool $tone |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | 7 | protected function format($pinyin, $tone = false) |
|
| 278 | } |
||
| 279 |
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.