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

dg / texy / 16790351274

06 Aug 2025 10:42PM UTC coverage: 92.746% (+0.005%) from 92.741%
16790351274

push

github

dg
HtmlElement: removed toHtml() & toText()

18 of 19 new or added lines in 5 files covered. (94.74%)

136 existing lines in 23 files now uncovered.

2391 of 2578 relevant lines covered (92.75%)

0.93 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.info)
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, strlen, strncmp;
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->toElement(...));
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[] = Texy\Regexp::quote($key) . '+'; // last char can be repeated
1✔
65
                }
66

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

78

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

86
                // find the closest match
87
                foreach ($this->icons as $emoticon => $foo) {
1✔
88
                        if (strncmp($match, $emoticon, strlen($emoticon)) === 0) {
1✔
89
                                return $this->texy->invokeAroundHandlers('emoticon', $parser, [$emoticon, $match]);
1✔
90
                        }
91
                }
92

UNCOV
93
                return null;
×
94
        }
95

96

97
        public function toElement(
1✔
98
                Texy\HandlerInvocation $invocation,
99
                string $emoticon,
100
                string $raw,
101
        ): Texy\HtmlElement|string
102
        {
103
                $texy = $this->texy;
1✔
104
                $file = $this->icons[$emoticon];
1✔
105
                if (!str_contains($file, '.')) {
1✔
106
                        return $file;
1✔
107
                }
108

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

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

124
                $texy->summary['images'][] = $el->attrs['src'];
1✔
125
                return $el;
1✔
126
        }
127
}
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