Skip to content

Commit

Permalink
Fixed preg error lookup for all PHP versions
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Jul 14, 2020
1 parent 4e49927 commit d4c24c3
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/Regex/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Dotenv\Regex;

use PhpOption\Option;

class Regex
{
/**
Expand Down Expand Up @@ -81,20 +79,23 @@ private static function pregAndWrap(callable $operation, $subject)
*/
private static function lookupError($code)
{
return Option::fromValue(get_defined_constants(true))
->filter(function (array $consts) {
return isset($consts['pcre']) && defined('ARRAY_FILTER_USE_KEY');
})
->map(function (array $consts) {
return array_filter($consts['pcre'], function ($msg) {
return substr($msg, -6) === '_ERROR';
}, ARRAY_FILTER_USE_KEY);
})
->flatMap(function (array $errors) use ($code) {
return Option::fromValue(
array_search($code, $errors, true)
);
})
->getOrElse('PREG_ERROR');
if (defined('PREG_JIT_STACKLIMIT_ERROR') && $code === PREG_JIT_STACKLIMIT_ERROR) {
return 'JIT stack limit exhausted';
}

switch ($code) {
case PREG_INTERNAL_ERROR:
return 'Internal error';
case PREG_BAD_UTF8_ERROR:
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
case PREG_BAD_UTF8_OFFSET_ERROR:
return 'The offset did not correspond to the beginning of a valid UTF-8 code point';
case PREG_BACKTRACK_LIMIT_ERROR:
return 'Backtrack limit exhausted';
case PREG_RECURSION_LIMIT_ERROR:
return 'Recursion limit exhausted';
default:
return 'Unknown error';
}
}
}

0 comments on commit d4c24c3

Please sign in to comment.