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

TwemojiProcessor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 29
c 1
b 0
f 1
dl 0
loc 61
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 47 7
A setConfiguration() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UnicornFail\Emoji\Extension\Twemoji;
6
7
use Astrotomic\Twemoji\Twemoji;
8
use League\Configuration\ConfigurationAwareInterface;
9
use League\Configuration\ConfigurationInterface;
10
use UnicornFail\Emoji\Event\DocumentParsedEvent;
11
use UnicornFail\Emoji\Node\Inline\AbstractEmoji;
12
use UnicornFail\Emoji\Node\Inline\EmojiImage;
13
14
/**
15
 * Replaces emojis with Twemoji images.
16
 */
17
final class TwemojiProcessor implements ConfigurationAwareInterface
18
{
19
    /**
20
     * @var ConfigurationInterface
21
     *
22
     * @psalm-readonly-allow-private-mutation
23
     */
24
    private $config;
25
26
    public function setConfiguration(ConfigurationInterface $configuration): void
27
    {
28
        $this->config = $configuration;
29
    }
30
31
    public function __invoke(DocumentParsedEvent $e): void
32
    {
33
        $base = (string) $this->config->get('twemoji.base');
34
35
        /** @var ?int $size */
36
        $size = $this->config->get('twemoji.size');
37
38
        $type = (string) $this->config->get('twemoji.type');
39
40
        $walker = $e->getDocument()->walker();
41
        while ($event = $walker->next()) {
42
            $node = $event->getNode();
43
            if (! ($node instanceof AbstractEmoji)) {
44
                continue;
45
            }
46
47
            $emoji   = $node->getEmoji();
48
            $unicode = $emoji->getUnicode();
49
50
            if (! $unicode) {
51
                continue;
52
            }
53
54
            $twemoji = Twemoji::emoji($unicode)->base($base);
55
56
            if ($type === 'png') {
57
                $twemoji->png();
58
                $size = 72;
59
            }
60
61
            /** @var string[] $classes */
62
            $classes = (array) $this->config->get('twemoji.classes');
63
64
            $shortcode = $emoji->getShortcode();
65
            if ($shortcode !== null) {
66
                $classes[] = \str_replace('_', '-', $shortcode);
67
            }
68
69
            $image = new EmojiImage($node->getParsedValue(), $emoji, $twemoji->url(), $emoji->annotation ?? $shortcode);
70
            $image->addClass(...$classes);
71
72
            if ($size) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $size of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
73
                $image->setAttribute('height', (string) $size);
74
                $image->setAttribute('width', (string) $size);
75
            }
76
77
            $node->replaceWith($image);
78
        }
79
    }
80
}
81