Skip to content

Commit 69599c8

Browse files
committed
added const RegexpException::MESSAGES
1 parent 6201e3f commit 69599c8

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/Utils/Strings.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -487,14 +487,6 @@ public static function replace(string $subject, $pattern, $replacement = null, i
487487
/** @internal */
488488
public static function pcre(string $func, array $args)
489489
{
490-
static $messages = [
491-
PREG_INTERNAL_ERROR => 'Internal error',
492-
PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted',
493-
PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted',
494-
PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data',
495-
PREG_BAD_UTF8_OFFSET_ERROR => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point',
496-
6 => 'Failed due to limited JIT stack space', // PREG_JIT_STACKLIMIT_ERROR
497-
];
498490
$res = Callback::invokeSafe($func, $args, function (string $message) use ($args) {
499491
// compile-time error, not detectable by preg_last_error
500492
throw new RegexpException($message . ' in pattern: ' . implode(' or ', (array) $args[0]));
@@ -503,7 +495,7 @@ public static function pcre(string $func, array $args)
503495
if (($code = preg_last_error()) // run-time error, but preg_last_error & return code are liars
504496
&& ($res === null || !in_array($func, ['preg_filter', 'preg_replace_callback', 'preg_replace'], true))
505497
) {
506-
throw new RegexpException(($messages[$code] ?? 'Unknown error')
498+
throw new RegexpException((RegexpException::MESSAGES[$code] ?? 'Unknown error')
507499
. ' (pattern: ' . implode(' or ', (array) $args[0]) . ')', $code);
508500
}
509501
return $res;

src/Utils/exceptions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ class JsonException extends \Exception
148148
*/
149149
class RegexpException extends \Exception
150150
{
151+
const MESSAGES = [
152+
PREG_INTERNAL_ERROR => 'Internal error',
153+
PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted',
154+
PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted',
155+
PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data',
156+
PREG_BAD_UTF8_OFFSET_ERROR => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point',
157+
6 => 'Failed due to limited JIT stack space', // PREG_JIT_STACKLIMIT_ERROR
158+
];
151159
}
152160

153161

0 commit comments

Comments
 (0)