Skip to content

Commit 842d35f

Browse files
committed
RegexExpressionHelper - Support all bracket style delimiters
1 parent bfef6da commit 842d35f

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/Type/Php/RegexExpressionHelper.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Type\Constant\ConstantStringType;
1111
use PHPStan\Type\Type;
1212
use PHPStan\Type\TypeCombinator;
13+
use function array_key_exists;
1314
use function strrpos;
1415
use function substr;
1516

@@ -76,8 +77,15 @@ public function getPatternModifiers(string $pattern): ?string
7677
return null;
7778
}
7879

79-
if ($delimiter === '{') {
80-
$endDelimiterPos = strrpos($pattern, '}');
80+
// delimiter variants, see https://www.php.net/manual/en/regexp.reference.delimiters.php
81+
$bracketStyleDelimiters = [
82+
'{' => '}',
83+
'(' => ')',
84+
'[' => ']',
85+
'<' => '>',
86+
];
87+
if (array_key_exists($delimiter, $bracketStyleDelimiters)) {
88+
$endDelimiterPos = strrpos($pattern, $bracketStyleDelimiters[$delimiter]);
8189
} else {
8290
// same start and end delimiter
8391
$endDelimiterPos = strrpos($pattern, $delimiter);

tests/PHPStan/Analyser/nsrt/preg_match_shapes_php82.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,20 @@ function doNonAutoCapturingFlag(string $s): void {
2222
}
2323
assertType('array{}|array{0: string, num: numeric-string, 1: numeric-string}', $matches);
2424
}
25+
26+
// delimiter variants, see https://www.php.net/manual/en/regexp.reference.delimiters.php
27+
function (string $s): void {
28+
if (preg_match('{(\d+)(?P<num>\d+)}n', $s, $matches)) {
29+
assertType('array{0: string, num: numeric-string, 1: numeric-string}', $matches);
30+
}
31+
};
32+
function (string $s): void {
33+
if (preg_match('<(\d+)(?P<num>\d+)>n', $s, $matches)) {
34+
assertType('array{0: string, num: numeric-string, 1: numeric-string}', $matches);
35+
}
36+
};
37+
function (string $s): void {
38+
if (preg_match('((\d+)(?P<num>\d+))n', $s, $matches)) {
39+
assertType('array{0: string, num: numeric-string, 1: numeric-string}', $matches);
40+
}
41+
};

0 commit comments

Comments
 (0)