Completed
Pull Request — master (#34)
by Fèvre
05:12 queued 02:29
created

EmojiParser::parse()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 6.9743
c 0
b 0
f 0
cc 7
eloc 29
nc 5
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Xetaravel\Markdown\Emoji;
3
4
use League\CommonMark\Inline\Element\Image;
5
use League\CommonMark\Inline\Parser\AbstractInlineParser;
6
use League\CommonMark\InlineParserContext;
7
8
class EmojiParser extends AbstractInlineParser
9
{
10
    /**
11
     * The emoji mappings.
12
     *
13
     * @var array
14
     */
15
    protected $map;
16
17
    /**
18
     * The emoji extension.
19
     *
20
     * @var string
21
     */
22
    protected $ext = '.png';
23
24
    /**
25
     * The emoji path directory.
26
     *
27
     * @var string
28
     */
29
    protected $path;
30
31
    /**
32
     * Create a new emoji parser instance.
33
     *
34
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
35
     */
36
    public function __construct()
37
    {
38
        $this->map = include __DIR__ . "/EmojiSheet.php";
39
        $this->path = config('app.url') . '/images/emojis/';
40
    }
41
42
    /**
43
     * Get the characters that must be matched.
44
     *
45
     * @return array
46
     */
47
    public function getCharacters()
48
    {
49
        return [':'];
50
    }
51
52
    /**
53
     * Parse a line and determine if it contains an emoji. If it does,
54
     * then we do the necessary.
55
     *
56
     * @param \League\CommonMark\InlineParserContext $inlineContext
57
     *
58
     * @return bool
59
     */
60
    public function parse(InlineParserContext $inlineContext)
61
    {
62
        $cursor = $inlineContext->getCursor();
63
64
        $previous = $cursor->peek(-1);
65
        if ($previous !== null && $previous !== ' ') {
66
            return false;
67
        }
68
69
        $saved = $cursor->saveState();
70
71
        $cursor->advance();
72
73
        $handle = $cursor->match('/^[a-z0-9\+\-_]+:/');
74
75
        if (!$handle) {
76
            $cursor->restoreState($saved);
77
78
            return false;
79
        }
80
81
        $next = $cursor->peek(0);
82
83
        if ($next !== null && $next !== ' ') {
84
            $cursor->restoreState($saved);
85
86
            return false;
87
        }
88
89
        $key = substr($handle, 0, -1);
90
91
        if (!in_array($key, $this->map)) {
92
            $cursor->restoreState($saved);
93
94
            return false;
95
        }
96
97
        $fileName = $this->path . $key . $this->ext;
98
99
        $inline = new Image($fileName, $key);
100
        $inline->data['attributes'] = [
101
            'class' => 'emoji',
102
            'data-emoji' => $key,
103
            'width' => '24',
104
            'height' => '24',
105
            'title' => ':' . $key . ':'
106
        ];
107
        $inlineContext->getContainer()->appendChild($inline);
108
109
        return true;
110
    }
111
}
112