Skip to content

Commit 375da67

Browse files
committed
Support posix character classes in preg_match() inference
1 parent 83aa36c commit 375da67

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

patches/Grammar.patch

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
diff --git a/Grammar.pp b/Grammar.pp
2-
index 5157084..8384417 100644
2+
index 5157084..48959cb 100644
33
--- a/Grammar.pp
44
+++ b/Grammar.pp
5-
@@ -73,7 +73,7 @@
5+
@@ -50,6 +50,8 @@
6+
%token class_ \[
7+
%token _class \]
8+
%token range \-
9+
+%token class_literal [^\\\[\]-]
10+
+%token posix_class \[^?:[a-z]+:\]
11+
12+
// Internal options.
13+
%token internal_option \(\?[\-+]?[imsx]\)
14+
@@ -73,7 +75,7 @@
615
%token co:comment .*?(?=(?<!\\)\))
716

817
// Capturing group.
@@ -11,7 +20,7 @@ index 5157084..8384417 100644
1120
%token nc:_named_capturing > -> default
1221
%token nc:capturing_name .+?(?=(?<!\\)>)
1322
%token non_capturing_ \(\?:
14-
@@ -109,7 +109,7 @@
23+
@@ -109,7 +111,7 @@
1524
// Please, see PCRESYNTAX(3), General Category properties, PCRE special category
1625
// properties and script names for \p{} and \P{}.
1726
%token character_type \\([CdDhHNRsSvVwWX]|[pP]{[^}]+})
@@ -20,16 +29,16 @@ index 5157084..8384417 100644
2029
%token match_point_reset \\K
2130
%token literal \\.|.
2231

23-
@@ -168,7 +168,7 @@ quantifier:
32+
@@ -168,7 +170,7 @@ quantifier:
2433
::negative_class_:: #negativeclass
2534
| ::class_::
2635
)
2736
- ( range() | literal() )+
28-
+ ( <class_> | range() | literal() )+
37+
+ ( <posix_class> | <class_> | range() | literal() | <class_literal> )+ <range>?
2938
::_class::
3039

3140
#range:
32-
@@ -178,7 +178,7 @@ simple:
41+
@@ -178,7 +180,7 @@ simple:
3342
capturing()
3443
| literal()
3544

@@ -38,7 +47,7 @@ index 5157084..8384417 100644
3847
::comment_:: <comment>? ::_comment:: #comment
3948
| (
4049
::named_capturing_:: <capturing_name> ::_named_capturing:: #namedcapturing
41-
@@ -191,6 +191,7 @@ capturing:
50+
@@ -191,6 +193,7 @@ capturing:
4251

4352
literal:
4453
<character>

tests/PHPStan/Analyser/nsrt/preg_match_shapes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,9 @@ function (string $s, $mixed): void {
437437
}
438438
assertType('array<string>', $matches);
439439
};
440+
441+
function bug11323(string $s): void {
442+
if (preg_match('/([*|+?{}()]+)([^*|+[:digit:]?{}()]+)/', $s, $matches)) {
443+
assertType('array{string, string, string}', $matches);
444+
}
445+
}

0 commit comments

Comments
 (0)