Skip to content

Commit b0ca01e

Browse files
committed
add tests for bug2792
1 parent 9a0bfd2 commit b0ca01e

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

Diff for: tests/PHPStan/Analyser/nsrt/preg_match_shapes.php

+59-1
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,65 @@ function bug12749f(string $str): void
10111011
}
10121012
}
10131013

1014-
function bug12397(string $string) : array {
1014+
function bug12397(string $string): void {
10151015
$m = preg_match('#\b([A-Z]{2,})-(\d+)#', $string, $match);
10161016
assertType('list{0?: string, 1?: non-falsy-string, 2?: numeric-string}', $match);
10171017
}
1018+
1019+
function bug2792(string $string): void {
1020+
if (preg_match('~a\Kb~', $string, $match) === 1) {
1021+
assertType('array{\'b\'}', $match);
1022+
}
1023+
1024+
if (preg_match('~a\K~', $string, $match) === 1) {
1025+
assertType('array{\'\'}', $match);
1026+
}
1027+
1028+
if (preg_match('~a\K.+~', $string, $match) === 1) {
1029+
assertType('array{non-empty-string}', $match);
1030+
}
1031+
1032+
if (preg_match('~a\K.*~', $string, $match) === 1) {
1033+
assertType('array{string}', $match);
1034+
}
1035+
1036+
if (preg_match('~a\K(.+)~', $string, $match) === 1) {
1037+
assertType('array{non-empty-string, non-empty-string}', $match);
1038+
}
1039+
1040+
if (preg_match('~a\K(.*)~', $string, $match) === 1) {
1041+
assertType('array{string, string}', $match);
1042+
}
1043+
1044+
if (preg_match('~a\K(.+?)~', $string, $match) === 1) {
1045+
assertType('array{non-empty-string, non-empty-string}', $match);
1046+
}
1047+
1048+
if (preg_match('~a\K(.*?)~', $string, $match) === 1) {
1049+
assertType('array{string, string}', $match);
1050+
}
1051+
1052+
if (preg_match('~a\K(?=.+)~', $string, $match) === 1) {
1053+
assertType('array{\'\'}', $match);
1054+
}
1055+
1056+
if (preg_match('~a\K(?=.*)~', $string, $match) === 1) {
1057+
assertType('array{\'\'}', $match);
1058+
}
1059+
1060+
if (preg_match('~a(?:x\Kb|c)~', $string, $match) === 1) {
1061+
assertType('array{\'ac\'|\'b\'}', $match);
1062+
}
1063+
1064+
if (preg_match('~a(?:c|x\Kb)~', $string, $match) === 1) {
1065+
assertType('array{\'ac\'|\'b\'}', $match);
1066+
}
1067+
1068+
if (preg_match('~a(y|(?:x\Kb|c))d~', $string, $match) === 1) {
1069+
assertType('array{\'acd\'|\'ayd\'|\'bd\', \'c\'|\'xb\'|\'y\'}', $match);
1070+
}
1071+
1072+
if (preg_match('~a((?:c|x\Kb)|y)d~', $string, $match) === 1) {
1073+
assertType('array{\'acd\'|\'ayd\'|\'bd\', \'c\'|\'xb\'|\'y\'}', $match);
1074+
}
1075+
}

Diff for: tests/PHPStan/Analyser/nsrt/preg_replace_callback_shapes.php

+11
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,14 @@ function ($matches) {
4545
PREG_OFFSET_CAPTURE|PREG_UNMATCHED_AS_NULL
4646
);
4747
};
48+
49+
function bug2792(string $string) : void {
50+
preg_replace_callback(
51+
'~\'(?:[^\']+|\'\')*+\'\K|\[(\w*)\]~',
52+
function ($matches) {
53+
assertType("array{0: string, 1?: string}", $matches);
54+
return '';
55+
},
56+
$string
57+
);
58+
}

0 commit comments

Comments
 (0)