From efbfa0a727a196e63a2b71f0a82f09150c2be354 Mon Sep 17 00:00:00 2001 From: Daimona Eaytoy Date: Thu, 19 Sep 2019 11:55:19 +0200 Subject: [PATCH] StringUtils: Clarify that isValidRegex is for PCRE regexps As suggested in c14571ba26e (I257a096319f1e). Change-Id: Ia91d037f2f4bf8a1bad4eac65fecb1d3e2679d2d --- includes/libs/StringUtils.php | 34 +++++++++---------- .../phpunit/includes/libs/StringUtilsTest.php | 9 ++--- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/includes/libs/StringUtils.php b/includes/libs/StringUtils.php index 19dd8fe4d3..7303d9beb7 100644 --- a/includes/libs/StringUtils.php +++ b/includes/libs/StringUtils.php @@ -316,6 +316,23 @@ class StringUtils { return $text; } + /** + * Utility function to check if the given string is a valid PCRE regex. Avoids + * manually calling suppressWarnings and restoreWarnings, and provides a + * one-line solution without the need to use @. + * + * @since 1.34 + * @param string $string The string you want to check being a valid regex + * @return bool + */ + public static function isValidPCRERegex( $string ) { + AtEase::suppressWarnings(); + // @phan-suppress-next-line PhanParamSuspiciousOrder False positive + $isValid = preg_match( $string, '' ); + AtEase::restoreWarnings(); + return $isValid !== false; + } + /** * Escape a string to make it suitable for inclusion in a preg_replace() * replacement parameter. @@ -343,21 +360,4 @@ class StringUtils { return new ArrayIterator( explode( $separator, $subject ) ); } } - - /** - * Utility function to check if the given string is a valid regex. Avoids - * manually calling suppressWarnings and restoreWarnings, and provides a - * one-line solution without the need to use @. - * - * @since 1.34 - * @param string $string The string you want to check being a valid regex - * @return bool - */ - public static function isValidRegex( $string ) { - AtEase::suppressWarnings(); - // @phan-suppress-next-line PhanParamSuspiciousOrder False positive - $isValid = preg_match( $string, '' ); - AtEase::restoreWarnings(); - return $isValid !== false; - } } diff --git a/tests/phpunit/includes/libs/StringUtilsTest.php b/tests/phpunit/includes/libs/StringUtilsTest.php index 79d5788fa2..66b04adacb 100644 --- a/tests/phpunit/includes/libs/StringUtilsTest.php +++ b/tests/phpunit/includes/libs/StringUtilsTest.php @@ -130,14 +130,15 @@ class StringUtilsTest extends PHPUnit\Framework\TestCase { * @param strin $input * @param bool $expected * @dataProvider provideRegexps - * @covers StringUtils::isValidRegex + * @covers StringUtils::isValidPCRERegex */ - public function testIsValidRegex( $input, $expected ) { - $this->assertSame( $expected, StringUtils::isValidRegex( $input ) ); + public function testIsValidPCRERegex( $input, $expected ) { + $this->assertSame( $expected, StringUtils::isValidPCRERegex( $input ) ); } /** - * Data provider for testValidRegex + * Data provider for testIsValidPCRERegex + * @return array */ public static function provideRegexps() { return [ -- 2.20.1