@@ -26,9 +26,7 @@ class Preg
26
26
*/
27
27
public static function match (string $ pattern , string $ subject , ?array &$ matches = null , int $ flags = 0 , int $ offset = 0 ): int
28
28
{
29
- if (($ flags & PREG_OFFSET_CAPTURE ) !== 0 ) {
30
- throw new \InvalidArgumentException ('PREG_OFFSET_CAPTURE is not supported as it changes the type of $matches, use matchWithOffsets() instead ' );
31
- }
29
+ self ::checkOffsetCapture ($ flags , 'matchWithOffsets ' );
32
30
33
31
$ result = preg_match ($ pattern , $ subject , $ matches , $ flags | PREG_UNMATCHED_AS_NULL , $ offset );
34
32
if ($ result === false ) {
@@ -66,9 +64,7 @@ public static function matchWithOffsets(string $pattern, string $subject, ?array
66
64
*/
67
65
public static function matchAll (string $ pattern , string $ subject , ?array &$ matches = null , int $ flags = 0 , int $ offset = 0 ): int
68
66
{
69
- if (($ flags & PREG_OFFSET_CAPTURE ) !== 0 ) {
70
- throw new \InvalidArgumentException ('PREG_OFFSET_CAPTURE is not supported as it changes the type of $matches, use matchAllWithOffsets() instead ' );
71
- }
67
+ self ::checkOffsetCapture ($ flags , 'matchAllWithOffsets ' );
72
68
73
69
if (($ flags & PREG_SET_ORDER ) !== 0 ) {
74
70
throw new \InvalidArgumentException ('PREG_SET_ORDER is not supported as it changes the type of $matches ' );
@@ -280,4 +276,11 @@ public static function isMatchAllWithOffsets(string $pattern, string $subject, ?
280
276
{
281
277
return (bool ) static ::matchAllWithOffsets ($ pattern , $ subject , $ matches , $ flags , $ offset );
282
278
}
279
+
280
+ protected static function checkOffsetCapture (int $ flags , string $ useFunctionName ): void
281
+ {
282
+ if (($ flags & PREG_OFFSET_CAPTURE ) !== 0 ) {
283
+ throw new \InvalidArgumentException ('PREG_OFFSET_CAPTURE is not supported as it changes the type of $matches, use ' . $ useFunctionName . '() instead ' );
284
+ }
285
+ }
283
286
}
0 commit comments