Skip to content

Commit 473dff1

Browse files
committed
refactor
1 parent 43964bb commit 473dff1

File tree

2 files changed

+50
-58
lines changed

2 files changed

+50
-58
lines changed

src/Type/Php/RegexArrayShapeMatcher.php

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use PHPStan\Type\IntegerRangeType;
1616
use PHPStan\Type\IntegerType;
1717
use PHPStan\Type\NullType;
18-
use PHPStan\Type\Regex\RegexAlternation;
1918
use PHPStan\Type\Regex\RegexCapturingGroup;
2019
use PHPStan\Type\Regex\RegexExpressionHelper;
2120
use PHPStan\Type\Regex\RegexGroupList;
@@ -117,9 +116,8 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
117116

118117
$regexGroupList = new RegexGroupList($groupList);
119118
$trailingOptionals = $regexGroupList->countTrailingOptionals();
120-
121-
$onlyOptionalTopLevelGroupId = $this->getOnlyOptionalTopLevelGroupId($groupList);
122-
$onlyTopLevelAlternation = $this->getOnlyTopLevelAlternation($groupList);
119+
$onlyOptionalTopLevelGroupId = $regexGroupList->getOnlyOptionalTopLevelGroupId();
120+
$onlyTopLevelAlternation = $regexGroupList->getOnlyTopLevelAlternation();
123121
$flags ??= 0;
124122

125123
if (
@@ -219,60 +217,6 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
219217
);
220218
}
221219

222-
/**
223-
* @param array<int, RegexCapturingGroup> $captureGroups
224-
*/
225-
private function getOnlyOptionalTopLevelGroupId(array $captureGroups): ?int
226-
{
227-
$groupIndex = null;
228-
foreach ($captureGroups as $captureGroup) {
229-
if (!$captureGroup->isTopLevel()) {
230-
continue;
231-
}
232-
233-
if (!$captureGroup->isOptional()) {
234-
return null;
235-
}
236-
237-
if ($groupIndex !== null) {
238-
return null;
239-
}
240-
241-
$groupIndex = $captureGroup->getId();
242-
}
243-
244-
return $groupIndex;
245-
}
246-
247-
/**
248-
* @param array<int, RegexCapturingGroup> $captureGroups
249-
*/
250-
private function getOnlyTopLevelAlternation(array $captureGroups): ?RegexAlternation
251-
{
252-
$alternation = null;
253-
foreach ($captureGroups as $captureGroup) {
254-
if (!$captureGroup->isTopLevel()) {
255-
continue;
256-
}
257-
258-
if (!$captureGroup->inAlternation()) {
259-
return null;
260-
}
261-
262-
if ($captureGroup->inOptionalQuantification()) {
263-
return null;
264-
}
265-
266-
if ($alternation === null) {
267-
$alternation = $captureGroup->getAlternation();
268-
} elseif ($alternation->getId() !== $captureGroup->getAlternation()->getId()) {
269-
return null;
270-
}
271-
}
272-
273-
return $alternation;
274-
}
275-
276220
/**
277221
* @param list<string> $markVerbs
278222
*/

src/Type/Regex/RegexGroupList.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,54 @@ public function removeGroup(int $id): self
102102
return new self($groups);
103103
}
104104

105+
public function getOnlyOptionalTopLevelGroupId(): ?int
106+
{
107+
$groupIndex = null;
108+
foreach ($this->groups as $captureGroup) {
109+
if (!$captureGroup->isTopLevel()) {
110+
continue;
111+
}
112+
113+
if (!$captureGroup->isOptional()) {
114+
return null;
115+
}
116+
117+
if ($groupIndex !== null) {
118+
return null;
119+
}
120+
121+
$groupIndex = $captureGroup->getId();
122+
}
123+
124+
return $groupIndex;
125+
}
126+
127+
public function getOnlyTopLevelAlternation(): ?RegexAlternation
128+
{
129+
$alternation = null;
130+
foreach ($this->groups as $captureGroup) {
131+
if (!$captureGroup->isTopLevel()) {
132+
continue;
133+
}
134+
135+
if (!$captureGroup->inAlternation()) {
136+
return null;
137+
}
138+
139+
if ($captureGroup->inOptionalQuantification()) {
140+
return null;
141+
}
142+
143+
if ($alternation === null) {
144+
$alternation = $captureGroup->getAlternation();
145+
} elseif ($alternation->getId() !== $captureGroup->getAlternation()->getId()) {
146+
return null;
147+
}
148+
}
149+
150+
return $alternation;
151+
}
152+
105153
public function count(): int
106154
{
107155
return count($this->groups);

0 commit comments

Comments
 (0)