@@ -15,25 +15,33 @@ function doMatch(string $s): void
15
15
assertType ('array{}|array{string} ' , $ matches );
16
16
17
17
if (Preg::match ('/Price: (£|€)\d+/ ' , $ s , $ matches )) {
18
- assertType ('array{string, string|null } ' , $ matches );
18
+ assertType ('array{string, string} ' , $ matches );
19
19
} else {
20
20
assertType ('array{} ' , $ matches );
21
21
}
22
- assertType ('array{}|array{string, string|null } ' , $ matches );
22
+ assertType ('array{}|array{string, string} ' , $ matches );
23
23
24
24
if (Preg::match ('/Price: (£|€)?\d+/ ' , $ s , $ matches )) {
25
- assertType ('array{0: string, 1? : string|null} ' , $ matches );
25
+ assertType ('array{0: string, 1: string|null} ' , $ matches );
26
26
} else {
27
27
assertType ('array{} ' , $ matches );
28
28
}
29
- assertType ('array{}|array{0: string, 1?: string|null} ' , $ matches );
29
+ assertType ('array{}|array{0: string, 1: string|null} ' , $ matches );
30
+
31
+ // passing the PREG_UNMATCHED_AS_NULL should change nothing compared to above as it is always set
32
+ if (Preg::match ('/Price: (£|€)?\d+/ ' , $ s , $ matches , PREG_UNMATCHED_AS_NULL )) {
33
+ assertType ('array{0: string, 1: string|null} ' , $ matches );
34
+ } else {
35
+ assertType ('array{} ' , $ matches );
36
+ }
37
+ assertType ('array{}|array{0: string, 1: string|null} ' , $ matches );
30
38
31
39
if (Preg::isMatch ('/Price: (?<currency>£|€)\d+/ ' , $ s , $ matches )) {
32
- assertType ('array{0: string, currency: string|null , 1: string|null } ' , $ matches );
40
+ assertType ('array{0: string, currency: string, 1: string} ' , $ matches );
33
41
} else {
34
42
assertType ('array{} ' , $ matches );
35
43
}
36
- assertType ('array{}|array{0: string, currency: string|null , 1: string|null } ' , $ matches );
44
+ assertType ('array{}|array{0: string, currency: string, 1: string} ' , $ matches );
37
45
}
38
46
39
47
function doMatchStrictGroups (string $ s ): void
@@ -60,6 +68,18 @@ function doMatchStrictGroups(string $s): void
60
68
assertType ('array{}|array{0: string, test: string, 1: string} ' , $ matches );
61
69
}
62
70
71
+ function doMatchStrictGroupsUnsafe (string $ s ): void
72
+ {
73
+ if (Preg::isMatchStrictGroups ('{Configure Command(?: *</td><td class="v">| *=> *)(.*)(?:</td>|$)}m ' , $ s , $ matches )) {
74
+ // does not error because the match group might be empty but is not optional
75
+ assertType ('array{string, string} ' , $ matches );
76
+ }
77
+
78
+ if (Preg::isMatchStrictGroups ('{Configure Command(?: *</td><td class="v">| *=> *)(.*)?(?:</td>|$)}m ' , $ s , $ matches )) {
79
+ // should error as it is unsafe due to the optional group
80
+ }
81
+ }
82
+
63
83
// disabled until https://github.com/phpstan/phpstan-src/pull/3185 can be resolved
64
84
//
65
85
//function identicalMatch(string $s): void
0 commit comments