-
Notifications
You must be signed in to change notification settings - Fork 529
Support literal strings in RegexGroupParser #3284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This pull request has been marked as ready for review. |
phpstan-nette needs some expectation adjustments after merge. I will work on it after this one landed. |
src/Type/Php/RegexGroupParser.php
Outdated
@@ -111,28 +113,39 @@ private function walkRegexAst( | |||
array &$groupCombinations, | |||
array &$markVerbs, | |||
bool $captureOnlyNamed, | |||
bool $repeatedMoreThenOnce, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Than
This is becoming ridiculously good! Thank you. |
Woah this is awesome! Makes me kinda want to handle foo? as 'fo'|'foo' for example.. And even character classes with very few possible outcomes could be done that way like [abc] can just be one of those 3.. Not sure if it'd bring enough value but as a regex nerd it just sounds cool. This may need to be forked as a regex static analysis lib tho 😅 I'm only half kidding this is getting so advanced it feels sad that it's not reusable by others. |
@Seldaek character classes have already been implemented with #3285
The current impl is tightly coupled with the PHPStan type-system. Its not that easy to be reused. I think we need a concrete use-case to see whether re-using can make sense. |
Damn you've been on fire these last few days. I'm on vacation so haven't kept track too closely. |
@@ -21,6 +21,7 @@ | |||
use PHPStan\Type\TypeCombinator; | |||
use function array_key_exists; | |||
use function count; | |||
use function implode; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is some issue - https://phpstan.org/r/78e9a062-ad8b-4a3d-a944-e671a4f17a9d - index 1
is optional, ie. the type is missing |array{string}
wider repro - https://phpstan.org/r/89288a2e-96e9-4c48-8419-3adc807bea74 - the last type is wrong too, the 1
and 2
indexes are definitely not present when index 3
is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Index 1/2 is present but empty when 3 exists: https://3v4l.org/VgCUJ
I agree there is a constant type problem though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the last type is wrong too, the
1
and2
indexes are definitely not present when index3
is
sorry, I meant empty string of course
the 2nd repro would be best fixed by "conditional types", ie. like if the inner capturing group type were dependent on the outer capturing group type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please open issues with reproducing snippets for the known problems. there are too many open todos in already-merged PRs and I can't keep track of all of them
thank you
by detecting static string literals in the regex pattern, we make constant-array result-types of the patterns more different.
this helps the TypeCombinator to produce tagged unions more often, which can result in more precise overall types.
first step into the direction described in phpstan/phpstan#11443 (comment)