-
Notifications
You must be signed in to change notification settings - Fork 471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support # comments in regex #3735
Conversation
This pull request has been marked as ready for review. |
src/Type/Regex/RegexGroupParser.php
Outdated
// The regex engine ignores everything after the (?# until the first closing parenthesis | ||
$regex = preg_replace('/\(\?#[^)]*\)/', '', $regex) ?? ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
general comments which work independent from modifiers
Thank you! |
$modifiers = $this->regexExpressionHelper->getPatternModifiers($regex) ?? ''; | ||
foreach (self::NOT_SUPPORTED_MODIFIERS as $notSupportedModifier) { | ||
if (str_contains($modifiers, $notSupportedModifier)) { | ||
return null; | ||
} | ||
} | ||
|
||
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) ?? ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will also get rid of the character before # due to [^?]
. I realize in most sane cases this will be a space and thus it's harmless, but IMO this should rather be a lookbehind like (?<!\?)
. I assume the goal was to exclude (?#...)
but those are already ignored/handled by the lexer https://github.com/phpstan/phpstan-src/blob/2.0.x/resources/RegexGrammar.pp#L80-L82
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will look into it, thanks for the heads up.
(I had to use this additional char before the #
as we would otherwise destroy comments in (?# ..)
notation). since we replace before lexing we need to make sure we don't turn the comments into something which is no longer a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix in #3739
closes phpstan/phpstan#12242
see https://www.regular-expressions.info/freespacing.html#freecomment
will look into whitespace normalization for the
x
-modifer in a separate PR