Test Setup Failed
Pull Request — latest (#3)
by Mark
34:22
created

Lexer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 44
c 0
b 0
f 0
dl 0
loc 109
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getNonCatchablePatterns() 0 3 1
A getType() 0 20 6
B getCatchablePatterns() 0 31 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UnicornFail\Emoji\Parser;
6
7
use Doctrine\Common\Lexer\AbstractLexer;
8
use UnicornFail\Emoji\Emojibase\EmojibaseRegexInterface;
9
use UnicornFail\Emoji\Environment\EnvironmentInterface;
10
11
class Lexer extends AbstractLexer implements EmojibaseRegexInterface
12
{
13
    public const T_TEXT = 0;
14
15
    public const T_EMOTICON = 1;
16
17
    public const T_HTML_ENTITY = 2;
18
19
    public const T_SHORTCODE = 3;
20
21
    public const T_UNICODE = 4;
22
23
    public const TEXT        = 'text';
24
    public const EMOTICON    = 'emoticon';
25
    public const HTML_ENTITY = 'html_entity';
26
    public const SHORTCODE   = 'shortcode';
27
    public const UNICODE     = 'unicode';
28
29
    public const TYPES = [
30
        self::T_TEXT => self::TEXT,
31
        self::T_EMOTICON => self::EMOTICON,
32
        self::T_HTML_ENTITY => self::HTML_ENTITY,
33
        self::T_SHORTCODE => self::SHORTCODE,
34
        self::T_UNICODE => self::UNICODE,
35
    ];
36
37
    /** @var EnvironmentInterface */
38
    private $environment;
39
40
    public function __construct(EnvironmentInterface $environment)
41
    {
42
        $this->environment = $environment;
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     *
48
     * @return string[]
49
     */
50
    protected function getCatchablePatterns()
51
    {
52
        $config = $this->environment->getConfiguration();
53
54
        $patterns = [];
55
56
        if ($config->get('convert.emoticons')) {
57
            $patterns[] = self::EMOTICON_REGEX;
58
        }
59
60
        if ($config->get('convert.html_entities')) {
61
            $patterns[] = self::HTML_ENTITY_REGEX;
62
        }
63
64
        if ($config->get('convert.shortcodes')) {
65
            $patterns[] = $this->environment->getRuntimeDataset()->isNative()
66
                ? self::SHORTCODE_NATIVE_REGEX
67
                : self::SHORTCODE_REGEX;
68
        }
69
70
        if ($config->get('convert.unicodes')) {
71
            $patterns[] = self::CODEPOINT_EMOJI_LOOSE_REGEX;
72
        }
73
74
        // Some regex patterns from the constants include the delimiter and modifiers. Because the
75
        // lexer joins these expressions together as an OR group (|), they must be removed.
76
        foreach ($patterns as &$pattern) {
77
            $pattern = \trim(\rtrim($pattern, 'imsxeADSUXJu'), '/');
78
        }
79
80
        return $patterns;
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     *
86
     * @return string[]
87
     */
88
    protected function getNonCatchablePatterns()
89
    {
90
        return [];
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     *
96
     * @return int
97
     *
98
     * @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection
99
     */
100
    protected function getType(&$value)
101
    {
102
        // @phpstan-ignore-next-line
103
        if (\preg_match($this->environment->getRuntimeDataset()->isNative() ? self::SHORTCODE_NATIVE_REGEX : self::SHORTCODE_REGEX, $value)) {
104
            return self::T_SHORTCODE;
105
        }
106
107
        if (\preg_match(self::EMOTICON_REGEX, $value)) {
108
            return self::T_EMOTICON;
109
        }
110
111
        if (\preg_match(self::CODEPOINT_EMOJI_LOOSE_REGEX, $value)) {
112
            return self::T_UNICODE;
113
        }
114
115
        if (\preg_match(self::HTML_ENTITY_REGEX, $value)) {
116
            return self::T_HTML_ENTITY;
117
        }
118
119
        return self::T_TEXT;
120
    }
121
}
122