Skip to content

Commit 3a32992

Browse files
authored
Allow overriding the PHP 7.2 support for PREG_UNMATCHED_AS_NULL
1 parent 6ffd0dd commit 3a32992

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/Type/Php/RegexArrayShapeMatcher.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
final class RegexArrayShapeMatcher
3333
{
3434

35+
/**
36+
* Pass this into $flagsType as well if the library supports emulating PREG_UNMATCHED_AS_NULL on PHP 7.2 and 7.3
37+
*/
38+
public const PREG_UNMATCHED_AS_NULL_ON_72_73 = 2048;
39+
3540
private static ?Parser $parser = null;
3641

3742
public function __construct(
@@ -53,14 +58,17 @@ public function matchType(Type $patternType, ?Type $flagsType, TrinaryLogic $was
5358

5459
$flags = null;
5560
if ($flagsType !== null) {
56-
if (
57-
!$flagsType instanceof ConstantIntegerType
58-
|| !in_array($flagsType->getValue(), [PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL, PREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL], true)
59-
) {
61+
if (!$flagsType instanceof ConstantIntegerType) {
6062
return null;
6163
}
6264

63-
$flags = $flagsType->getValue();
65+
/** @var int-mask<PREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL | self::PREG_UNMATCHED_AS_NULL_ON_72_73> $flags */
66+
$flags = $flagsType->getValue() & (PREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL | self::PREG_UNMATCHED_AS_NULL_ON_72_73);
67+
68+
// some other unsupported/unexpected flag was passed in
69+
if ($flags !== $flagsType->getValue()) {
70+
return null;
71+
}
6472
}
6573

6674
$matchedTypes = [];
@@ -81,7 +89,7 @@ public function matchType(Type $patternType, ?Type $flagsType, TrinaryLogic $was
8189
}
8290

8391
/**
84-
* @param int-mask<PREG_OFFSET_CAPTURE|PREG_UNMATCHED_AS_NULL>|null $flags
92+
* @param int-mask<PREG_OFFSET_CAPTURE|PREG_UNMATCHED_AS_NULL|self::PREG_UNMATCHED_AS_NULL_ON_72_73>|null $flags
8593
*/
8694
private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched): ?Type
8795
{
@@ -295,7 +303,7 @@ private function buildArrayType(
295303

296304
private function containsUnmatchedAsNull(int $flags): bool
297305
{
298-
return ($flags & PREG_UNMATCHED_AS_NULL) !== 0 && $this->phpVersion->supportsPregUnmatchedAsNull();
306+
return ($flags & PREG_UNMATCHED_AS_NULL) !== 0 && (($flags & self::PREG_UNMATCHED_AS_NULL_ON_72_73) !== 0 || $this->phpVersion->supportsPregUnmatchedAsNull());
299307
}
300308

301309
private function getKeyType(int|string $key): Type

0 commit comments

Comments
 (0)