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

keradus / PHP-CS-Fixer / 16018263876

02 Jul 2025 06:58AM UTC coverage: 94.846% (-0.002%) from 94.848%
16018263876

push

github

keradus
debug2

28193 of 29725 relevant lines covered (94.85%)

45.34 hits per line

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

98.33
/src/Preg.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer;
16

17
/**
18
 * This class replaces preg_* functions to better handling UTF8 strings,
19
 * ensuring no matter "u" modifier is present or absent subject will be handled correctly.
20
 *
21
 * @author Kuba Werłos <werlos@gmail.com>
22
 *
23
 * @internal
24
 */
25
final class Preg
26
{
27
    /**
28
     * @param array<array-key, mixed>                               $matches
29
     * @param int-mask<PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL> $flags
30
     *
31
     * @param-out ($flags is PREG_OFFSET_CAPTURE
32
     *     ? array<array-key, array{string, 0|positive-int}|array{'', -1}>
33
     *     : ($flags is PREG_UNMATCHED_AS_NULL
34
     *         ? array<array-key, string|null>
35
     *         : ($flags is int-mask<PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL>&768
36
     *             ? array<array-key, array{string, 0|positive-int}|array{null, -1}>
37
     *             : array<array-key, string>
38
     *         )
39
     *     )
40
     * ) $matches
41
     *
42
     * @throws PregException
43
     */
44
    public static function match(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): bool
45
    {
46
        $result = @preg_match(self::addUtf8Modifier($pattern), $subject, $matches, $flags, $offset);
18✔
47
        if (false !== $result && PREG_NO_ERROR === preg_last_error()) {
18✔
48
            return 1 === $result;
4✔
49
        }
50

51
        $result = @preg_match(self::removeUtf8Modifier($pattern), $subject, $matches, $flags, $offset);
14✔
52
        if (false !== $result && PREG_NO_ERROR === preg_last_error()) {
14✔
53
            return 1 === $result;
7✔
54
        }
55

56
        throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, $pattern);
7✔
57
    }
58

59
    /**
60
     * @param array<array-key, mixed>                                                                   $matches
61
     * @param int-mask<PREG_PATTERN_ORDER, PREG_SET_ORDER, PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL> $flags
62
     *
63
     * @param-out ($flags is PREG_PATTERN_ORDER
64
     *     ? array<list<string>>
65
     *     : ($flags is PREG_SET_ORDER
66
     *         ? list<array<string>>
67
     *         : ($flags is int-mask<PREG_PATTERN_ORDER, PREG_OFFSET_CAPTURE>&(256|257)
68
     *             ? array<list<array{string, int}>>
69
     *             : ($flags is int-mask<PREG_SET_ORDER, PREG_OFFSET_CAPTURE>&258
70
     *                 ? list<array<array{string, int}>>
71
     *                 : ($flags is int-mask<PREG_PATTERN_ORDER, PREG_UNMATCHED_AS_NULL>&(512|513)
72
     *                     ? array<list<?string>>
73
     *                     : ($flags is int-mask<PREG_SET_ORDER, PREG_UNMATCHED_AS_NULL>&514
74
     *                         ? list<array<?string>>
75
     *                         : ($flags is int-mask<PREG_SET_ORDER, PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL>&770
76
     *                             ? list<array<array{?string, int}>>
77
     *                             : ($flags is 0 ? array<list<string>> : array<mixed>)
78
     *                         )
79
     *                     )
80
     *                 )
81
     *             )
82
     *         )
83
     *     )
84
     * ) $matches
85
     *
86
     * @throws PregException
87
     */
88
    public static function matchAll(string $pattern, string $subject, ?array &$matches = null, int $flags = PREG_PATTERN_ORDER, int $offset = 0): int
89
    {
90
        $result = @preg_match_all(self::addUtf8Modifier($pattern), $subject, $matches, $flags, $offset);
6✔
91
        if (false !== $result && PREG_NO_ERROR === preg_last_error()) {
6✔
92
            return $result;
3✔
93
        }
94

95
        $result = @preg_match_all(self::removeUtf8Modifier($pattern), $subject, $matches, $flags, $offset);
3✔
96
        if (false !== $result && PREG_NO_ERROR === preg_last_error()) {
3✔
97
            return $result;
2✔
98
        }
99

100
        throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, $pattern);
1✔
101
    }
102

103
    /**
104
     * @param-out int $count
105
     *
106
     * @return ($subject is non-empty-string ? ($replacement is non-empty-string ? non-empty-string : string) : string)
107
     *
108
     * @throws PregException
109
     */
