From bbf5cb884dac749a52e72c24f2b8dab7e5f75d31 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 1 Jul 2023 20:42:49 +0300 Subject: [PATCH] Rename methods --- README.md | 5 +++-- src/CombinedRegexp.php | 11 ++++++++--- tests/CombinedRegexpTest.php | 24 ++++++++++++------------ 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 39f2723..21a561d 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ The package provides: - `NumericHelper` that has static methods to work with numeric strings; - `Inflector` provides methods such as `toPlural()` or `toSlug()` that derive a new string based on the string given; - `WildcardPattern` is a shell wildcard pattern to match strings against. +- `CombinedRegexp` is a wrapper that optimizes multiple regular expressions matching. ## Requirements @@ -196,8 +197,8 @@ $patterns = [ $regexp = new CombinedRegexp($patterns, 'i'); $regexp->matches('a5'); // true – matches the third pattern $regexp->matches('A5'); // true – matches the third pattern because of `i` flag that is applied to all regular expressions -$regexp->matchPattern('a5'); // '^a\d$' – the pattern that matched -$regexp->matchPatternPosition('a5'); // 2 – the index of the pattern in the array +$regexp->getMatchingPattern('a5'); // '^a\d$' – the pattern that matched +$regexp->getMatchingPatternPosition('a5'); // 2 – the index of the pattern in the array $regexp->getCompiledPattern(); // '~(?|first|second()|^a\d$()())~' ``` diff --git a/src/CombinedRegexp.php b/src/CombinedRegexp.php index f045786..13d27c2 100644 --- a/src/CombinedRegexp.php +++ b/src/CombinedRegexp.php @@ -55,16 +55,16 @@ public function matches(string $string): bool * Returns pattern that matches the given string. * @throws \Exception if the string does not match any of the patterns. */ - public function matchPattern(string $string): string + public function getMatchingPattern(string $string): string { - return $this->patterns[$this->matchPatternPosition($string)]; + return $this->patterns[$this->getMatchingPatternPosition($string)]; } /** * Returns position of the pattern that matches the given string. * @throws \Exception if the string does not match any of the patterns. */ - public function matchPatternPosition(string $string): int + public function getMatchingPatternPosition(string $string): int { $match = preg_match($this->compiledPattern, $string, $matches); if ($match !== 1) { @@ -87,6 +87,11 @@ private function compilePatterns(array $patterns): string { $quotedPatterns = []; + /** + * Possible mutant escaping, but it's ok for our case. + * It doesn't matter where to place `()` in the pattern: + * https://regex101.com/r/lE1Q1S/1, https://regex101.com/r/rWg7Fj/1 + */ for ($i = 0; $i < count($patterns); $i++) { $quotedPatterns[] = $patterns[$i] . str_repeat('()', $i); } diff --git a/tests/CombinedRegexpTest.php b/tests/CombinedRegexpTest.php index fb38017..e63a15e 100644 --- a/tests/CombinedRegexpTest.php +++ b/tests/CombinedRegexpTest.php @@ -107,12 +107,12 @@ public static function dataMatchAny(): iterable } /** - * @dataProvider dataMatchPattern + * @dataProvider dataMatchingPattern */ - public function testMatchPattern(array $patterns, string $string, string $expectedResult): void + public function testMatchingPattern(array $patterns, string $string, string $expectedResult): void { $regexp = new CombinedRegexp($patterns); - $actualResult = $regexp->matchPattern($string); + $actualResult = $regexp->getMatchingPattern($string); $message = sprintf( 'Failed to assert that string "%s" matches the string "%s".', $regexp->getCompiledPattern(), @@ -121,7 +121,7 @@ public function testMatchPattern(array $patterns, string $string, string $expect $this->assertEquals($expectedResult, $actualResult, $message); } - public static function dataMatchPattern(): iterable + public static function dataMatchingPattern(): iterable { yield 'the "first" pattern' => [ [ @@ -154,12 +154,12 @@ public static function dataMatchPattern(): iterable } /** - * @dataProvider dataMatchPosition + * @dataProvider dataMatchingPatternPosition */ - public function testMatchPosition(array $patterns, string $string, int $expectedResult): void + public function testMatchingPatternPosition(array $patterns, string $string, int $expectedResult): void { $regexp = new CombinedRegexp($patterns); - $actualResult = $regexp->matchPatternPosition($string); + $actualResult = $regexp->getMatchingPatternPosition($string); $message = sprintf( 'Failed to assert that string "%s" matches the string "%s".', $regexp->getCompiledPattern(), @@ -168,7 +168,7 @@ public function testMatchPosition(array $patterns, string $string, int $expected $this->assertEquals($expectedResult, $actualResult, $message); } - public static function dataMatchPosition(): iterable + public static function dataMatchingPatternPosition(): iterable { yield 'the "first" pattern' => [ [ @@ -215,8 +215,8 @@ public function testMatchDifferentDelimiters( $string, ); $this->assertTrue($regexp->matches($string), $message); - $this->assertEquals($patterns[0], $regexp->matchPattern($string), $message); - $this->assertEquals(0, $regexp->matchPatternPosition($string), $message); + $this->assertEquals($patterns[0], $regexp->getMatchingPattern($string), $message); + $this->assertEquals(0, $regexp->getMatchingPatternPosition($string), $message); } public static function dataMatchDifferentDelimiters(): iterable @@ -249,7 +249,7 @@ public function testInvalidMatch(): void $this->expectException(\Exception::class); $this->expectExceptionMessage('Failed to match pattern "/(?|abc)/" with string "def".'); - $regexp->matchPatternPosition($string); - $regexp->matchPattern($string); + $regexp->getMatchingPatternPosition($string); + $regexp->getMatchingPattern($string); } }