Test Failed
Push — docs ( 541d16...38ea2c )
by Mark
35:24
created

EmojiCoreProcessor::__invoke()   C

Complexity

Conditions 16
Paths 59

Size

Total Lines 63
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 35
nc 59
nop 1
dl 0
loc 63
rs 5.5666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
3
declare(strict_types=1);
4
5
namespace League\Emoji\Extension;
6
7
use League\Configuration\ConfigurationAwareInterface;
8
use League\Configuration\ConfigurationInterface;
9
use League\Emoji\EmojiConverterInterface;
10
use League\Emoji\Emojibase\EmojibaseDatasetInterface;
11
use League\Emoji\Event\DocumentParsedEvent;
12
use League\Emoji\Lexer\EmojiLexer;
13
use League\Emoji\Node\Emoji;
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
        foreach ($e->getDocument()->getNodes() as $node) {
38
            if (! ($node instanceof Emoji)) {
39
                continue;
40
            }
41
42
            $literal    = null;
43
            $type       = $node->getParsedType();
44
            $configPath = 'convert.' . (EmojiConverterInterface::TYPES[$type] ?? '');
45
46
            /** @var ?int $conversionType */
47
            $conversionType = null;
48
49
            if ($this->config->exists($configPath) && ($configType = (string) ($this->config->get($configPath) ?? ''))) {
50
                $index = \array_search($configType, EmojiConverterInterface::TYPES, true);
51
                if (\is_int($index)) {
52
                    $conversionType = $index;
53
                }
54
            }
55
56
            // If the conversion type isn't one of the core Lexer:TYPES, then do nothing.
57
            // It should be handled by a different extension/processor.
58
            if ($conversionType === null) {
59
                continue;
60
            }
61
62
            switch ($conversionType) {
63
                case EmojiLexer::T_EMOTICON:
64
                    $literal = $node->emoticon;
65
                    break;
66
67
                case EmojiLexer::T_HTML_ENTITY:
68
                    $literal = $node->htmlEntity;
69
                    break;
70
71
                case EmojiLexer::T_SHORTCODE:
72
                    if ($shortcode = $node->getShortcode($excludedShortcodes, true)) {
73
                        $literal = $shortcode;
74
                    }
75
76
                    break;
77
78
                case EmojiLexer::T_TEXT:
79
                case EmojiLexer::T_UNICODE:
80
                    if (($presentation ?? $node->type) === EmojibaseDatasetInterface::TEXT && $node->text) {
81
                        $literal = $node->text;
82
                    } else {
83
                        $literal = $node->emoji ?? $node->unicode;
84
                    }
85
86
                    break;
87
            }
88
89
            if ($literal !== null) {
90
                $node->setContent($literal);
91
            }
92
        }
93
    }
94
95
    public function setConfiguration(ConfigurationInterface $configuration): void
96
    {
97
        $this->config = $configuration;
98
    }
99
}
100