110
    public static function replace(string $pattern, string $replacement, string $subject, int $limit = -1, ?int &$count = null): string
111
    {
112
        $result = @preg_replace(self::addUtf8Modifier($pattern), $replacement, $subject, $limit, $count);
16✔
113
        if (null !== $result && PREG_NO_ERROR === preg_last_error()) {
16✔
114
            return $result;
3✔
115
        }
116

117
        $result = @preg_replace(self::removeUtf8Modifier($pattern), $replacement, $subject, $limit, $count);
13✔
118
        if (null !== $result && PREG_NO_ERROR === preg_last_error()) {
13✔
119
            return $result;
6✔
120
        }
121

122
        throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, $pattern);
7✔
123
    }
124

125
    /**
126
     * @param-out int $count
127
     *
128
     * @throws PregException
129
     */
130
    public static function replaceCallback(string $pattern, callable $callback, string $subject, int $limit = -1, ?int &$count = null): string
131
    {
132
        $result = @preg_replace_callback(self::addUtf8Modifier($pattern), $callback, $subject, $limit, $count);
6✔
133
        if (null !== $result && PREG_NO_ERROR === preg_last_error()) {
6✔
134
            return $result;
3✔
135
        }
136

137
        $result = @preg_replace_callback(self::removeUtf8Modifier($pattern), $callback, $subject, $limit, $count);
3✔
138
        if (null !== $result && PREG_NO_ERROR === preg_last_error()) {
3✔
139
            return $result;
2✔
140
        }
141

142
        throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, $pattern);
1✔
143
    }
144

145
    /**
146
     * @return ($flags is PREG_SPLIT_OFFSET_CAPTURE ? list<array{string, int<0, max>}> : list<string>)
147
     *
148
     * @throws PregException
149
     */
150
    public static function split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array
151
    {
152
        $result = @preg_split(self::addUtf8Modifier($pattern), $subject, $limit, $flags);
6✔
153
        if (false !== $result && PREG_NO_ERROR === preg_last_error()) {
6✔
154
            return $result;
3✔
155
        }
156

157
        $result = @preg_split(self::removeUtf8Modifier($pattern), $subject, $limit, $flags);
3✔
158
        if (false !== $result && PREG_NO_ERROR === preg_last_error()) {
3✔
159
            return $result;
2✔
160
        }
161

162
        throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, $pattern);
1✔
163
    }
164

165
    private static function addUtf8Modifier(string $pattern): string
166
    {
167
        return $pattern.'u';
52✔
168
    }
169

170
    private static function removeUtf8Modifier(string $pattern): string
171
    {
172
        if ('' === $pattern) {
36✔
173
            return '';
7✔
174
        }
175

176
        $delimiter = $pattern[0];
29✔
177

178
        $endDelimiterPosition = strrpos($pattern, $delimiter);
29✔
179
        \assert(\is_int($endDelimiterPosition));
29✔
180

181
        return substr($pattern, 0, $endDelimiterPosition).str_replace('u', '', substr($pattern, $endDelimiterPosition));
29✔
182
    }
183

184
    /**
185
     * Create the generic PregException message and tell more about such kind of error in the message.
186
     */
187
    private static function newPregException(int $error, string $errorMsg, string $method, string $pattern): PregException
188
    {
189
        $result = null;
17✔
190
        $errorMessage = null;
17✔
191

192
        try {
193
            $result = ExecutorWithoutErrorHandler::execute(static fn () => preg_match($pattern, ''));
17✔
194
        } catch (ExecutorWithoutErrorHandlerException $e) {
17✔
195
            $result = false;
17✔
196
            $errorMessage = $e->getMessage();
17✔
197
        }
198

199
        if (false !== $result) {
17✔
200
            return new PregException(\sprintf('Unknown error occurred when calling %s: %s.', $method, $errorMsg), $error);
×
201
        }
202

203
        $code = preg_last_error();
17✔
204

205
        $message = \sprintf(
17✔
206
            '(code: %d) %s',
17✔
207
            $code,
17✔
208
            preg_replace('~preg_[a-z_]+[()]{2}: ~', '', $errorMessage)
17✔
209
        );
17✔
210

211
        return new PregException(
17✔
212
            \sprintf('%s(): Invalid PCRE pattern "%s": %s (version: %s)', $method, $pattern, $message, PCRE_VERSION),
17✔
213
            $code
17✔
214
        );
17✔
215
    }
216
}
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