Open staabm opened 4 years ago
I found these snippets:
I think this should rather be handled in a plugin.
@weirdan I guess because regex is a core language feature, it's not necessarily something that should live in a plugin – for reference PHPStan uses a Nette
library for regex parsing: https://github.com/phpstan/phpstan-src/blob/master/src/Rules/Regexp/RegularExpressionPatternRule.php#L114-L123
phpstan recently also added https://github.com/hoaproject/Regex which is a AST for regex.
just in case this would make more sense for you because of additional use-cases.
OTOH I imagine these errors are relatively rare, and it might parsing every regex might slow Psalm down a little
Instead of parsing to detect errors, it might be nice to parse for capture groups.
That way you can improve the types for e.g. preg_match
.
For example:
$matches = [];
$a = preg_match('/([H])(?P<word>ello)/', "Hello", $matches);
// Now $matches = array
// Better $matches = array{ 0: string, 1: string, word: string, 2: string}
A more detailed discussion can be found here: https://github.com/azjezz/psl/issues/29#issuecomment-706131004
This package could be used to parse capture groups in order to determine the matching array (once a release is tagged): https://github.com/BackEndTea/Regexer
Instead of parsing to detect errors, it might be nice to parse for capture groups. That way you can improve the types for e.g.
preg_match
.
I implemented this in PHPStan, see https://github.com/phpstan/phpstan-src/blob/992072ca90ca17a543339be76b56569e08117c08/src/Type/Php/RegexArrayShapeMatcher.php and related classes
expected a error in https://psalm.dev/r/0005cb68ee like one we have in phpstan https://phpstan.org/r/6aebe69d-ee3a-432d-b3a8-3e996612b14a