Passed
Push — latest ( 9a2102...03f783 )
by Mark
03:04
created

Lexer::getNonCatchablePatterns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UnicornFail\Emoji;
6
7
use Doctrine\Common\Lexer\AbstractLexer;
8
use UnicornFail\Emoji\Emojibase\RegexInterface;
9
10
class Lexer extends AbstractLexer implements RegexInterface
11
{
12
    public const T_TEXT = 0;
13
14
    public const T_EMOTICON = 1;
15
16
    public const T_HTML_ENTITY = 2;
17
18
    public const T_SHORTCODE = 3;
19
20
    public const T_UNICODE = 4;
21
22
    public const TYPES = [
23
        self::T_TEXT => 'text',
24
        self::T_EMOTICON => 'emoticon',
25
        self::T_HTML_ENTITY => 'htmlEntity',
26
        self::T_SHORTCODE => 'shortcode',
27
        self::T_UNICODE => 'unicode',
28
    ];
29
30
    /** @var Configuration|ConfigurationInterface */
31
    private $configuration;
32
33 192
    public function __construct(ConfigurationInterface $configuration)
34
    {
35 192
        $this->configuration = $configuration;
36 192
    }
37
38
    /**
39
     * {@inheritDoc}
40
     *
41
     * @return string[]
42
     */
43 81
    protected function getCatchablePatterns()
44
    {
45
        $patterns = [
46 81
            self::CODEPOINT_EMOJI_LOOSE_REGEX,
47 81
            self::HTML_ENTITY_REGEX,
48 81
            $this->configuration->get('native') ? self::SHORTCODE_NATIVE_REGEX : self::SHORTCODE_REGEX,
49
        ];
50 81
        if ($this->configuration->get('convertEmoticons')) {
51 81
            $patterns[] = self::EMOTICON_REGEX;
52
        }
53
54
        // Some regex patterns from the constants include the delimiter and modifiers. Because the
55
        // lexer joins these expressions together as an OR group (|), they must be removed.
56 81
        foreach ($patterns as &$pattern) {
57 81
            $pattern = \trim(\rtrim($pattern, 'imsxeADSUXJu'), '/');
58
        }
59
60 81
        return $patterns;
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     *
66
     * @return string[]
67
     */
68 81
    protected function getNonCatchablePatterns()
69
    {
70 81
        return [];
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     *
76
     * @return int
77
     *
78
     * @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection
79
     */
80 81
    protected function getType(&$value)
81
    {
82 81
        if (\preg_match(self::HTML_ENTITY_REGEX, $value)) {
83 15
            return self::T_HTML_ENTITY;
84
        }
85
86
        // @phpstan-ignore-next-line
87 81
        if (\preg_match($this->configuration->get('native') ? self::SHORTCODE_NATIVE_REGEX : self::SHORTCODE_REGEX, $value)) {
88 54
            return self::T_SHORTCODE;
89
        }
90
91 69
        if (\preg_match(self::EMOTICON_REGEX, $value)) {
92 18
            return self::T_EMOTICON;
93
        }
94
95 69
        if (\preg_match(self::CODEPOINT_EMOJI_LOOSE_REGEX, $value)) {
96 42
            return self::T_UNICODE;
97
        }
98
99 66
        return self::T_TEXT;
100
    }
101
}
102