1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
<?php
declare(strict_types=1);
namespace Pelago\Emogrifier\Utilities;
/** * PHP's `preg_*` functions can return `false` on failure. * Failure is rare but may occur with a complex pattern applied to a long subject. * Catastrophic backtracking may occur ({@see https://www.regular-expressions.info/catastrophic.html}). * Failure may also occur due to programmer error, if an invalid pattern is provided. * * Catering for failure in each case clutters up the code with error handling. * This class provides wrappers for some `preg_*` functions, with errors handled either * - by throwing an exception, or * - by triggering a user error and providing fallback logic (e.g. returning the subject string unmodified). * * @internal */ final class Preg { /** * whether to throw exceptions on errors (or call `trigger_error` and implement fallback) * * @var bool */ private $throwExceptions = false;
/** * Sets whether exceptions should be thrown if an error occurs. */ public function throwExceptions(bool $throw): self { $this->throwExceptions = $throw;
return $this; }
/** * Wraps `preg_replace`, though does not support `$subject` being an array. * If an error occurs, and exceptions are not being thrown, the original `$subject` is returned. * * @param non-empty-string|non-empty-array<non-empty-string> $pattern * @param string|non-empty-array<string> $replacement * * @throws \RuntimeException */ public function replace($pattern, $replacement, string $subject, int $limit = -1, ?int &$count = null): string { $result = \preg_replace($pattern, $replacement, $subject, $limit, $count);
if ($result === null) { $this->logOrThrowPregLastError(); $result = $subject; }
return $result; }
/** * Wraps `preg_replace_callback`, though does not support `$subject` being an array. * If an error occurs, and exceptions are not being thrown, the original `$subject` is returned. * * Note that (unlike when calling `preg_replace_callback`), `$callback` cannot be a non-public method * represented by an array comprising an object or class name and the method name. * To circumvent that, use `\Closure::fromCallable([$objectOrClassName, 'method'])`. * * @param non-empty-string|non-empty-array<non-empty-string> $pattern * * @throws \RuntimeException */ public function replaceCallback( $pattern, callable $callback, string $subject, int $limit = -1, ?int &$count = null ): string { $result = \preg_replace_callback($pattern, $callback, $subject, $limit, $count);
if ($result === null) { $this->logOrThrowPregLastError(); $result = $subject; }
return $result; }
/** * Wraps `preg_split`. * If an error occurs, and exceptions are not being thrown, * a single-element array containing the original `$subject` is returned. * This method does not support the `PREG_SPLIT_OFFSET_CAPTURE` flag and will throw an exception if it is specified. * * @param non-empty-string $pattern * * @return array<int, string> * * @throws \RuntimeException */ public function split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array { if (($flags & PREG_SPLIT_OFFSET_CAPTURE) !== 0) { throw new \RuntimeException('PREG_SPLIT_OFFSET_CAPTURE is not supported by Preg::split', 1726506348); }
$result = \preg_split($pattern, $subject, $limit, $flags);
if ($result === false) { $this->logOrThrowPregLastError(); $result = [$subject]; }
return $result; }
/** * Wraps `preg_match`. * If an error occurs, and exceptions are not being thrown, * zero (`0`) is returned, and if the `$matches` parameter is provided, it is set to an empty array. * This method does not currently support the `$flags` or `$offset` parameters. * * @param non-empty-string $pattern * @param array<int, string> $matches * * @return 0|1 * * @throws \RuntimeException */ public function match(string $pattern, string $subject, ?array &$matches = null): int { $result = \preg_match($pattern, $subject, $matches);
if ($result === false) { $this->logOrThrowPregLastError(); $result = 0; $matches = []; }
return $result; }
/** * Wraps `preg_match_all`. * * If an error occurs, and exceptions are not being thrown, zero (`0`) is returned. * * In the error case, if the `$matches` parameter is provided, it is set to an array containing empty arrays for the * full pattern match and any possible subpattern match that might be expected. * The algorithm to determine the length of this array simply counts the number of opening parentheses in the * `$pattern`, which may result in a longer array than expected, but guarantees that it is at least as long as * expected. * * This method does not currently support the `$flags` or `$offset` parameters. * * @param non-empty-string $pattern * @param array<int, array<int, string>> $matches * * @throws \RuntimeException */ public function matchAll(string $pattern, string $subject, ?array &$matches = null): int { $result = \preg_match_all($pattern, $subject, $matches);
if ($result === false) { $this->logOrThrowPregLastError(); $result = 0; $matches = \array_fill(0, \substr_count($pattern, '(') + 1, []); }
return $result; }
/** * Obtains the name of the error constant for `preg_last_error` * (based on code posted at {@see https://www.php.net/manual/en/function.preg-last-error.php#124124}) * and puts it into an error message which is either passed to `trigger_error` * or used in the exception which is thrown (depending on the `$throwExceptions` property). * * @throws \RuntimeException */ private function logOrThrowPregLastError(): void { $pcreConstants = \get_defined_constants(true)['pcre']; $pcreErrorConstantNames = \array_flip(\array_filter( $pcreConstants, static function (string $key): bool { return \substr($key, -6) === '_ERROR'; }, ARRAY_FILTER_USE_KEY ));
$pregLastError = \preg_last_error(); $message = 'PCRE regex execution error `' . (string) ($pcreErrorConstantNames[$pregLastError] ?? $pregLastError) . '`';
if ($this->throwExceptions) { throw new \RuntimeException($message, 1592870147); } \trigger_error($message); } }
|