Skip to content

Commit 1a20bb8

Browse files
committed
bring PREG_UNMATCHED_AS_NULL tests togehter
1 parent e991fad commit 1a20bb8

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

tests/PHPStan/Analyser/nsrt/bug-11311.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,40 @@ function unmatchedAsNullWithOptionalGroup(string $s): void {
2929
assertType('array{}|array{string, string|null}', $matches);
3030
}
3131

32+
function bug11331a(string $url):void {
33+
// group a is actually optional as the entire (?:...) around it is optional
34+
if (preg_match('{^
35+
(?:
36+
(?<a>.+)
37+
)?
38+
(?<b>.+)}mix', $url, $matches, PREG_UNMATCHED_AS_NULL)) {
39+
assertType('array{0: string, a: string|null, 1: string|null, b: string, 2: string}', $matches);
40+
}
41+
}
42+
43+
function bug11331b(string $url):void {
44+
if (preg_match('{^
45+
(?:
46+
(?<a>.+)
47+
)?
48+
(?<b>.+)?}mix', $url, $matches, PREG_UNMATCHED_AS_NULL)) {
49+
assertType('array{0: string, a: string|null, 1: string|null, b: string|null, 2: string|null}', $matches);
50+
}
51+
}
52+
53+
function bug11331c(string $url):void {
54+
if (preg_match('{^
55+
(?:
56+
(?:https?|git)://([^/]+)/ (?# group 1 here can be null if group 2 matches)
57+
| (?# the alternation making it so that only either should match)
58+
git@([^:]+):/? (?# group 2 here can be null if group 1 matches)
59+
)
60+
(?# removing what follows makes the two first groups nullable, although it then has group 2 unsettable which looks buggy too as PREG_UNMATCHED_AS_NULL is present)
61+
([^/]+)
62+
/
63+
([^/]+?)
64+
(?:\.git|/)?
65+
$}x', $url, $matches, PREG_UNMATCHED_AS_NULL)) {
66+
assertType('array{string, string|null, string|null, string, string}', $matches);
67+
}
68+
}

tests/PHPStan/Analyser/nsrt/preg_match_shapes.php

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -353,44 +353,6 @@ function bug11277b(string $value): void
353353
}
354354
}
355355

356-
function bug11331a(string $url):void {
357-
// group a is actually optional as the entire (?:...) around it is optional
358-
if (preg_match('{^
359-
(?:
360-
(?<a>.+)
361-
)?
362-
(?<b>.+)}mix', $url, $matches, PREG_UNMATCHED_AS_NULL)) {
363-
assertType('array{0: string, a: string|null, 1: string|null, b: string, 2: string}', $matches);
364-
}
365-
}
366-
367-
function bug11331b(string $url):void {
368-
if (preg_match('{^
369-
(?:
370-
(?<a>.+)
371-
)?
372-
(?<b>.+)?}mix', $url, $matches, PREG_UNMATCHED_AS_NULL)) {
373-
assertType('array{0: string, a: string|null, 1: string|null, b: string|null, 2: string|null}', $matches);
374-
}
375-
}
376-
377-
function bug11331c(string $url):void {
378-
if (preg_match('{^
379-
(?:
380-
(?:https?|git)://([^/]+)/ (?# group 1 here can be null if group 2 matches)
381-
| (?# the alternation making it so that only either should match)
382-
git@([^:]+):/? (?# group 2 here can be null if group 1 matches)
383-
)
384-
(?# removing what follows makes the two first groups nullable, although it then has group 2 unsettable which looks buggy too as PREG_UNMATCHED_AS_NULL is present)
385-
([^/]+)
386-
/
387-
([^/]+?)
388-
(?:\.git|/)?
389-
$}x', $url, $matches, PREG_UNMATCHED_AS_NULL)) {
390-
assertType('array{string, string|null, string|null, string, string}', $matches);
391-
}
392-
}
393-
394356
// https://www.pcre.org/current/doc/html/pcre2pattern.html#dupgroupnumber
395357
// https://3v4l.org/09qdT
396358
function bug11291(string $s): void {

0 commit comments

Comments
 (0)