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

dg / texy / 12879605443

21 Jan 2025 03:31AM UTC coverage: 92.224% (+0.03%) from 92.197%
12879605443

push

github

dg
regexp: uses unmatched as null (BC break)

14 of 14 new or added lines in 6 files covered. (100.0%)

101 existing lines in 14 files now uncovered.

2372 of 2572 relevant lines covered (92.22%)

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.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

14

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

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

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

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

43

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

52

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

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

61
                $pattern = [];
1✔
62
                foreach ($this->icons as $key => $foo) {
1✔
63
                        $pattern[] = Texy\Regexp::quote($key) . '+'; // last char can be repeated
1✔
64
                }
65

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

77

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

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

UNCOV
92
                return null;
×
93
        }
94

95

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

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

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

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