phpstan / phpstan-nette

Nette Framework class reflection extension for PHPStan & framework-specific rules
MIT License
100 stars 35 forks source link

Strings::match() should be checked for invalid regex #108

Closed enumag closed 1 year ago

enumag commented 1 year ago

I recently added a rule to force using Strings::match() instead of preg_match() in my project for better API and error handling. However it seems that while PHPStan checks preg_match for invalid regular expression (https://github.com/phpstan/phpstan-src/blob/1.9.x/src/Rules/Regexp/RegularExpressionPatternRule.php) the same isn't done for Strings::match().

This is actually very problematic because Strings::match() has the regex and haystack arguments swapped which can be easily missed when switching from preg_match and I already noticed two cases where one of my developers made that mistake.

How should this be solved? Should we copy the rule from PHPStan here and adjust it? Or should the rule in PHPStan be configurable somehow to allow checking other functions and methods? Or maybe a new regex-string type should be added?

MartinMystikJonas commented 1 year ago

I would like regex-string in core PHPStan

enumag commented 1 year ago

That would be my preferred solution as well... Some other languages do have regex as separate type so it would make sense in my opinion.

ondrejmirtes commented 1 year ago

Feel free to copy and adjust the rule :)

github-actions[bot] commented 1 year ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.