Skip to content

Commit 5f51c6d

Browse files
gwostiondrejmirtes
authored andcommitted
Infer non-negative offsets for mandatory regex groups
Amp-Thread-ID: https://ampcode.com/threads/T-01a19219-4d78-7363-95de-11322f00b310
1 parent 091c5a7 commit 5f51c6d

10 files changed

Lines changed: 49 additions & 30 deletions

‎src/Type/Php/RegexArrayShapeMatcher.php‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,10 @@ private function isSubjectOptional(TrinaryLogic $wasMatched, bool $matchesAll):
380380
*/
381381
private function createSubjectValueType(Type $baseType, int $flags, bool $matchesAll, TrinaryLogic $wasMatched): Type
382382
{
383-
$subjectValueType = TypeCombinator::removeNull($this->getValueType($baseType, $flags, $matchesAll));
383+
$subjectValueType = TypeCombinator::removeNull($this->getValueType($baseType, $flags, $matchesAll, false));
384384

385385
if ($matchesAll) {
386-
$subjectValueType = TypeCombinator::removeNull($this->getValueType(new StringType(), $flags, $matchesAll));
386+
$subjectValueType = TypeCombinator::removeNull($this->getValueType(new StringType(), $flags, $matchesAll, false));
387387

388388
if ($this->containsPatternOrder($flags)) {
389389
$accessoryTypes = [
@@ -428,6 +428,8 @@ private function isGroupOptional(RegexCapturingGroup $captureGroup, TrinaryLogic
428428

429429
private function createGroupValueType(RegexCapturingGroup $captureGroup, TrinaryLogic $wasMatched, int $flags, bool $isTrailingOptional, bool $isLastGroup, bool $matchesAll): Type
430430
{
431+
$canBeUnmatched = $captureGroup->isOptional() || $captureGroup->isForcedUnmatched();
432+
431433
if ($matchesAll) {
432434
if (
433435
(
@@ -448,10 +450,11 @@ private function createGroupValueType(RegexCapturingGroup $captureGroup, Trinary
448450
TypeCombinator::union($captureGroup->getType(), new ConstantStringType('')),
449451
$flags,
450452
$matchesAll,
453+
$canBeUnmatched,
451454
);
452455
$groupValueType = TypeCombinator::removeNull($groupValueType);
453456
} else {
454-
$groupValueType = $this->getValueType($captureGroup->getType(), $flags, $matchesAll);
457+
$groupValueType = $this->getValueType($captureGroup->getType(), $flags, $matchesAll, $canBeUnmatched);
455458
}
456459

457460
if (!$isTrailingOptional && $this->containsUnmatchedAsNull($flags, $matchesAll) && !$captureGroup->isOptional()) {
@@ -475,9 +478,10 @@ private function createGroupValueType(RegexCapturingGroup $captureGroup, Trinary
475478
TypeCombinator::union($captureGroup->getType(), $this->getUnmatchedGroupType($flags)),
476479
$flags,
477480
$matchesAll,
481+
$canBeUnmatched,
478482
);
479483
} else {
480-
$groupValueType = $this->getValueType($captureGroup->getType(), $flags, $matchesAll);
484+
$groupValueType = $this->getValueType($captureGroup->getType(), $flags, $matchesAll, $canBeUnmatched);
481485
}
482486

483487
if ($wasMatched->yes()) {
@@ -540,12 +544,11 @@ private function getKeyType(int|string $key): Type
540544
return new ConstantIntegerType($key);
541545
}
542546

543-
private function getValueType(Type $baseType, int $flags, bool $matchesAll): Type
547+
private function getValueType(Type $baseType, int $flags, bool $matchesAll, bool $canBeUnmatched): Type
544548
{
545549
$valueType = $baseType;
546550

547-
// unmatched groups return -1 as offset
548-
$offsetType = IntegerRangeType::fromInterval(-1, null);
551+
$offsetType = IntegerRangeType::fromInterval($canBeUnmatched ? -1 : 0, null);
549552
if ($this->containsUnmatchedAsNull($flags, $matchesAll)) {
550553
$valueType = TypeCombinator::addNull($valueType);
551554
}

‎src/Type/Regex/RegexCapturingGroup.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public function forceType(Type $type): self
6868
$this->alternation,
6969
$this->inOptionalQuantification,
7070
$this->parent,
71-
$type,
71+
$this->type,
7272
$this->forceNonOptional,
73-
$this->forceType,
73+
$type,
7474
$this->forceAlternationBranchMatched,
7575
);
7676
}
@@ -150,6 +150,11 @@ private function isOptionalWithinAlternationBranch(): bool
150150
return false;
151151
}
152152

153+
public function isForcedUnmatched(): bool
154+
{
155+
return $this->forceType !== null;
156+
}
157+
153158
public function inOptionalQuantification(): bool
154159
{
155160
return $this->inOptionalQuantification;

‎tests/PHPStan/Analyser/nsrt/bug-11311.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ function (string $s): void {
210210

211211
function (string $s): void {
212212
if (preg_match('~a|(\d)|(\s)~', $s, $matches, PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE) === 1) {
213-
assertType("array{array{non-empty-string|null, int<-1, max>}, array{numeric-string|null, int<-1, max>}, array{non-empty-string|null, int<-1, max>}}", $matches);
213+
assertType("array{array{non-empty-string|null, int<0, max>}, array{numeric-string|null, int<-1, max>}, array{non-empty-string|null, int<-1, max>}}", $matches);
214214
}
215215
};
216216

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Bug14959;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
if (1 === preg_match('/^<(\w+)([^>]+?)?/', '<div>hello world</div>', $matches, PREG_OFFSET_CAPTURE)) {
8+
assertType('int<0, max>', $matches[0][1]);
9+
assertType('int<0, max>', $matches[1][1]);
10+
assertType('int<-1, max>|null', $matches[2][1] ?? null);
11+
}

‎tests/PHPStan/Analyser/nsrt/by-ref-writeback-native-type.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ function offsetCapture(string $s): void
1111
return;
1212
}
1313

14-
assertType("array{0: array{non-falsy-string, int<-1, max>}, 1: array{'a', int<-1, max>}, 2?: array{'b', int<-1, max>}}", $m);
15-
assertNativeType("array{0: array{non-falsy-string, int<-1, max>}, 1: array{'a', int<-1, max>}, 2?: array{'b', int<-1, max>}}", $m);
14+
assertType("array{0: array{non-falsy-string, int<0, max>}, 1: array{'a', int<0, max>}, 2?: array{'b', int<-1, max>}}", $m);
15+
assertNativeType("array{0: array{non-falsy-string, int<0, max>}, 1: array{'a', int<0, max>}, 2?: array{'b', int<-1, max>}}", $m);
1616
}
1717

1818
function noFlags(string $s): void

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,25 @@ function (string $size): void {
112112

113113
function (string $size): void {
114114
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE)) {
115-
assertType("non-empty-list<array{0: array{string, int<-1, max>}, num: array{numeric-string, int<-1, max>}, 1: array{numeric-string, int<-1, max>}, suffix?: array{'ab', int<-1, max>}, 2?: array{'ab', int<-1, max>}}>", $matches);
115+
assertType("non-empty-list<array{0: array{string, int<0, max>}, num: array{numeric-string, int<0, max>}, 1: array{numeric-string, int<0, max>}, suffix?: array{'ab', int<-1, max>}, 2?: array{'ab', int<-1, max>}}>", $matches);
116116
}
117117
};
118118

119119
function (string $size): void {
120120
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_PATTERN_ORDER|PREG_OFFSET_CAPTURE)) {
121-
assertType("array{0: non-empty-list<array{string, int<-1, max>}>, num: non-empty-list<array{numeric-string, int<-1, max>}>, 1: non-empty-list<array{numeric-string, int<-1, max>}>, suffix: non-empty-list<array{''|'ab', int<-1, max>}>, 2: non-empty-list<array{''|'ab', int<-1, max>}>}", $matches);
121+
assertType("array{0: non-empty-list<array{string, int<0, max>}>, num: non-empty-list<array{numeric-string, int<0, max>}>, 1: non-empty-list<array{numeric-string, int<0, max>}>, suffix: non-empty-list<array{''|'ab', int<-1, max>}>, 2: non-empty-list<array{''|'ab', int<-1, max>}>}", $matches);
122122
}
123123
};
124124

125125
function (string $size): void {
126126
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_UNMATCHED_AS_NULL|PREG_SET_ORDER|PREG_OFFSET_CAPTURE)) {
127-
assertType("non-empty-list<array{0: array{string|null, int<-1, max>}, num: array{numeric-string|null, int<-1, max>}, 1: array{numeric-string|null, int<-1, max>}, suffix: array{'ab'|null, int<-1, max>}, 2: array{'ab'|null, int<-1, max>}}>", $matches);
127+
assertType("non-empty-list<array{0: array{string|null, int<0, max>}, num: array{numeric-string|null, int<0, max>}, 1: array{numeric-string|null, int<0, max>}, suffix: array{'ab'|null, int<-1, max>}, 2: array{'ab'|null, int<-1, max>}}>", $matches);
128128
}
129129
};
130130

131131
function (string $size): void {
132132
if (preg_match_all('/ab(?P<num>\d+)(?P<suffix>ab)?/', $size, $matches, PREG_UNMATCHED_AS_NULL|PREG_PATTERN_ORDER|PREG_OFFSET_CAPTURE)) {
133-
assertType("array{0: non-empty-list<array{string|null, int<-1, max>}>, num: non-empty-list<array{numeric-string|null, int<-1, max>}>, 1: non-empty-list<array{numeric-string|null, int<-1, max>}>, suffix: non-empty-list<array{'ab'|null, int<-1, max>}>, 2: non-empty-list<array{'ab'|null, int<-1, max>}>}", $matches);
133+
assertType("array{0: non-empty-list<array{string|null, int<0, max>}>, num: non-empty-list<array{numeric-string|null, int<0, max>}>, 1: non-empty-list<array{numeric-string|null, int<0, max>}>, suffix: non-empty-list<array{'ab'|null, int<-1, max>}>, 2: non-empty-list<array{'ab'|null, int<-1, max>}>}", $matches);
134134
}
135135
};
136136

@@ -142,7 +142,7 @@ public function sayHello(string $content): void
142142
return;
143143
}
144144

