vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.56k stars 660 forks source link

psalm should detect syntax errors in regex-patterns #2708

Open staabm opened 4 years ago

staabm commented 4 years ago

expected a error in https://psalm.dev/r/0005cb68ee like one we have in phpstan https://phpstan.org/r/6aebe69d-ee3a-432d-b3a8-3e996612b14a

psalm-github-bot[bot] commented 4 years ago

I found these snippets:

https://psalm.dev/r/0005cb68ee ```php
weirdan commented 4 years ago

I think this should rather be handled in a plugin.

muglug commented 4 years ago

@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

staabm commented 4 years ago

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.

muglug commented 4 years ago

OTOH I imagine these errors are relatively rare, and it might parsing every regex might slow Psalm down a little

veewee commented 4 years ago

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:

https://3v4l.org/ptoMe

$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

staabm commented 2 months ago

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