Skip to content

Commit

Permalink
bug #45980 [Finder] Add support of no-capture regex modifier in Multi…
Browse files Browse the repository at this point in the history
…plePcreFilterIterator (available from PHP 8.2) (alexandre-daubois)

This PR was merged into the 4.4 branch.

Discussion
----------

[Finder] Add support of no-capture regex modifier in MultiplePcreFilterIterator (available from PHP 8.2)

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes (new language feature)
| New feature?  | no
| Deprecations? | no
| Tickets       | _NA_
| License       | MIT
| Doc PR        | _NA_

The no-capture regex modifier has been implemented in PHP 8.2. `MultiplePcreFilterIterator` should support it, if available.

- PHP Core PR: php/php-src@e089a50
- Human Readable article about it: https://php.watch/versions/8.2/preg-n-no-capture-modifier

Commits
-------

4ca973f462 [Finder] Add support of no-capture regex modifier in MultiplePcreFilterIterator (available from PHP 8.2)
  • Loading branch information
fabpot committed Apr 14, 2022
2 parents 029e52c + 4d2580c commit 40790bd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
8 changes: 7 additions & 1 deletion Iterator/MultiplePcreFilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
38 changes: 20 additions & 18 deletions Tests/Iterator/MultiplePcreFilterIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
['<foo>', 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 ['<foo>', 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'];
}
}
}

Expand Down

0 comments on commit 40790bd

Please sign in to comment.