fix: distinguish PCRE failure from a mismatch in Assert::regex()
preg_match() has three outcomes -- 1, 0 and false -- but regex() folded false into the "no match" branch. An uncompilable pattern or an exhausted PCRE limit therefore leaked a raw PHP warning out of the library and then reported the misleading "The value ... does not match the expected pattern." Check for false separately and report the pattern together with preg_last_error_msg(), following the existing false === $x = nativeCall() handling in Assert::strlen(). The caller's custom message is intentionally not reused for that branch, since it describes a mismatch that never happened. Matching values and genuine mismatches are unaffected.
This commit is contained in:
1 parent
f6badd5191
commit
24cce423ab
2 files changed
+70
-1
No files matched your search
+9
-1
@@ -1507,7 +1507,15 @@ class Assert
|
||||
static::string($value);
|
||||
static::string($pattern);
|
||||
|
||||
if (!\preg_match($pattern, $value)) {
|
||||
if (false === $result = @\preg_match($pattern, $value)) {
|
||||
static::reportInvalidArgument(\sprintf(
|
||||
'The pattern %s could not be evaluated: %s.',
|
||||
static::valueToString($pattern),
|
||||
\preg_last_error_msg()
|
||||
));
|
||||
}
|
||||
|
||||
if (0 === $result) {
|
||||
$message = self::resolveMessage($message);
|
||||
static::reportInvalidArgument(\sprintf(
|
||||
$message ?: 'The value %s does not match the expected pattern.',
|
||||
|
||||
Reference in new issue
Block a user