From 6bf30fd5c694ef1f1a961904b6bff3e927b07514 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 17 Dec 2024 11:52:29 +0100 Subject: [PATCH] Fix regex comment support eating too much chars --- src/Type/Regex/RegexGroupParser.php | 2 +- tests/PHPStan/Analyser/nsrt/bug-12242.php | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Type/Regex/RegexGroupParser.php b/src/Type/Regex/RegexGroupParser.php index a98fb20b42..beea95bce0 100644 --- a/src/Type/Regex/RegexGroupParser.php +++ b/src/Type/Regex/RegexGroupParser.php @@ -74,7 +74,7 @@ public function parseGroups(string $regex): ?array if (str_contains($modifiers, 'x')) { // in freespacing mode the # character starts a comment and runs until the end of the line - $regex = preg_replace('/[^?]#.*/', '', $regex) ?? ''; + $regex = preg_replace('/(?regexExpressionHelper->removeDelimitersAndModifiers($regex); diff --git a/tests/PHPStan/Analyser/nsrt/bug-12242.php b/tests/PHPStan/Analyser/nsrt/bug-12242.php index cb6d424567..4d065367a2 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-12242.php +++ b/tests/PHPStan/Analyser/nsrt/bug-12242.php @@ -30,3 +30,14 @@ function bar(string $str): void assertType('array{string, non-empty-string, string, string}', $matches); } } + +function foobar(string $str): void +{ + $regexp = '/ + # ( + ([\d,]*)# a comment immediately behind with a closing parenthesis ) + /x'; + if (preg_match($regexp, $str, $match)) { + assertType('array{string, string}', $match); + } +}