From 4d2580c0dd97c775fbc5c1f399b60a44992b9d67 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Thu, 10 Jun 2021 09:06:30 +0200 Subject: [PATCH] [Finder] Add support of no-capture regex modifier in MultiplePcreFilterIterator (available from PHP 8.2) [Finder] Add support of no-capture regex modifier in MultiplePcreFilterIterator (available from PHP 8.2) --- Iterator/MultiplePcreFilterIterator.php | 8 +++- .../MultiplePcreFilterIteratorTest.php | 38 ++++++++++--------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/Iterator/MultiplePcreFilterIterator.php b/Iterator/MultiplePcreFilterIterator.php index 18b082ec..e185d130 100644 --- a/Iterator/MultiplePcreFilterIterator.php +++ b/Iterator/MultiplePcreFilterIterator.php @@ -83,7 +83,13 @@ protected function isAccepted($string) */ protected function isRegex($str) { - if (preg_match('/^(.{3,}?)[imsxuADU]*$/', $str, $m)) { + $availableModifiers = 'imsxuADU'; + + if (\PHP_VERSION_ID >= 80200) { + $availableModifiers .= 'n'; + } + + if (preg_match('/^(.{3,}?)['.$availableModifiers.']*$/', $str, $m)) { $start = substr($m[1], 0, 1); $end = substr($m[1], -1); diff --git a/Tests/Iterator/MultiplePcreFilterIteratorTest.php b/Tests/Iterator/MultiplePcreFilterIteratorTest.php index 590aea21..157c647e 100644 --- a/Tests/Iterator/MultiplePcreFilterIteratorTest.php +++ b/Tests/Iterator/MultiplePcreFilterIteratorTest.php @@ -27,24 +27,26 @@ public function testIsRegex($string, $isRegex, $message) public function getIsRegexFixtures() { - return [ - ['foo', false, 'string'], - [' foo ', false, '" " is not a valid delimiter'], - ['\\foo\\', false, '"\\" is not a valid delimiter'], - ['afooa', false, '"a" is not a valid delimiter'], - ['//', false, 'the pattern should contain at least 1 character'], - ['/a/', true, 'valid regex'], - ['/foo/', true, 'valid regex'], - ['/foo/i', true, 'valid regex with a single modifier'], - ['/foo/imsxu', true, 'valid regex with multiple modifiers'], - ['#foo#', true, '"#" is a valid delimiter'], - ['{foo}', true, '"{,}" is a valid delimiter pair'], - ['[foo]', true, '"[,]" is a valid delimiter pair'], - ['(foo)', true, '"(,)" is a valid delimiter pair'], - ['', true, '"<,>" is a valid delimiter pair'], - ['*foo.*', false, '"*" is not considered as a valid delimiter'], - ['?foo.?', false, '"?" is not considered as a valid delimiter'], - ]; + yield ['foo', false, 'string']; + yield [' foo ', false, '" " is not a valid delimiter']; + yield ['\\foo\\', false, '"\\" is not a valid delimiter']; + yield ['afooa', false, '"a" is not a valid delimiter']; + yield ['//', false, 'the pattern should contain at least 1 character']; + yield ['/a/', true, 'valid regex']; + yield ['/foo/', true, 'valid regex']; + yield ['/foo/i', true, 'valid regex with a single modifier']; + yield ['/foo/imsxu', true, 'valid regex with multiple modifiers']; + yield ['#foo#', true, '"#" is a valid delimiter']; + yield ['{foo}', true, '"{,}" is a valid delimiter pair']; + yield ['[foo]', true, '"[,]" is a valid delimiter pair']; + yield ['(foo)', true, '"(,)" is a valid delimiter pair']; + yield ['', true, '"<,>" is a valid delimiter pair']; + yield ['*foo.*', false, '"*" is not considered as a valid delimiter']; + yield ['?foo.?', false, '"?" is not considered as a valid delimiter']; + + if (\PHP_VERSION_ID >= 80200) { + yield ['/foo/n', true, 'valid regex with the no-capture modifier']; + } } }