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

dg / texy / 21344532034

26 Jan 2026 02:43AM UTC coverage: 91.98% (-0.4%) from 92.376%
21344532034

push

github

dg
added CLAUDE.md

2397 of 2606 relevant lines covered (91.98%)

0.92 hits per line

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

88.37
/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
 * Replaces emoticons with images or Unicode characters.
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
         * @param  string[]  $matches
79
         */
80
        public function pattern(Texy\LineParser $parser, array $matches): Texy\HtmlElement|string|null
1✔
81
        {
82
                $match = $matches[0];
1✔
83

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

91
                return null;
×
92
        }
93

94

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

106
                $el = new Texy\HtmlElement('img');
1✔
107
                $el->attrs['src'] = Texy\Helpers::prependRoot($file, $this->root ?? $texy->imageModule->root);
1✔
108
                $el->attrs['alt'] = $raw;
1✔
109
                if ($this->class !== null) {
1✔
110
                        $el->attrs['class'] = (array) ($el->attrs['class'] ?? []);
1✔
111
                        $el->attrs['class'][] = $this->class;
1✔
112
                }
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];
×
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