-
Notifications
You must be signed in to change notification settings - Fork 506
RegexArrayShapeMatcher - Fix shape of single top level alternations #3299
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
@mvorisek please double check the correctness of the test expectations |
|
||
function (string $s): void { | ||
if (preg_match('~a|(\d)|(\s)~', $s, $matches) === 1) { | ||
assertType("array{0: string, 1?: numeric-string}|array{string, '', non-empty-string}", $matches); |
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.
LGTM, thank you for fixing this!
This pull request has been marked as ready for review. |
|
||
function (string $s): void { | ||
if (preg_match('~a|((u)x)|((v)y)~', $s, $matches, PREG_UNMATCHED_AS_NULL) === 1) { | ||
assertType("array{string, 'ux'|null, 'u'|null, 'vy'|null, 'v'|null}", $matches); |
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.
I really like this tagged unions
$combiType = TypeCombinator::union( | ||
new ConstantArrayType([new ConstantIntegerType(0)], [$this->createSubjectValueType($flags, $matchesAll)], [0], [], true), | ||
$combiType, | ||
); | ||
} | ||
|
||
$onlyOptionalTopLevelGroup->clearOverrides(); |
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.
This smells of too much statefulness and mutability. Is this pattern in more places in Regex? I'd much prefer immutable stateless value objects.
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.
totally agree.
I tried doing it in a immutable way, but my attempts don't work because the objects beeing changed here, are referenced by other objects via parent
property and we need the objects referenced by $onlyOptionalTopLevelGroup
to change, no matter which object in the $groupList
directly or nested references it.
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.
Is this pattern in more places in Regex?
we use this pattern for the 2 tagged union special cases in this method here. nowhere else.
d307f86
to
55422b2
Compare
Conflict here |
Thank you! |
closes phpstan/phpstan#11462
additionally in more cases we get more precise tagged unions 🥳