Skip to content

Commit d4c24c3

Browse files
Fixed preg error lookup for all PHP versions
1 parent 4e49927 commit d4c24c3

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/Regex/Regex.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Dotenv\Regex;
44

5-
use PhpOption\Option;
6-
75
class Regex
86
{
97
/**
@@ -81,20 +79,23 @@ private static function pregAndWrap(callable $operation, $subject)
8179
*/
8280
private static function lookupError($code)
8381
{
84-
return Option::fromValue(get_defined_constants(true))
85-
->filter(function (array $consts) {
86-
return isset($consts['pcre']) && defined('ARRAY_FILTER_USE_KEY');
87-
})
88-
->map(function (array $consts) {
89-
return array_filter($consts['pcre'], function ($msg) {
90-
return substr($msg, -6) === '_ERROR';
91-
}, ARRAY_FILTER_USE_KEY);
92-
})
93-
->flatMap(function (array $errors) use ($code) {
94-
return Option::fromValue(
95-
array_search($code, $errors, true)
96-
);
97-
})
98-
->getOrElse('PREG_ERROR');
82+
if (defined('PREG_JIT_STACKLIMIT_ERROR') && $code === PREG_JIT_STACKLIMIT_ERROR) {
83+
return 'JIT stack limit exhausted';
84+
}
85+
86+
switch ($code) {
87+
case PREG_INTERNAL_ERROR:
88+
return 'Internal error';
89+
case PREG_BAD_UTF8_ERROR:
90+
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
91+
case PREG_BAD_UTF8_OFFSET_ERROR:
92+
return 'The offset did not correspond to the beginning of a valid UTF-8 code point';
93+
case PREG_BACKTRACK_LIMIT_ERROR:
94+
return 'Backtrack limit exhausted';
95+
case PREG_RECURSION_LIMIT_ERROR:
96+
return 'Recursion limit exhausted';
97+
default:
98+
return 'Unknown error';
99+
}
99100
}
100101
}

0 commit comments

Comments
 (0)