145-
assertType('array{list<array{string, int<-1, max>}>}', $matches);
145+
assertType('array{list<array{string, int<0, max>}>}', $matches);
146146
}
147147

148148
public function sayFoo(string $content): void
@@ -165,13 +165,13 @@ public function sayBar(string $content): void
165165

166166
function doFoobar(string $s): void {
167167
if (preg_match_all('/(foo)?(bar)?(baz)?/', $s, $matches, PREG_OFFSET_CAPTURE)) {
168-
assertType("array{non-empty-list<array{string, int<-1, max>}>, non-empty-list<array{''|'foo', int<-1, max>}>, non-empty-list<array{''|'bar', int<-1, max>}>, non-empty-list<array{''|'baz', int<-1, max>}>}", $matches);
168+
assertType("array{non-empty-list<array{string, int<0, max>}>, non-empty-list<array{''|'foo', int<-1, max>}>, non-empty-list<array{''|'bar', int<-1, max>}>, non-empty-list<array{''|'baz', int<-1, max>}>}", $matches);
169169
}
170170
}
171171

172172
function doFoobarNull(string $s): void {
173173
if (preg_match_all('/(foo)?(bar)?(baz)?/', $s, $matches, PREG_OFFSET_CAPTURE|PREG_UNMATCHED_AS_NULL)) {
174-
assertType("array{non-empty-list<array{string|null, int<-1, max>}>, non-empty-list<array{'foo'|null, int<-1, max>}>, non-empty-list<array{'bar'|null, int<-1, max>}>, non-empty-list<array{'baz'|null, int<-1, max>}>}", $matches);
174+
assertType("array{non-empty-list<array{string|null, int<0, max>}>, non-empty-list<array{'foo'|null, int<-1, max>}>, non-empty-list<array{'bar'|null, int<-1, max>}>, non-empty-list<array{'baz'|null, int<-1, max>}>}", $matches);
175175
}
176176
}
177177
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ function doNamedSubpattern(string $s): void {
115115

116116
function doOffsetCapture(string $s): void {
117117
if (preg_match('/(foo)(bar)(baz)/', $s, $matches, PREG_OFFSET_CAPTURE)) {
118-
assertType("array{array{non-falsy-string, int<-1, max>}, array{'foo', int<-1, max>}, array{'bar', int<-1, max>}, array{'baz', int<-1, max>}}", $matches);
118+
assertType("array{array{non-falsy-string, int<0, max>}, array{'foo', int<0, max>}, array{'bar', int<0, max>}, array{'baz', int<0, max>}}", $matches);
119119
}
120-
assertType("array{}|array{array{non-falsy-string, int<-1, max>}, array{'foo', int<-1, max>}, array{'bar', int<-1, max>}, array{'baz', int<-1, max>}}", $matches);
120+
assertType("array{}|array{array{non-falsy-string, int<0, max>}, array{'foo', int<0, max>}, array{'bar', int<0, max>}, array{'baz', int<0, max>}}", $matches);
121121
}
122122

123123
function doUnknownFlags(string $s, int $flags): void {
@@ -679,13 +679,13 @@ function (string $s): void {
679679
function (string $value): void
680680
{
681681
if (preg_match('/^(x)*$/', $value, $matches, PREG_OFFSET_CAPTURE)) {
682-
assertType("array{0: array{string, int<-1, max>}, 1?: array{non-empty-string, int<-1, max>}}", $matches);
682+
assertType("array{0: array{string, int<0, max>}, 1?: array{non-empty-string, int<0, max>}}", $matches);
683683
}
684684
};
685685

686686
function (string $value): void {
687687
if (preg_match('/^(?:(x)|(y))*$/', $value, $matches, PREG_OFFSET_CAPTURE)) {
688-
assertType("array{0: array{string, int<-1, max>}, 1?: array{non-empty-string, int<-1, max>}}|array{array{string, int<-1, max>}, array{'', int<-1, max>}, array{non-empty-string, int<-1, max>}}", $matches);
688+
assertType("array{0: array{string, int<0, max>}, 1?: array{non-empty-string, int<0, max>}}|array{array{string, int<0, max>}, array{'', int<-1, max>}, array{non-empty-string, int<0, max>}}", $matches);
689689
}
690690
};
691691

@@ -724,7 +724,7 @@ function (string $s): void {
724724

725725
function (string $s): void {
726726
if (preg_match('~a|(\d)|(\s)~', $s, $matches, PREG_OFFSET_CAPTURE) === 1) {
727-
assertType("array{0: array{non-empty-string, int<-1, max>}, 1?: array{numeric-string, int<-1, max>}}|array{array{non-empty-string, int<-1, max>}, array{'', int<-1, max>}, array{non-empty-string, int<-1, max>}}", $matches);
727+
assertType("array{0: array{non-empty-string, int<0, max>}, 1?: array{numeric-string, int<0, max>}}|array{array{non-empty-string, int<0, max>}, array{'', int<-1, max>}, array{non-empty-string, int<0, max>}}", $matches);
728728
}
729729
};
730730

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
function doOffsetCaptureWithUnmatchedNull(string $s): void {
88
// see https://3v4l.org/07rBO#v8.2.9
99
if (preg_match('/(foo)(bar)(baz)/', $s, $matches, PREG_OFFSET_CAPTURE|PREG_UNMATCHED_AS_NULL)) {
10-
assertType("array{array{non-falsy-string|null, int<-1, max>}, array{'foo'|null, int<-1, max>}, array{'bar'|null, int<-1, max>}, array{'baz'|null, int<-1, max>}}", $matches);
10+
assertType("array{array{non-falsy-string|null, int<0, max>}, array{'foo'|null, int<0, max>}, array{'bar'|null, int<0, max>}, array{'baz'|null, int<0, max>}}", $matches);
1111
}
12-
assertType("array{}|array{array{non-falsy-string|null, int<-1, max>}, array{'foo'|null, int<-1, max>}, array{'bar'|null, int<-1, max>}, array{'baz'|null, int<-1, max>}}", $matches);
12+
assertType("array{}|array{array{non-falsy-string|null, int<0, max>}, array{'foo'|null, int<0, max>}, array{'bar'|null, int<0, max>}, array{'baz'|null, int<0, max>}}", $matches);
1313
}
1414

1515
function doNonAutoCapturingModifier(string $s): void {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function (string $s): void {
2323
preg_replace_callback(
2424
'/(foo)?(bar)?(baz)?/',
2525
function ($matches) {
26-
assertType("list{0: array{string, int<-1, max>}, 1?: array{''|'foo', int<-1, max>}, 2?: array{''|'bar', int<-1, max>}, 3?: array{'baz', int<-1, max>}}", $matches);
26+
assertType("list{0: array{string, int<0, max>}, 1?: array{''|'foo', int<-1, max>}, 2?: array{''|'bar', int<-1, max>}, 3?: array{'baz', int<-1, max>}}", $matches);
2727
return '';
2828
},
2929
$s,
@@ -37,7 +37,7 @@ function (string $s): void {
3737
preg_replace_callback(
3838
'/(foo)?(bar)?(baz)?/',
3939
function ($matches) {
40-
assertType("array{array{string|null, int<-1, max>}, array{'foo'|null, int<-1, max>}, array{'bar'|null, int<-1, max>}, array{'baz'|null, int<-1, max>}}", $matches);
40+
assertType("array{array{string|null, int<0, max>}, array{'foo'|null, int<-1, max>}, array{'bar'|null, int<-1, max>}, array{'baz'|null, int<-1, max>}}", $matches);
4141
return '';
4242
},
4343
$s,

‎tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3009,11 +3009,11 @@ public function testConstantParameterCheck(): void
30093009
110,
30103010
],
30113011
[
3012-
'Parameter #2 $callback of function preg_replace_callback expects callable(array<string>): string, Closure(mixed): array{non-falsy-string, int<-1, max>} given.',
3012+
'Parameter #2 $callback of function preg_replace_callback expects callable(array<string>): string, Closure(mixed): array{non-falsy-string, int<0, max>} given.',
30133013
113,
30143014
],
30153015
[
3016-
'Parameter #2 $callback of function preg_replace_callback expects callable(array<string>): string, Closure(mixed): array{non-falsy-string|null, int<-1, max>} given.',
3016+
'Parameter #2 $callback of function preg_replace_callback expects callable(array<string>): string, Closure(mixed): array{non-falsy-string|null, int<0, max>} given.',
30173017
116,
30183018
],
30193019
[

0 commit comments

Comments
 (0)