32
32
final class RegexArrayShapeMatcher
33
33
{
34
34
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
+
35
40
private static ?Parser $ parser = null ;
36
41
37
42
public function __construct (
@@ -53,14 +58,17 @@ public function matchType(Type $patternType, ?Type $flagsType, TrinaryLogic $was
53
58
54
59
$ flags = null ;
55
60
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) {
60
62
return null ;
61
63
}
62
64
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
+ }
64
72
}
65
73
66
74
$ matchedTypes = [];
@@ -81,7 +89,7 @@ public function matchType(Type $patternType, ?Type $flagsType, TrinaryLogic $was
81
89
}
82
90
83
91
/**
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
85
93
*/
86
94
private function matchRegex (string $ regex , ?int $ flags , TrinaryLogic $ wasMatched ): ?Type
87
95
{
@@ -295,7 +303,7 @@ private function buildArrayType(
295
303
296
304
private function containsUnmatchedAsNull (int $ flags ): bool
297
305
{
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 () );
299
307
}
300
308
301
309
private function getKeyType (int |string $ key ): Type
0 commit comments