• 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

88.24
/src/Texy/Regexp.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;
11

12
use JetBrains\PhpStorm\Language;
13

14

15
class Regexp
16
{
17
        /**
18
         * Divides the string into arrays according to the regular expression. Expressions in parentheses will be captured and returned as well.
19
         */
20
        public static function split(
1✔
21
                string $subject,
22
                #[Language('RegExp')]
23
                string $pattern,
24
                bool $captureOffset = false,
25
                bool $skipEmpty = false,
26
                int $limit = -1,
27
        ): array
28
        {
29
                $flags = ($captureOffset ? PREG_SPLIT_OFFSET_CAPTURE : 0) | ($skipEmpty ? PREG_SPLIT_NO_EMPTY : 0);
1✔
30
                return self::pcre('preg_split', [$pattern . 'ux', $subject, $limit, $flags | PREG_SPLIT_DELIM_CAPTURE]);
1✔
31
        }
32

33

34
        /**
35
         * Searches the string for the part matching the regular expression and returns
36
         * an array with the found expression and individual subexpressions, or `null`.
37
         */
38
        public static function match(
1✔
39
                string $subject,
40
                #[Language('RegExp')]
41
                string $pattern,
42
                bool $captureOffset = false,
43
                int $offset = 0,
44
        ): ?array
45
        {
46
                $flags = ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | PREG_UNMATCHED_AS_NULL;
1✔
47
                if ($offset > strlen($subject)) {
1✔
48
                        return null;
×
49
                } elseif (!self::pcre('preg_match', [$pattern . 'ux', $subject, &$m, $flags, $offset])) {
1✔
50
                        return null;
1✔
51
                } else {
52
                        return $m;
1✔
53
                }
54
        }
55

56

57
        /**
58
         * Searches the string for all occurrences matching the regular expression and
59
         * returns an array of arrays containing the found expression and each subexpression.
60
         * @return array[]
61
         */
62
        public static function matchAll(
1✔
63
                string $subject,
64
                #[Language('RegExp')]
65
                string $pattern,
66
                bool $captureOffset = false,
67
                int $offset = 0,
68
        ): array
69
        {
70
                if ($offset > strlen($subject)) {
1✔
71
                        return [];
×
72
                }
73
                $flags = ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | PREG_UNMATCHED_AS_NULL | PREG_SET_ORDER;
1✔
74
                self::pcre('preg_match_all', [$pattern . 'ux', $subject, &$m, $flags, $offset]);
1✔
75
                return $m;
1✔
76
        }
77

78

79
        /**
80
         * Replaces all occurrences matching regular expression $pattern which can be string or array in the form `pattern => replacement`.
81
         */
82
        public static function replace(
1✔
83
                string $subject,
84
                #[Language('RegExp')]
85
                string|array $pattern,
86
                string|callable $replacement = '',
87
                int $limit = -1,
88
                bool $captureOffset = false,
89
        ): string
90
        {
91
                if (is_object($replacement) || is_array($replacement)) {
1✔
92
                        if (!is_callable($replacement, false, $textual)) {
1✔
93
                                throw new \InvalidStateException("Callback '$textual' is not callable.");
×
94
                        }
95

96
                        $flags = ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | PREG_UNMATCHED_AS_NULL;
1✔
97
                        return self::pcre('preg_replace_callback', [$pattern . 'ux', $replacement, $subject, $limit, 0, $flags]);
1✔
98

99
                } elseif (is_array($pattern) && is_string(key($pattern))) {
1✔
100
                        $patterns = array_map(static fn($p) => $p . 'ux', array_keys($pattern));
1✔
101
                        return self::pcre('preg_replace', [$patterns, array_values($pattern), $subject, $limit]);
1✔
102

103
                } else {
104
                        return self::pcre('preg_replace', [$pattern . 'ux', $replacement, $subject, $limit]);
1✔
105
                }
106
        }
107

108

109
        public static function quote(string $s): string
1✔
110
        {
111
                return addcslashes($s, "\x00..\x20-.\\+*?[^]$(){}=!<>|:-#");
1✔
112
        }
113

114

115
        /** @internal */
116
        public static function pcre(string $func, array $args)
1✔
117
        {
118
                $res = @$func(...$args);
1✔
119
                if (($code = preg_last_error()) // run-time error, but preg_last_error & return code are liars
1✔
120
                        && ($res === null || !in_array($func, ['preg_replace_callback', 'preg_replace'], true))
1✔
121
                ) {
UNCOV
122
                        throw new RegexpException(preg_last_error_msg() . ' (pattern: ' . implode(' or ', (array) $args[0]) . ')', $code);
×
123
                }
124

125
                return $res;
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