From 857e6753651c487e23929ff3551b244da8562d3c Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sun, 9 Aug 2026 13:59:23 +0200 Subject: [PATCH] improve regex error messages --- CHANGELOG.md | 1 + src/RegExp.php | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a174b4..e634cb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Changed - Requires PHP `8.5` +- Exception messages raised in `RegExp` now uses `preg_last_error_msg()` for better debuggability ### Fixed diff --git a/src/RegExp.php b/src/RegExp.php index 291f626..5053f5a 100644 --- a/src/RegExp.php +++ b/src/RegExp.php @@ -20,7 +20,7 @@ private function __construct(string $pattern) /** @psalm-suppress ArgumentTypeCoercion */ if (@\preg_match($pattern, '') === false) { /** @psalm-suppress ImpureFunctionCall */ - throw new LogicException($pattern, \preg_last_error()); + throw new LogicException(\preg_last_error_msg(), \preg_last_error()); } $this->pattern = $pattern; @@ -46,7 +46,7 @@ public function matches(Str $string): bool if ($value === false) { /** @psalm-suppress ImpureFunctionCall */ - throw new InvalidRegex('', \preg_last_error()); + throw new InvalidRegex(\preg_last_error_msg(), \preg_last_error()); } return (bool) $value; @@ -66,7 +66,7 @@ public function capture(Str $string): Map if ($value === false) { /** @psalm-suppress ImpureFunctionCall */ - throw new InvalidRegex('', \preg_last_error()); + throw new InvalidRegex(\preg_last_error_msg(), \preg_last_error()); } /** @var Map */