Skip to content

Commit 562ca94

Browse files
committed
Improve PHPStan types
1 parent 5330017 commit 562ca94

File tree

4 files changed

+47
-30
lines changed

4 files changed

+47
-30
lines changed

phpstan-baseline.neon

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
parameters:
2-
ignoreErrors:
3-
-
4-
message: "#^Function preg_replace_callback invoked with 6 parameters, 3\\-5 required\\.$#"
5-
count: 1
6-
path: src/Preg.php
2+
ignoreErrors:
3+
-
4+
message: "#^Function preg_replace_callback invoked with 6 parameters, 3\\-5 required\\.$#"
5+
count: 1
6+
path: src/Preg.php
77

8-
-
9-
message: "#^Function preg_replace_callback_array invoked with 5 parameters, 2\\-4 required\\.$#"
10-
count: 1
11-
path: src/Preg.php
8+
-
9+
message: "#^Function preg_replace_callback_array invoked with 5 parameters, 2\\-4 required\\.$#"
10+
count: 1
11+
path: src/Preg.php
12+
13+
-
14+
message: "#^Offset 0 does not exist on string\\|null\\.$#"
15+
count: 1
16+
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 or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
130+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
130131
*/
131132
public static function replaceCallback($pattern, callable $replacement, $subject, int $limit = -1, int &$count = null, int $flags = 0): string
132133
{
@@ -151,10 +152,10 @@ public static function replaceCallback($pattern, callable $replacement, $subject
151152
}
152153

153154
/**
154-
* @param array<string, callable> $pattern
155+
* @param array<string, callable(array<int|string, string|null>): string> $pattern
155156
* @param string $subject
156157
* @param int $count Set by method
157-
* @param int $flags PREG_OFFSET_CAPTURE or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
158+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
158159
*/
159160
public static function replaceCallbackArray(array $pattern, $subject, int $limit = -1, int &$count = null, int $flags = 0): string
160161
{
@@ -180,7 +181,7 @@ public static function replaceCallbackArray(array $pattern, $subject, int $limit
180181
}
181182

182183
/**
183-
* @param int $flags PREG_SPLIT_NO_EMPTY or PREG_SPLIT_DELIM_CAPTURE
184+
* @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
184185
* @return list<string>
185186
*/
186187
public static function split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array
@@ -198,7 +199,7 @@ public static function split(string $pattern, string $subject, int $limit = -1,
198199
}
199200

200201
/**
201-
* @param int $flags PREG_SPLIT_NO_EMPTY or PREG_SPLIT_DELIM_CAPTURE, PREG_SPLIT_OFFSET_CAPTURE is always set
202+
* @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
202203
* @return list<array{string, int}>
203204
* @phpstan-return list<array{string, int<0, max>}>
204205
*/
@@ -216,7 +217,7 @@ public static function splitWithOffsets(string $pattern, string $subject, int $l
216217
* @template T of string|\Stringable
217218
* @param string $pattern
218219
* @param array<T> $array
219-
* @param int $flags PREG_GREP_INVERT
220+
* @param int-mask<PREG_GREP_INVERT> $flags PREG_GREP_INVERT
220221
* @return array<T>
221222
*/
222223
public static function grep(string $pattern, array $array, int $flags = 0): array
@@ -232,7 +233,7 @@ public static function grep(string $pattern, array $array, int $flags = 0): arra
232233
/**
233234
* @param non-empty-string $pattern
234235
* @param array<string|null> $matches Set by method
235-
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
236+
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
236237
*/
237238
public static function isMatch(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): bool
238239
{
@@ -242,7 +243,7 @@ public static function isMatch(string $pattern, string $subject, ?array &$matche
242243
/**
243244
* @param non-empty-string $pattern
244245
* @param array<int|string, list<string|null>> $matches Set by method
245-
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
246+
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
246247
*/
247248
public static function isMatchAll(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): bool
248249
{
@@ -254,7 +255,7 @@ public static function isMatchAll(string $pattern, string $subject, ?array &$mat
254255
*
255256
* @param non-empty-string $pattern
256257
* @param array<int|string, array{string|null, int}> $matches Set by method
257-
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
258+
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
258259
*
259260
* @phpstan-param array<int|string, array{string|null, int<-1, max>}> $matches
260261
*/
@@ -268,7 +269,7 @@ public static function isMatchWithOffsets(string $pattern, string $subject, ?arr
268269
*
269270
* @param non-empty-string $pattern
270271
* @param array<int|string, list<array{string|null, int}>> $matches Set by method
271-
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
272+
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
272273
*
273274
* @phpstan-param array<int|string, list<array{string|null, int<-1, max>}>> $matches
274275
*/

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 or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
95+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
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 or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
107+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
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)