• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dg / texy / 22262381750

25 Jan 2026 11:44PM UTC coverage: 92.367% (-0.7%) from 93.057%
22262381750

push

github

dg
cs

9 of 11 new or added lines in 8 files covered. (81.82%)

161 existing lines in 23 files now uncovered.

2384 of 2581 relevant lines covered (92.37%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.8
/src/Texy/Modules/EmoticonModule.php
1
<?php
2

3
/**
4
 * This file is part of the Texy! (https://texy.nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Texy\Modules;
11

12
use Texy;
13
use function getimagesize, implode, is_array, is_file, krsort, preg_quote, rtrim, str_contains;
14

15

16
/**
17
 * Emoticon module.
18
 */
19
final class EmoticonModule extends Texy\Module
20
{
21
        /** @var array<string, string>  supported emoticons and image files / chars */
22
        public array $icons = [
23
                ':-)' => '🙂',
24
                ':-(' => '☹',
25
                ';-)' => '😉',
26
                ':-D' => '😁',
27
                '8-O' => '😮',
28
                '8-)' => '😄',
29
                ':-?' => '😕',
30
                ':-x' => '😶',
31
                ':-P' => '😛',
32
                ':-|' => '😐',
33
        ];
34

35
        /** @deprecated */
36
        public ?string $class = null;
37

38
        /** @deprecated */
39
        public ?string $root = null;
40

41
        /** @deprecated */
42
        public ?string $fileRoot = null;
43

44

45
        public function __construct(Texy\Texy $texy)
1✔
46
        {
47
                $this->texy = $texy;
1✔
48
                $texy->allowed['emoticon'] = false;
1✔
49
                $texy->addHandler('emoticon', $this->solve(...));
1✔
50
                $texy->addHandler('beforeParse', $this->beforeParse(...));
1✔
51
        }
1✔
52

53

54
        private function beforeParse(): void
55
        {
56
                if (empty($this->texy->allowed['emoticon'])) {
1✔
57
                        return;
1✔
58
                }
59

60
                krsort($this->icons);
1✔
61

62
                $pattern = [];
1✔
63
                foreach ($this->icons as $key => $foo) {
1✔
64
                        $pattern[] = preg_quote($key, '#') . '+'; // last char can be repeated
1✔
65
                }
66

67
                $this->texy->registerLinePattern(
1✔
68
                        $this->pattern(...),
1✔
69
                        '#(?<=^|[\x00-\x20])(' . implode('|', $pattern) . ')#',
1✔
70
                        'emoticon',
1✔
71
                        '#' . implode('|', $pattern) . '#',
1✔
72
                );
73
        }
1✔
74

75

76
        /**
77
         * Callback for: :-))).
78
         */
79
        public function pattern(Texy\LineParser $parser, array $matches): Texy\HtmlElement|string|null
1✔
80
        {
81
                $match = $matches[0];
1✔
82

83
                // find the closest match
84
                foreach ($this->icons as $emoticon => $foo) {
1✔
85
                        if (str_starts_with($match, $emoticon)) {
1✔
86
                                return $this->texy->invokeAroundHandlers('emoticon', $parser, [$emoticon, $match]);
1✔
87
                        }
88
                }
89

UNCOV
90
                return null;
×
91
        }
92

93

94
        /**
95
         * Finish invocation.
96
         */
97
        private function solve(Texy\HandlerInvocation $invocation, string $emoticon, string $raw): Texy\HtmlElement|string
1✔
98
        {
99
                $texy = $this->texy;
1✔
100
                $file = $this->icons[$emoticon];
1✔
101
                if (!str_contains($file, '.')) {
1✔
102
                        return $file;
1✔
103
                }
104

105
                $el = new Texy\HtmlElement('img');
1✔
106
                $el->attrs['src'] = Texy\Helpers::prependRoot($file, $this->root ?? $texy->imageModule->root);
1✔
107
                $el->attrs['alt'] = $raw;
1✔
108
                $el->attrs['class'][] = $this->class;
1✔
109

110
                // file path
111
                $file = rtrim($this->fileRoot ?? (string) $texy->imageModule->fileRoot, '/\\') . '/' . $file;
1✔
112
                if (@is_file($file)) { // intentionally @
1✔
UNCOV
113
                        $size = @getimagesize($file); // intentionally @
×
UNCOV
114
                        if (is_array($size)) {
×
UNCOV
115
                                $el->attrs['width'] = $size[0];
×
UNCOV
116
                                $el->attrs['height'] = $size[1];
×
117
                        }
118
                }
119

120
                $texy->summary['images'][] = $el->attrs['src'];
1✔
121
                return $el;
1✔
122
        }
123
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc