Skip to content

Commit 7dc4cc6

Browse files
staabmondrejmirtes
authored andcommitted
Fix regex comment support eating too much chars
1 parent c053dbc commit 7dc4cc6

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Type/Regex/RegexGroupParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function parseGroups(string $regex): ?array
7474

7575
if (str_contains($modifiers, 'x')) {
7676
// in freespacing mode the # character starts a comment and runs until the end of the line
77-
$regex = preg_replace('/[^?]#.*/', '', $regex) ?? '';
77+
$regex = preg_replace('/(?<!\?)#.*/', '', $regex) ?? '';
7878
}
7979

8080
$rawRegex = $this->regexExpressionHelper->removeDelimitersAndModifiers($regex);

tests/PHPStan/Analyser/nsrt/bug-12242.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,14 @@ function bar(string $str): void
3030
assertType('array{string, non-empty-string, string, string}', $matches);
3131
}
3232
}
33+
34+
function foobar(string $str): void
35+
{
36+
$regexp = '/
37+
# (
38+
([\d,]*)# a comment immediately behind with a closing parenthesis )
39+
/x';
40+
if (preg_match($regexp, $str, $match)) {
41+
assertType('array{string, string}', $match);
42+
}
43+
}

0 commit comments

Comments
 (0)