Skip to content

Commit 6af1968

Browse files
committed
Merge branch '2.x'
2 parents 3c1d8ca + 562ca94 commit 6af1968

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

phpstan-baseline.neon

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
parameters:
2-
ignoreErrors:
2+
ignoreErrors:
3+
-
4+
message: "#^Offset 0 does not exist on string\\|null\\.$#"
5+
count: 1
6+
path: tests/PregTests/ReplaceCallbackTest.php

src/Preg.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Preg
2121
/**
2222
* @param non-empty-string $pattern
2323
* @param array<string|null> $matches Set by method
24-
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
24+
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
2525
* @return 0|1
2626
*/
2727
public static function match(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): int
@@ -41,7 +41,7 @@ public static function match(string $pattern, string $subject, ?array &$matches
4141
*
4242
* @param non-empty-string $pattern
4343
* @param array<int|string, array{string|null, int}> $matches Set by method
44-
* @param int $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
44+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_UNMATCHED_AS_NULL and PREG_OFFSET_CAPTURE are always set, no other flags are supported
4545
* @return 0|1
4646
*
4747
* @phpstan-param array<int|string, array{string|null, int<-1, max>}> $matches
@@ -59,7 +59,7 @@ public static function matchWithOffsets(string $pattern, string $subject, ?array
5959
/**
6060
* @param non-empty-string $pattern
6161
* @param array<int|string, list<string|null>> $matches Set by method
62-
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
62+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_SET_ORDER> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
6363
* @return 0|positive-int
6464
*/
6565
public static function matchAll(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): int
@@ -83,7 +83,7 @@ public static function matchAll(string $pattern, string $subject, ?array &$match
8383
*
8484
* @param non-empty-string $pattern
8585
* @param array<int|string, list<array{string|null, int}>> $matches Set by method
86-
* @param int $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
86+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
8787
* @return 0|positive-int
8888
*
8989
* @phpstan-param array<int|string, list<array{string|null, int<-1, max>}>> $matches
@@ -124,9 +124,10 @@ public static function replace($pattern, $replacement, $subject, int $limit = -1
124124

125125
/**
126126
* @param string|string[] $pattern
127+
* @param callable(array<int|string, string|null>): string $replacement
127128
* @param string $subject
128129
* @param int $count Set by method
129-
* @param int $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
130+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
130131
*/
131132
public static function replaceCallback($pattern, callable $replacement, $subject, int $limit = -1, int &$count = null, int $flags = 0): string
132133
{
@@ -147,10 +148,10 @@ public static function replaceCallback($pattern, callable $replacement, $subject
147148
}
148149

149150
/**
150-
* @param array<string, callable> $pattern
151+
* @param array<string, callable(array<int|string, string|null>): string> $pattern
151152
* @param string $subject
152153
* @param int $count Set by method
153-
* @param int $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
154+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
154155
*/
155156
public static function replaceCallbackArray(array $pattern, $subject, int $limit = -1, int &$count = null, int $flags = 0): string
156157
{
@@ -172,7 +173,7 @@ public static function replaceCallbackArray(array $pattern, $subject, int $limit
172173
}
173174

174175
/**
175-
* @param int $flags PREG_SPLIT_NO_EMPTY or PREG_SPLIT_DELIM_CAPTURE
176+
* @param int-mask<PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_OFFSET_CAPTURE> $flags PREG_SPLIT_NO_EMPTY or PREG_SPLIT_DELIM_CAPTURE
176177
* @return list<string>
177178
*/
178179
public static function split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array
@@ -190,7 +191,7 @@ public static function split(string $pattern, string $subject, int $limit = -1,
190191
}
191192

192193
/**
193-
* @param int $flags PREG_SPLIT_NO_EMPTY or PREG_SPLIT_DELIM_CAPTURE, PREG_SPLIT_OFFSET_CAPTURE is always set
194+
* @param int-mask<PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_OFFSET_CAPTURE> $flags PREG_SPLIT_NO_EMPTY or PREG_SPLIT_DELIM_CAPTURE, PREG_SPLIT_OFFSET_CAPTURE is always set
194195
* @return list<array{string, int}>
195196
* @phpstan-return list<array{string, int<0, max>}>
196197
*/
@@ -208,7 +209,7 @@ public static function splitWithOffsets(string $pattern, string $subject, int $l
208209
* @template T of string|\Stringable
209210
* @param string $pattern
210211
* @param array<T> $array
211-
* @param int $flags PREG_GREP_INVERT
212+
* @param int-mask<PREG_GREP_INVERT> $flags PREG_GREP_INVERT
212213
* @return array<T>
213214
*/
214215
public static function grep(string $pattern, array $array, int $flags = 0): array
@@ -224,7 +225,7 @@ public static function grep(string $pattern, array $array, int $flags = 0): arra
224225
/**
225226
* @param non-empty-string $pattern
226227
* @param array<string|null> $matches Set by method
227-
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
228+
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
228229
*/
229230
public static function isMatch(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): bool
230231
{
@@ -234,7 +235,7 @@ public static function isMatch(string $pattern, string $subject, ?array &$matche
234235
/**
235236
* @param non-empty-string $pattern
236237
* @param array<int|string, list<string|null>> $matches Set by method
237-
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
238+
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
238239
*/
239240
public static function isMatchAll(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): bool
240241
{
@@ -246,7 +247,7 @@ public static function isMatchAll(string $pattern, string $subject, ?array &$mat
246247
*
247248
* @param non-empty-string $pattern
248249
* @param array<int|string, array{string|null, int}> $matches Set by method
249-
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
250+
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
250251
*
251252
* @phpstan-param array<int|string, array{string|null, int<-1, max>}> $matches
252253
*/
@@ -260,7 +261,7 @@ public static function isMatchWithOffsets(string $pattern, string $subject, ?arr
260261
*
261262
* @param non-empty-string $pattern
262263
* @param array<int|string, list<array{string|null, int}>> $matches Set by method
263-
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
264+
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
264265
*
265266
* @phpstan-param array<int|string, list<array{string|null, int<-1, max>}>> $matches
266267
*/

src/Regex.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static function isMatch(string $pattern, string $subject, int $offset = 0
2323

2424
/**
2525
* @param non-empty-string $pattern
26-
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
26+
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
2727
*/
2828
public static function match(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchResult
2929
{
@@ -38,7 +38,7 @@ public static function match(string $pattern, string $subject, int $flags = 0, i
3838
* Runs preg_match with PREG_OFFSET_CAPTURE
3939
*
4040
* @param non-empty-string $pattern
41-
* @param int $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
41+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
4242
*/
4343
public static function matchWithOffsets(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchWithOffsetsResult
4444
{
@@ -49,7 +49,7 @@ public static function matchWithOffsets(string $pattern, string $subject, int $f
4949

5050
/**
5151
* @param non-empty-string $pattern
52-
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
52+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_SET_ORDER> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
5353
*/
5454
public static function matchAll(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchAllResult
5555
{
@@ -68,7 +68,7 @@ public static function matchAll(string $pattern, string $subject, int $flags = 0
6868
* Runs preg_match_all with PREG_OFFSET_CAPTURE
6969
*
7070
* @param non-empty-string $pattern
71-
* @param int $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
71+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
7272
*/
7373
public static function matchAllWithOffsets(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchAllWithOffsetsResult
7474
{
@@ -90,8 +90,9 @@ public static function replace($pattern, $replacement, $subject, int $limit = -1
9090

9191
/**
9292
* @param string|string[] $pattern
93+
* @param callable(array<int|string, string|null>): string $replacement
9394
* @param string $subject
94-
* @param int $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
95+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
9596
*/
9697
public static function replaceCallback($pattern, callable $replacement, $subject, int $limit = -1, int $flags = 0): ReplaceResult
9798
{
@@ -101,9 +102,9 @@ public static function replaceCallback($pattern, callable $replacement, $subject
101102
}
102103

103104
/**
104-
* @param array<string, callable> $pattern
105+
* @param array<string, callable(array<int|string, string|null>): string> $pattern
105106
* @param string $subject
106-
* @param int $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
107+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
107108
*/
108109
public static function replaceCallbackArray(array $pattern, $subject, int $limit = -1, int $flags = 0): ReplaceResult
109110
{

tests/PregTests/ReplaceCallbackTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ public function testSuccess(): void
3131
self::assertSame('abc(d)', $result);
3232
}
3333

34+
public function testSuccessWithOffset(): void
35+
{
36+
$result = Preg::replaceCallback('{(?P<m>d)}', function ($match) {
37+
return '('.$match[0][0].')';
38+
}, 'abcd', -1, $count, PREG_OFFSET_CAPTURE);
39+
40+
self::assertSame(1, $count);
41+
self::assertSame('abc(d)', $result);
42+
}
43+
3444
public function testSuccessNoRef(): void
3545
{
3646
$result = Preg::replaceCallback('{(?P<m>d)}', function ($match) {

0 commit comments

Comments
 (0)