Open tacovandenbroek opened 4 years ago
Thank you for reporting @tacovandenbroek, I'll look into it.
@tacovandenbroek : unfortunately I was not able to reproduce the issue. Would you please share an isolated code fragment where the issue appears?
Sure! It seems to depend on the context. I couldn't trigger the inspection without wrapping the code in a function:
<?php
function doesMatch(string $subject): bool {
$regex = <<< REGEX
(foo)
REGEX;
return (bool)preg_match($regex, $subject);
}
I think this issue is caused by leading white space (even though the heredoc example does not have the whitespace in the resulting string).
I was able to reproduce it with this simple test case:
preg_match(' /.+/', '');
PhpStorm 2021.3.2, PHP Inspections (EA Extended) 4.0.7.1
Leading and trailing whitespace is supported in PCRE, see https://3v4l.org/Kvcqa
Maybe changing the lines from here https://github.com/kalessil/phpinspectionsea/blob/9bd52620388ca5f8f0d894d49ad5e921a6adf533/src/main/java/com/kalessil/phpStorm/phpInspectionsEA/inspectors/regularExpressions/NotOptimalRegularExpressionsInspector.java#L67-L71 to something like this (adding \s*
to the beginning and end) could work?
matchers.add(Pattern.compile("^\\s*([^{<(\\[])(.*)(\\1)([a-zA-Z]+)?\\s*$", Pattern.DOTALL));
matchers.add(Pattern.compile("^\\s*(\\{)(.*)(\\})([a-zA-Z]+)?\\s*$", Pattern.DOTALL));
matchers.add(Pattern.compile("^\\s*(<)(.*)(>)([a-zA-Z]+)?\\s*$", Pattern.DOTALL));
matchers.add(Pattern.compile("^\\s*(\\()(.*)(\\))([a-zA-Z]+)?\\s*$", Pattern.DOTALL));
matchers.add(Pattern.compile("^\\s*(\\[)(.*)(\\])([a-zA-Z]+)?\\s*$", Pattern.DOTALL));
Current behaviour
Expected behaviour
I'd expect no warning for this valid regular expression.
Environment details
PhpStorm 2019.3