From 375da678c31345c92f802037d599b5dd5e8088eb Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 15 Jul 2024 14:16:54 +0200 Subject: [PATCH] Support posix character classes in preg_match() inference --- patches/Grammar.patch | 23 +++++++++++++------ .../Analyser/nsrt/preg_match_shapes.php | 6 +++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/patches/Grammar.patch b/patches/Grammar.patch index e3fba1b624..f36dab9228 100644 --- a/patches/Grammar.patch +++ b/patches/Grammar.patch @@ -1,8 +1,17 @@ diff --git a/Grammar.pp b/Grammar.pp -index 5157084..8384417 100644 +index 5157084..48959cb 100644 --- a/Grammar.pp +++ b/Grammar.pp -@@ -73,7 +73,7 @@ +@@ -50,6 +50,8 @@ + %token class_ \[ + %token _class \] + %token range \- ++%token class_literal [^\\\[\]-] ++%token posix_class \[^?:[a-z]+:\] + + // Internal options. + %token internal_option \(\?[\-+]?[imsx]\) +@@ -73,7 +75,7 @@ %token co:comment .*?(?=(? -> default %token nc:capturing_name .+?(?=(?) %token non_capturing_ \(\?: -@@ -109,7 +109,7 @@ +@@ -109,7 +111,7 @@ // Please, see PCRESYNTAX(3), General Category properties, PCRE special category // properties and script names for \p{} and \P{}. %token character_type \\([CdDhHNRsSvVwWX]|[pP]{[^}]+}) @@ -20,16 +29,16 @@ index 5157084..8384417 100644 %token match_point_reset \\K %token literal \\.|. -@@ -168,7 +168,7 @@ quantifier: +@@ -168,7 +170,7 @@ quantifier: ::negative_class_:: #negativeclass | ::class_:: ) - ( range() | literal() )+ -+ ( | range() | literal() )+ ++ ( | | range() | literal() | )+ ? ::_class:: #range: -@@ -178,7 +178,7 @@ simple: +@@ -178,7 +180,7 @@ simple: capturing() | literal() @@ -38,7 +47,7 @@ index 5157084..8384417 100644 ::comment_:: ? ::_comment:: #comment | ( ::named_capturing_:: ::_named_capturing:: #namedcapturing -@@ -191,6 +191,7 @@ capturing: +@@ -191,6 +193,7 @@ capturing: literal: diff --git a/tests/PHPStan/Analyser/nsrt/preg_match_shapes.php b/tests/PHPStan/Analyser/nsrt/preg_match_shapes.php index a3bb2c2b4a..01ca14275b 100644 --- a/tests/PHPStan/Analyser/nsrt/preg_match_shapes.php +++ b/tests/PHPStan/Analyser/nsrt/preg_match_shapes.php @@ -437,3 +437,9 @@ function (string $s, $mixed): void { } assertType('array', $matches); }; + +function bug11323(string $s): void { + if (preg_match('/([*|+?{}()]+)([^*|+[:digit:]?{}()]+)/', $s, $matches)) { + assertType('array{string, string, string}', $matches); + } +}