Skip to content

Commit 64ab476

Browse files
committed
add test with mandatory group
1 parent ccc0ae2 commit 64ab476

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

tests/PHPStan/Analyser/nsrt/preg_match_shapes.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ function doNonCapturingGroup(string $s): void {
9393

9494
function doNamedSubpattern(string $s): void {
9595
if (preg_match('/\w-(?P<num>\d+)-(\w)/', $s, $matches)) {
96-
assertType('array{0: string, num: string, 1: string, 2: string}', $matches);
96+
// could be assertType('array{0: string, num: string, 1: string, 2: string, 3: string}', $matches);
97+
assertType('array<string>', $matches);
9798
}
98-
assertType('array{}|array{0: string, num: string, 1: string, 2: string}', $matches);
99+
assertType('array<string>', $matches);
99100

100101
if (preg_match('/^(?<name>\S+::\S+)/', $s, $matches)) {
101102
assertType('array{0: string, name: string, 1: string}', $matches);
@@ -364,22 +365,12 @@ function bug11291(string $s): void {
364365
assertType('array{}|array{0: string, 1: string, 2?: string, 3?: string}', $matches);
365366
}
366367

367-
function bug11323a(string $s): void
368-
{
369-
if (preg_match('/Price: (?P<currency>£|€)\d+/', $s, $matches)) {
368+
function unmatchedAsNullWithMandatoryGroup(string $s): void {
369+
if (preg_match('/Price: (?<currency>£|€)\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
370370
assertType('array{0: string, currency: string, 1: string}', $matches);
371371
} else {
372372
assertType('array{}', $matches);
373373
}
374374
assertType('array{}|array{0: string, currency: string, 1: string}', $matches);
375375
}
376376

377-
function bug11323b(string $s): void
378-
{
379-
if (preg_match('/Price: (?<currency>£|€)\d+/', $s, $matches)) {
380-
assertType('array{0: string, currency: string, 1: string}', $matches);
381-
} else {
382-
assertType('array{}', $matches);
383-
}
384-
assertType('array{}|array{0: string, currency: string, 1: string}', $matches);
385-
}

0 commit comments

Comments
 (0)