Skip to content

Commit

Permalink
Fix regex comment support eating too much chars
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 17, 2024
1 parent c053dbc commit 6bf30fd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Type/Regex/RegexGroupParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('/(?<!\?)#.*/', '', $regex) ?? '';
}

$rawRegex = $this->regexExpressionHelper->removeDelimitersAndModifiers($regex);
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12242.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 6bf30fd

Please sign in to comment.