Skip to content

Commit 16529f5

Browse files
committed
Do not infer preg_match() shapes for duplicate group names outside of separate alternation branches
Amp-Thread-ID: https://ampcode.com/threads/T-01a177b5-960f-71d2-a3e2-fa7649f39d2c
1 parent bb86a02 commit 16529f5

3 files changed

Lines changed: 75 additions & 0 deletions

File tree

‎src/Type/Php/RegexArrayShapeMatcher.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
149149
$onlyTopLevelAlternation = $regexGroupList->getOnlyTopLevelAlternation();
150150
$flags ??= 0;
151151

152+
if ($regexGroupList->hasDuplicateNamesOutsideOfAlternationBranches(
153+
!$matchesAll && $onlyOptionalTopLevelGroup === null && !$this->containsUnmatchedAsNull($flags, $matchesAll)
154+
? $onlyTopLevelAlternation
155+
: null,
156+
)) {
157+
// which of the groups sharing a name sets the named offset is only modelled
158+
// for groups in different branches of a top level alternation
159+
return null;
160+
}
161+
152162
if (
153163
!$matchesAll
154164
&& $wasMatched->yes()

‎src/Type/Regex/RegexGroupList.php‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Override;
99
use PHPStan\ShouldNotHappenException;
1010
use PHPStan\Type\Type;
11+
use function array_key_exists;
1112
use function array_reverse;
1213
use function count;
1314

@@ -130,6 +131,53 @@ public function getOnlyOptionalTopLevelGroup(): ?RegexCapturingGroup
130131
return $group;
131132
}
132133

134+
/**
135+
* Groups sharing a name (allowed by (?J)) are only understood
136+
* when each of them is in a different branch of the given alternation.
137+
*/
138+
public function hasDuplicateNamesOutsideOfAlternationBranches(?RegexAlternation $alternation): bool
139+
{
140+
$groupIdsByName = [];
141+
foreach ($this->groups as $captureGroup) {
142+
if (!$captureGroup->isNamed()) {
143+
continue;
144+
}
145+
146+
$groupIdsByName[$captureGroup->getName()][] = $captureGroup->getId();
147+
}
148+
149+
$branchByGroupId = [];
150+
if ($alternation !== null) {
151+
foreach ($alternation->getGroupCombinations() as $branch => $groupIds) {
152+
foreach ($groupIds as $groupId) {
153+
$branchByGroupId[$groupId] = $branch;
154+
}
155+
}
156+
}
157+
158+
foreach ($groupIdsByName as $groupIds) {
159+
if (count($groupIds) === 1) {
160+
continue;
161+
}
162+
163+
$branches = [];
164+
foreach ($groupIds as $groupId) {
165+
if (!array_key_exists($groupId, $branchByGroupId)) {
166+
return true;
167+
}
168+
169+
$branch = $branchByGroupId[$groupId];
170+
if (array_key_exists($branch, $branches)) {
171+
return true;
172+
}
173+
174+
$branches[$branch] = true;
175+
}
176+
}
177+
178+
return false;
179+
}
180+
133181
public function getOnlyTopLevelAlternation(): ?RegexAlternation
134182
{
135183
$alternation = null;

‎tests/PHPStan/Analyser/nsrt/preg_match_shapes.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,3 +1117,20 @@ function testGroupsInNegativeAssertionsAreNeverSet(string $s): void {
11171117
assertType("array{0: non-falsy-string, 1?: 'c'|'d'}", $matches);
11181118
}
11191119
}
1120+
1121+
function testDuplicateNamesInternalOption(string $s): void {
1122+
// from php-src ext/pcre/tests/bug79257.phpt
1123+
if (preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))(?<h>baz)/', $s, $matches)) {
1124+
assertType('array<string>', $matches);
1125+
}
1126+
1127+
// from php-src ext/pcre/tests/bug79257.phpt
1128+
if (preg_match('/(?J)(?:(?<g>foo)|(?<g>bar))/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
1129+
assertType('array<string|null>', $matches);
1130+
}
1131+
1132+
// from php-src ext/pcre/tests/bug79257.phpt
1133+
if (preg_match_all('/(?J)(?<chr>[ac])(?<num>\d)|(?<chr>[b])/', $s, $matches, PREG_SET_ORDER)) {
1134+
assertType('list<array<string>>', $matches);
1135+
}
1136+
}

0 commit comments

Comments
 (0)