Skip to content

Commit

Permalink
Rename methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Jul 1, 2023
1 parent 08c9edc commit bbf5cb8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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$()())~'
```

Expand Down
11 changes: 8 additions & 3 deletions src/CombinedRegexp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);

Check warning on line 96 in src/CombinedRegexp.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ * 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); + $quotedPatterns[] = str_repeat('()', $i) . $patterns[$i]; } $combinedRegexps = '(?|' . strtr(implode('|', $quotedPatterns), [self::REGEXP_DELIMITER => self::QUOTE_REPLACER]) . ')'; return self::REGEXP_DELIMITER . $combinedRegexps . self::REGEXP_DELIMITER; } }

Check warning on line 96 in src/CombinedRegexp.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ * 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); + $quotedPatterns[] = str_repeat('()', $i) . $patterns[$i]; } $combinedRegexps = '(?|' . strtr(implode('|', $quotedPatterns), [self::REGEXP_DELIMITER => self::QUOTE_REPLACER]) . ')'; return self::REGEXP_DELIMITER . $combinedRegexps . self::REGEXP_DELIMITER; } }
}
Expand Down
24 changes: 12 additions & 12 deletions tests/CombinedRegexpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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' => [
[
Expand Down Expand Up @@ -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(),
Expand All @@ -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' => [
[
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit bbf5cb8

Please sign in to comment.