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 |
||
| 7 | class Emojify |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * The path to the file containing the emoji map. |
||
| 11 | * |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | const EMOJI_MAP_FILE = '/../Storage/emoji.json'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * The array mapping words to emoji. |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected $emojiMap; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The array mapping emoji back to words. |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $wordMap; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Emojify constructor. |
||
| 32 | * |
||
| 33 | * @param null|string $emojiMapFile |
||
| 34 | * |
||
| 35 | * @throws TelegramEmojiMapFileNotFoundException |
||
| 36 | */ |
||
| 37 | 76 | public function __construct($emojiMapFile = null) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Translate Word to Emoji |
||
| 54 | * |
||
| 55 | * @param $text |
||
| 56 | * |
||
| 57 | * @return mixed |
||
| 58 | */ |
||
| 59 | 6 | public function toEmoji($text) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Alias of toEmoji() |
||
| 66 | * |
||
| 67 | * @param $text |
||
| 68 | * |
||
| 69 | * @return mixed |
||
| 70 | */ |
||
| 71 | public static function text($text) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Translate Emoji to Word |
||
| 78 | * |
||
| 79 | * @param $text |
||
| 80 | * |
||
| 81 | * @return mixed |
||
| 82 | */ |
||
| 83 | 10 | public function toWord($text) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Alias of toWord() |
||
| 90 | * |
||
| 91 | * @param $text |
||
| 92 | * |
||
| 93 | * @return mixed |
||
| 94 | */ |
||
| 95 | 8 | public static function translate($text) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Replace |
||
| 102 | * |
||
| 103 | * @param $line |
||
| 104 | * @param $replace |
||
| 105 | * @param bool $toWord |
||
| 106 | * @param string $delimiter |
||
| 107 | * |
||
| 108 | * @return mixed |
||
| 109 | */ |
||
| 110 | protected function replace($line, $replace, $toWord = false, $delimiter = ':') |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Finds words enclosed by the delimiter and converts them to the |
||
| 126 | * appropriate emoji character. |
||
| 127 | * |
||
| 128 | * @param $line |
||
| 129 | * @param $replace |
||
| 130 | * @param $delimiter |
||
| 131 | * |
||
| 132 | * @return mixed |
||
| 133 | */ |
||
| 134 | 6 | View Code Duplication | protected function wordToEmojiReplace($line, $replace, $delimiter) |
| 142 | |||
| 143 | /** |
||
| 144 | * Finds emojis and replaces them with text enclosed by the delimiter |
||
| 145 | * |
||
| 146 | * @param $line |
||
| 147 | * @param $replace |
||
| 148 | * @param $delimiter |
||
| 149 | * |
||
| 150 | * @return mixed |
||
| 151 | */ |
||
| 152 | 10 | View Code Duplication | protected function emojiToWordReplace($line, $replace, $delimiter) |
| 160 | } |
||
| 161 |
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.