Test Setup Failed
Pull Request — latest (#3)
by Mark
33:50
created

EmojiCoreProcessor   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 18
eloc 42
c 1
b 0
f 1
dl 0
loc 86
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C __invoke() 0 69 17
A setConfiguration() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UnicornFail\Emoji\Extension;
6
7
use League\Configuration\ConfigurationAwareInterface;
8
use League\Configuration\ConfigurationInterface;
9
use UnicornFail\Emoji\EmojiConverterInterface;
10
use UnicornFail\Emoji\Emojibase\EmojibaseDatasetInterface;
11
use UnicornFail\Emoji\Event\DocumentParsedEvent;
12
use UnicornFail\Emoji\Node\Emoji;
13
use UnicornFail\Emoji\Parser\Lexer;
14
15
/**
16
 * Processes all parsed Emoji nodes and set the various configurations.
17
 */
18
final class EmojiCoreProcessor implements ConfigurationAwareInterface
19
{
20
    /**
21
     * @var ConfigurationInterface
22
     *
23
     * @psalm-readonly-allow-private-mutation
24
     * @psalm-suppress PropertyNotSetInConstructor
25
     */
26
    private $config;
27
28
    public function __invoke(DocumentParsedEvent $e): void
29
    {
30
        /** @var string[] $excludedShortcodes */
31
        $excludedShortcodes = $this->config->get('exclude/shortcodes');
32
33
        /** @var ?int $presentation */
34
        $presentation = $this->config->get('presentation');
35
36
        // Ensure emojis are set to the correct stringable type.
37
        $walker = $e->getDocument()->walker();
38
        while ($event = $walker->next()) {
39
            if (! $event->isEntering()) {
40
                continue;
41
            }
42
43
            $node = $event->getNode();
44
            if (! ($node instanceof Emoji)) {
45
                continue;
46
            }
47
48
            $literal    = null;
49
            $type       = $node->getParsedType();
50
            $configPath = 'convert.' . (EmojiConverterInterface::TYPES[$type] ?? '');
51
52
            /** @var ?int $conversionType */
53
            $conversionType = null;
54
55
            if ($this->config->exists($configPath) && ($configType = (string) ($this->config->get($configPath) ?? ''))) {
56
                $index = \array_search($configType, EmojiConverterInterface::TYPES, true);
57
                if (\is_int($index)) {
58
                    $conversionType = $index;
59
                }
60
            }
61
62
            // If the conversion type isn't one of the core Lexer:TYPES, then do nothing.
63
            // It should be handled by a different extension/processor.
64
            if ($conversionType === null) {
65
                continue;
66
            }
67
68
            switch ($conversionType) {
69
                case Lexer::T_EMOTICON:
70
                    $literal = $node->emoticon;
71
                    break;
72
73
                case Lexer::T_HTML_ENTITY:
74
                    $literal = $node->htmlEntity;
75
                    break;
76
77
                case Lexer::T_SHORTCODE:
78
                    if ($shortcode = $node->getShortcode($excludedShortcodes, true)) {
79
                        $literal = $shortcode;
80
                    }
81
82
                    break;
83
84
                case Lexer::T_TEXT:
85
                case Lexer::T_UNICODE:
86
                    if (($presentation ?? $node->type) === EmojibaseDatasetInterface::TEXT && $node->text) {
87
                        $literal = $node->text;
88
                    } else {
89
                        $literal = $node->emoji ?? $node->unicode;
90
                    }
91
92
                    break;
93
            }
94
95
            if ($literal !== null) {
96
                $node->setLiteral($literal);
97
            }
98
        }
99
    }
100
101
    public function setConfiguration(ConfigurationInterface $configuration): void
102
    {
103
        $this->config = $configuration;
104
    }
105
}
106