Skip to content

Commit a549175

Browse files
committed
Allow overriding the PHP 7.2 support for PREG_UNMATCHED_AS_NULL
1 parent ebce317 commit a549175

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/Type/Php/RegexArrayShapeMatcher.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(
4040
{
4141
}
4242

43-
public function matchType(Type $patternType, ?Type $flagsType, TrinaryLogic $wasMatched): ?Type
43+
public function matchType(Type $patternType, ?Type $flagsType, TrinaryLogic $wasMatched, bool $supportsUnmatchedAsNullOn72 = false): ?Type
4444
{
4545
if ($wasMatched->no()) {
4646
return new ConstantArrayType([], []);
@@ -65,7 +65,7 @@ public function matchType(Type $patternType, ?Type $flagsType, TrinaryLogic $was
6565

6666
$matchedTypes = [];
6767
foreach ($constantStrings as $constantString) {
68-
$matched = $this->matchRegex($constantString->getValue(), $flags, $wasMatched);
68+
$matched = $this->matchRegex($constantString->getValue(), $flags, $wasMatched, $supportsUnmatchedAsNullOn72);
6969
if ($matched === null) {
7070
return null;
7171
}
@@ -83,7 +83,7 @@ public function matchType(Type $patternType, ?Type $flagsType, TrinaryLogic $was
8383
/**
8484
* @param int-mask<PREG_OFFSET_CAPTURE|PREG_UNMATCHED_AS_NULL>|null $flags
8585
*/
86-
private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched): ?Type
86+
private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched, bool $supportsUnmatchedAsNullOn72): ?Type
8787
{
8888
$parseResult = $this->parseGroups($regex);
8989
if ($parseResult === null) {
@@ -100,7 +100,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
100100
$trailingOptionals++;
101101
}
102102

103-
$valueType = $this->getValueType($flags ?? 0);
103+
$valueType = $this->getValueType($flags ?? 0, $supportsUnmatchedAsNullOn72);
104104
$onlyOptionalTopLevelGroup = $this->getOnlyOptionalTopLevelGroup($groupList);
105105
$onlyTopLevelAlternationId = $this->getOnlyTopLevelAlternationId($groupList);
106106

@@ -119,6 +119,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
119119
$wasMatched,
120120
$trailingOptionals,
121121
$flags ?? 0,
122+
$supportsUnmatchedAsNullOn72,
122123
);
123124

124125
return TypeCombinator::union(
@@ -154,6 +155,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
154155
$wasMatched,
155156
$trailingOptionals,
156157
$flags ?? 0,
158+
$supportsUnmatchedAsNullOn72,
157159
);
158160

159161
$combiTypes[] = $combiType;
@@ -177,6 +179,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
177179
$wasMatched,
178180
$trailingOptionals,
179181
$flags ?? 0,
182+
$supportsUnmatchedAsNullOn72,
180183
);
181184
}
182185

@@ -239,6 +242,7 @@ private function buildArrayType(
239242
TrinaryLogic $wasMatched,
240243
int $trailingOptionals,
241244
int $flags,
245+
bool $supportsUnmatchedAsNullOn72,
242246
): Type
243247
{
244248
$builder = ConstantArrayTypeBuilder::createEmpty();
@@ -260,10 +264,10 @@ private function buildArrayType(
260264
} else {
261265
if ($i < $countGroups - $trailingOptionals) {
262266
$optional = false;
263-
if ($this->containsUnmatchedAsNull($flags)) {
267+
if ($this->containsUnmatchedAsNull($flags, $supportsUnmatchedAsNullOn72)) {
264268
$groupValueType = TypeCombinator::removeNull($groupValueType);
265269
}
266-
} elseif ($this->containsUnmatchedAsNull($flags)) {
270+
} elseif ($this->containsUnmatchedAsNull($flags, $supportsUnmatchedAsNullOn72)) {
267271
$optional = false;
268272
} else {
269273
$optional = $captureGroup->isOptional();
@@ -290,9 +294,9 @@ private function buildArrayType(
290294
return $builder->getArray();
291295
}
292296

293-
private function containsUnmatchedAsNull(int $flags): bool
297+
private function containsUnmatchedAsNull(int $flags, bool $supportsUnmatchedAsNullOn72): bool
294298
{
295-
return ($flags & PREG_UNMATCHED_AS_NULL) !== 0 && $this->phpVersion->supportsPregUnmatchedAsNull();
299+
return ($flags & PREG_UNMATCHED_AS_NULL) !== 0 && ($supportsUnmatchedAsNullOn72 || $this->phpVersion->supportsPregUnmatchedAsNull());
296300
}
297301

298302
private function getKeyType(int|string $key): Type
@@ -304,11 +308,11 @@ private function getKeyType(int|string $key): Type
304308
return new ConstantIntegerType($key);
305309
}
306310

307-
private function getValueType(int $flags): Type
311+
private function getValueType(int $flags, bool $supportsUnmatchedAsNullOn72): Type
308312
{
309313
$valueType = new StringType();
310314
$offsetType = IntegerRangeType::fromInterval(0, null);
311-
if ($this->containsUnmatchedAsNull($flags)) {
315+
if ($this->containsUnmatchedAsNull($flags, $supportsUnmatchedAsNullOn72)) {
312316
$valueType = TypeCombinator::addNull($valueType);
313317
// unmatched groups return -1 as offset
314318
$offsetType = IntegerRangeType::fromInterval(-1, null);

0 commit comments

Comments
 (0)