Completed
Push — master ( dedd46...e02d88 )
by Irfaq
02:31
created

Emojify   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 154
Duplicated Lines 10.39 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.12%

Importance

Changes 5
Bugs 2 Features 2
Metric Value
wmc 13
c 5
b 2
f 2
lcom 1
cbo 1
dl 16
loc 154
ccs 32
cts 34
cp 0.9412
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A toEmoji() 0 4 1
A text() 0 4 1
A toWord() 0 4 1
A translate() 0 4 1
A replace() 0 13 2
A wordToEmojiReplace() 8 8 2
A emojiToWordReplace() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Telegram\Bot\Helpers;
4
5
use Telegram\Bot\Exceptions\TelegramEmojiMapFileNotFoundException;
6
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)
38
    {
39 76
        if (empty($emojiMapFile)) {
40 76
            $emojiMapFile = __DIR__.self::EMOJI_MAP_FILE;
41 76
        }
42
43 76
        if (!file_exists($emojiMapFile)) {
44 2
            throw new TelegramEmojiMapFileNotFoundException();
45
        }
46
47 76
        $emojiMapFileContents = file_get_contents($emojiMapFile);
48 76
        $this->emojiMap = (array)json_decode($emojiMapFileContents);
49 76
        $this->wordMap = array_flip($this->emojiMap);
50 76
    }
51
52
    /**
53
     * Translate Word to Emoji
54
     *
55
     * @param $text
56
     *
57
     * @return mixed
58
     */
59 6
    public function toEmoji($text)
60
    {
61 6
        return $this->replace($text, $this->emojiMap);
62
    }
63
64
    /**
65
     * Alias of toEmoji()
66
     *
67
     * @param $text
68
     *
69
     * @return mixed
70
     */
71
    public static function text($text)
72
    {
73
        return (new static)->toEmoji($text);
74
    }
75
76
    /**
77
     * Translate Emoji to Word
78
     *
79
     * @param $text
80
     *
81
     * @return mixed
82
     */
83 10
    public function toWord($text)
84
    {
85 10
        return $this->replace($text, $this->wordMap, true);
86
    }
87
88
    /**
89
     * Alias of toWord()
90
     *
91
     * @param $text
92
     *
93
     * @return mixed
94
     */
95 8
    public static function translate($text)
96
    {
97 8
        return (new static)->toWord($text);
98
    }
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 = ':')
111
    {
112 14
        uksort($replace, function ($a, $b) {
113 14
            return strlen($b) - strlen($a);
114 14
        });
115
116
117 14
        if ($toWord) {
118 10
            return $this->emojiToWordReplace($line, $replace, $delimiter);
119
        }
120
121 6
        return $this->wordToEmojiReplace($line, $replace, $delimiter);
122
    }
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
135
    {
136 6
        foreach ($replace as $key => $value) {
137 6
            $line = str_replace($delimiter.$key.$delimiter, $value, $line);
138 6
        }
139
140 6
        return $line;
141
    }
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
153
    {
154 10
        foreach ($replace as $key => $value) {
155 10
            $line = str_replace($key, $delimiter.$value.$delimiter, $line);
156 10
        }
157
158 10
        return $line;
159
    }
160
}
161