phpinspectionsea icon indicating copy to clipboard operation
phpinspectionsea copied to clipboard

Flexible heredoc not recognized

Open tacovandenbroek opened this issue 4 years ago • 5 comments

Subject Details
Plugin Php Inspections (EA Extended) version 4.0.3
Language level PHP 7.3

Current behaviour

2020-04-09_10-48

Expected behaviour

I'd expect no warning for this valid regular expression.

Environment details

PhpStorm 2019.3

tacovandenbroek avatar Apr 09 '20 09:04 tacovandenbroek

Thank you for reporting @tacovandenbroek, I'll look into it.

kalessil avatar Apr 24 '20 12:04 kalessil

@tacovandenbroek : unfortunately I was not able to reproduce the issue. Would you please share an isolated code fragment where the issue appears?

kalessil avatar May 08 '20 11:05 kalessil

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);
}

tacovandenbroek avatar May 08 '20 11:05 tacovandenbroek

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(' /.+/', '');

Bildschirmfoto 2022-02-24 um 12 44 44
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

ausi avatar Feb 24 '22 11:02 ausi

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));

ausi avatar Feb 24 '22 13:02 ausi