phpinspectionsea icon indicating copy to clipboard operation
phpinspectionsea copied to clipboard

Misleading regex suggestion / wrong regex simplification

Open lserni opened this issue 1 year ago • 0 comments

Subject Details
Plugin Php (EA Ultimate), Version 2021.5
Language level PHP 7.4

Current behaviour

In all PHP levels, EA Ultimate marks as suspicious or improveable a perfectly valid regex; in the second case, the "improvement" results in a broken regex and a different (but, now, correct) warning.

preg_match('#\[(\d+)-(\d+)]#', '[10-25]', $gregs);
 // ")-(" range is looking rather suspicious, please check
preg_match('#[[](\d+)-(\d+)[]]#', '[10-25]', $gregs);
// '[[]' can be simplified to [   '[]] can be simplified to ]

If I follow the suggestions in the second line, I get another "range is looking rather suspicious" which is correct, since now the regex won't compile.

Workaround:

preg_match("#\[(\d+)\x2d(\d+)]$#" is not recognized as a "range", and [ is a correct escape for regex. so no warning is issued (at the expense of some legibility, since \x2d is not immediately recognizable as a "-").

Expected behaviour

Both lines are accepted with no warning, since both are correct.

Possibly, the second line might suggest "[[] can be written [ for better legibility". Or it could assume that [[] can never be simplified by [ (and the same for ] ).

Environment details

Information from Help - About dialog

lserni avatar May 22 '23 14:05 lserni