Emoticon   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 73
ccs 22
cts 22
cp 1
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get() 0 8 2
A random() 0 3 1
A search() 0 5 1
A emojify() 0 7 2
A stripColons() 0 9 2
1
<?php
2
3
namespace MarufMax\Emoticon;
4
5
class Emoticon
6
{
7
    private $list;
8
9 8
    public function __construct()
10
    {
11 8
        $this->list = require 'IconList.php';
12 8
    }
13
14
    /**
15
     * Get a single emoji.
16
     *
17
     * @param string $name
18
     *
19
     * @return bool|mixed
20
     */
21 6
    public function get(string $name)
22
    {
23 6
        $itemName = $this->stripColons($name);
24 6
        if (array_key_exists($itemName, $this->list)) {
25 4
            return $this->list[$itemName];
26
        }
27
28 3
        return false;
29
    }
30
31
    /**
32
     * Replace all :emoji: with the actual emoji.
33
     *
34
     * @param string $text
35
     *
36
     * @return string
37
     */
38
    public function emojify(string $text): string
39
    {
40 4
        return preg_replace_callback('/:([a-zA-Z0-9_\-+]+):/', function ($match) {
41 4
            $emoji = $this->get($match[1]);
42
43 4
            return false !== $emoji ? $emoji : $match[0];
44 4
        }, $text);
45
    }
46
47
    /**
48
     * Returns array of items with matching emoji.
49
     *
50
     * @param string $emoji
51
     *
52
     * @return array
53
     */
54
    public function search(string $emoji): array
55
    {
56 1
        return array_filter($this->list, function ($item) use ($emoji) {
57 1
            return strpos($item, $emoji) !== false;
58 1
        }, ARRAY_FILTER_USE_KEY);
59
    }
60
61
    /**
62
     * Generate a random emoji.
63
     */
64 1
    public function random()
65
    {
66 1
        return $this->list[array_rand($this->list)];
67
    }
68
69 6
    protected function stripColons(string $item): string
70
    {
71 6
        $colonPosition = strpos($item, ':');
72
73 6
        if ($colonPosition > -1) {
74 1
            return str_replace(':', '', $item);
75
        }
76
77 5
        return $item;
78
    }
79
}
80