Skip to content

Commit e105dfe

Browse files
authored
Code cleanup (#13)
* PSR2 fix on ReplaceCallbackTest * Remove PREG_OFFSET_CAPTURE check code duplication
1 parent b2d040d commit e105dfe

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

src/Preg.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ class Preg
2626
*/
2727
public static function match(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): int
2828
{
29-
if (($flags & PREG_OFFSET_CAPTURE) !== 0) {
30-
throw new \InvalidArgumentException('PREG_OFFSET_CAPTURE is not supported as it changes the type of $matches, use matchWithOffsets() instead');
31-
}
29+
self::checkOffsetCapture($flags, 'matchWithOffsets');
3230

3331
$result = preg_match($pattern, $subject, $matches, $flags | PREG_UNMATCHED_AS_NULL, $offset);
3432
if ($result === false) {
@@ -66,9 +64,7 @@ public static function matchWithOffsets(string $pattern, string $subject, ?array
6664
*/
6765
public static function matchAll(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): int
6866
{
69-
if (($flags & PREG_OFFSET_CAPTURE) !== 0) {
70-
throw new \InvalidArgumentException('PREG_OFFSET_CAPTURE is not supported as it changes the type of $matches, use matchAllWithOffsets() instead');
71-
}
67+
self::checkOffsetCapture($flags, 'matchAllWithOffsets');
7268

7369
if (($flags & PREG_SET_ORDER) !== 0) {
7470
throw new \InvalidArgumentException('PREG_SET_ORDER is not supported as it changes the type of $matches');
@@ -280,4 +276,11 @@ public static function isMatchAllWithOffsets(string $pattern, string $subject, ?
280276
{
281277
return (bool) static::matchAllWithOffsets($pattern, $subject, $matches, $flags, $offset);
282278
}
279+
280+
protected static function checkOffsetCapture(int $flags, string $useFunctionName): void
281+
{
282+
if (($flags & PREG_OFFSET_CAPTURE) !== 0) {
283+
throw new \InvalidArgumentException('PREG_OFFSET_CAPTURE is not supported as it changes the type of $matches, use ' . $useFunctionName . '() instead');
284+
}
285+
}
283286
}

src/Regex.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ public static function isMatch(string $pattern, string $subject, int $offset = 0
2727
*/
2828
public static function match(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchResult
2929
{
30-
if (($flags & PREG_OFFSET_CAPTURE) !== 0) {
31-
throw new \InvalidArgumentException('PREG_OFFSET_CAPTURE is not supported as it changes the return type, use matchWithOffsets() instead');
32-
}
30+
self::checkOffsetCapture($flags);
3331

3432
$count = Preg::match($pattern, $subject, $matches, $flags, $offset);
3533

@@ -55,9 +53,7 @@ public static function matchWithOffsets(string $pattern, string $subject, int $f
5553
*/
5654
public static function matchAll(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchAllResult
5755
{
58-
if (($flags & PREG_OFFSET_CAPTURE) !== 0) {
59-
throw new \InvalidArgumentException('PREG_OFFSET_CAPTURE is not supported as it changes the return type, use matchAllWithOffsets() instead');
60-
}
56+
self::checkOffsetCapture($flags);
6157

6258
if (($flags & PREG_SET_ORDER) !== 0) {
6359
throw new \InvalidArgumentException('PREG_SET_ORDER is not supported as it changes the return type');
@@ -115,4 +111,11 @@ public static function replaceCallbackArray(array $pattern, $subject, int $limit
115111

116112
return new ReplaceResult($count, $result);
117113
}
114+
115+
protected static function checkOffsetCapture(int $flags): void
116+
{
117+
if (($flags & PREG_OFFSET_CAPTURE) !== 0) {
118+
throw new \InvalidArgumentException('PREG_OFFSET_CAPTURE is not supported as it changes the return type, use matchWithOffsets() instead');
119+
}
120+
}
118121
}

tests/RegexTests/ReplaceCallbackTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testSuccess(): void
3636

3737
public function testFailure(): void
3838
{
39-
$result = Regex::replaceCallback('{abc}', function ($match) {
39+
$result = Regex::replaceCallback('{abc}', function ($match) {
4040
return '('.$match[0].')';
4141
}, 'def');
4242

0 commit comments

Comments
 (0)