Skip to content

Commit 21d53c7

Browse files
committed
stronger types
1 parent 9af7350 commit 21d53c7

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/Type/Php/RegexArrayShapeMatcher.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,18 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
116116

117117
$regexGroupList = new RegexGroupList($groupList);
118118
$trailingOptionals = $regexGroupList->countTrailingOptionals();
119-
$onlyOptionalTopLevelGroupId = $regexGroupList->getOnlyOptionalTopLevelGroupId();
119+
$onlyOptionalTopLevelGroup = $regexGroupList->getOnlyOptionalTopLevelGroup();
120120
$onlyTopLevelAlternation = $regexGroupList->getOnlyTopLevelAlternation();
121121
$flags ??= 0;
122122

123123
if (
124124
!$matchesAll
125125
&& $wasMatched->yes()
126-
&& $onlyOptionalTopLevelGroupId !== null
126+
&& $onlyOptionalTopLevelGroup !== null
127127
) {
128128
// if only one top level capturing optional group exists
129129
// we build a more precise tagged union of a empty-match and a match with the group
130-
$regexGroupList = $regexGroupList->forceGroupIdNonOptional($onlyOptionalTopLevelGroupId);
130+
$regexGroupList = $regexGroupList->forceGroupNonOptional($onlyOptionalTopLevelGroup);
131131

132132
$combiType = $this->buildArrayType(
133133
$regexGroupList,
@@ -149,7 +149,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
149149
return $combiType;
150150
} elseif (
151151
!$matchesAll
152-
&& $onlyOptionalTopLevelGroupId === null
152+
&& $onlyOptionalTopLevelGroup === null
153153
&& $onlyTopLevelAlternation !== null
154154
&& !$wasMatched->no()
155155
) {
@@ -161,21 +161,21 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
161161
$comboList = new RegexGroupList($groupList);
162162

163163
$beforeCurrentCombo = true;
164-
foreach ($comboList as $groupId => $group) {
165-
if (in_array($groupId, $groupCombo, true)) {
164+
foreach ($comboList as $group) {
165+
if (in_array($group->getId(), $groupCombo, true)) {
166166
$isOptionalAlternation = $group->inOptionalAlternation();
167-
$comboList = $comboList->forceGroupIdNonOptional($group->getId());
167+
$comboList = $comboList->forceGroupNonOptional($group);
168168
$beforeCurrentCombo = false;
169169
} elseif ($beforeCurrentCombo && !$group->resetsGroupCounter()) {
170-
$comboList = $comboList->forceGroupIdTypeAndNonOptional(
171-
$group->getId(),
170+
$comboList = $comboList->forceGroupTypeAndNonOptional(
171+
$group,
172172
$this->containsUnmatchedAsNull($flags, $matchesAll) ? new NullType() : new ConstantStringType(''),
173173
);
174174
} elseif (
175175
$group->getAlternationId() === $onlyTopLevelAlternation->getId()
176176
&& !$this->containsUnmatchedAsNull($flags, $matchesAll)
177177
) {
178-
$comboList = $comboList->removeGroup($groupId);
178+
$comboList = $comboList->removeGroup($group);
179179
}
180180
}
181181

src/Type/Regex/RegexGroupList.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,22 @@ public function countTrailingOptionals(): int
3737
return $trailingOptionals;
3838
}
3939

40-
public function forceGroupIdNonOptional(int $id): self
40+
public function forceGroupNonOptional(RegexCapturingGroup $group): self
4141
{
42-
return $this->cloneAndReParentList($id);
42+
return $this->cloneAndReParentList($group);
4343
}
4444

45-
public function forceGroupIdTypeAndNonOptional(int $id, Type $type): self
45+
public function forceGroupTypeAndNonOptional(RegexCapturingGroup $group, Type $type): self
4646
{
47-
return $this->cloneAndReParentList($id, $type);
47+
return $this->cloneAndReParentList($group, $type);
4848
}
4949

50-
private function cloneAndReParentList(int $id, ?Type $type = null): self
50+
private function cloneAndReParentList(RegexCapturingGroup $target, ?Type $type = null): self
5151
{
5252
$groups = [];
5353
$forcedGroup = null;
5454
foreach ($this->groups as $i => $group) {
55-
if ($group->getId() === $id) {
55+
if ($group->getId() === $target->getId()) {
5656
$forcedGroup = $group->forceNonOptional();
5757
if ($type !== null) {
5858
$forcedGroup = $forcedGroup->forceType($type);
@@ -78,7 +78,7 @@ private function cloneAndReParentList(int $id, ?Type $type = null): self
7878
continue;
7979
}
8080

81-
if ($parent->getId() === $id) {
81+
if ($parent->getId() === $target->getId()) {
8282
$groups[$i] = $groups[$i]->withParent($forcedGroup);
8383
}
8484
$parent = $parent->getParent();
@@ -88,11 +88,11 @@ private function cloneAndReParentList(int $id, ?Type $type = null): self
8888
return new self($groups);
8989
}
9090

91-
public function removeGroup(int $id): self
91+
public function removeGroup(RegexCapturingGroup $remove): self
9292
{
9393
$groups = [];
9494
foreach ($this->groups as $i => $group) {
95-
if ($group->getId() === $id) {
95+
if ($group->getId() === $remove->getId()) {
9696
continue;
9797
}
9898

@@ -102,9 +102,9 @@ public function removeGroup(int $id): self
102102
return new self($groups);
103103
}
104104

105-
public function getOnlyOptionalTopLevelGroupId(): ?int
105+
public function getOnlyOptionalTopLevelGroup(): ?RegexCapturingGroup
106106
{
107-
$groupIndex = null;
107+
$group = null;
108108
foreach ($this->groups as $captureGroup) {
109109
if (!$captureGroup->isTopLevel()) {
110110
continue;
@@ -114,14 +114,14 @@ public function getOnlyOptionalTopLevelGroupId(): ?int
114114
return null;
115115
}
116116

117-
if ($groupIndex !== null) {
117+
if ($group !== null) {
118118
return null;
119119
}
120120

121-
$groupIndex = $captureGroup->getId();
121+
$group = $captureGroup;
122122
}
123123

124-
return $groupIndex;
124+
return $group;
125125
}
126126

127127
public function getOnlyTopLevelAlternation(): ?RegexAlternation

0 commit comments

Comments
 (0)