Skip to content

Commit

Permalink
Merge branch '2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Nov 3, 2022
2 parents 3c1d8ca + 562ca94 commit 6af1968
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
6 changes: 5 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
parameters:
ignoreErrors:
ignoreErrors:
-
message: "#^Offset 0 does not exist on string\\|null\\.$#"
count: 1
path: tests/PregTests/ReplaceCallbackTest.php
29 changes: 15 additions & 14 deletions src/Preg.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Preg
/**
* @param non-empty-string $pattern
* @param array<string|null> $matches Set by method
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
* @return 0|1
*/
public static function match(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): int
Expand All @@ -41,7 +41,7 @@ public static function match(string $pattern, string $subject, ?array &$matches
*
* @param non-empty-string $pattern
* @param array<int|string, array{string|null, int}> $matches Set by method
* @param int $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_UNMATCHED_AS_NULL and PREG_OFFSET_CAPTURE are always set, no other flags are supported
* @return 0|1
*
* @phpstan-param array<int|string, array{string|null, int<-1, max>}> $matches
Expand All @@ -59,7 +59,7 @@ public static function matchWithOffsets(string $pattern, string $subject, ?array
/**
* @param non-empty-string $pattern
* @param array<int|string, list<string|null>> $matches Set by method
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_SET_ORDER> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
* @return 0|positive-int
*/
public static function matchAll(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): int
Expand All @@ -83,7 +83,7 @@ public static function matchAll(string $pattern, string $subject, ?array &$match
*
* @param non-empty-string $pattern
* @param array<int|string, list<array{string|null, int}>> $matches Set by method
* @param int $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
* @return 0|positive-int
*
* @phpstan-param array<int|string, list<array{string|null, int<-1, max>}>> $matches
Expand Down Expand Up @@ -124,9 +124,10 @@ public static function replace($pattern, $replacement, $subject, int $limit = -1

/**
* @param string|string[] $pattern
* @param callable(array<int|string, string|null>): string $replacement
* @param string $subject
* @param int $count Set by method
* @param int $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
*/
public static function replaceCallback($pattern, callable $replacement, $subject, int $limit = -1, int &$count = null, int $flags = 0): string
{
Expand All @@ -147,10 +148,10 @@ public static function replaceCallback($pattern, callable $replacement, $subject
}

/**
* @param array<string, callable> $pattern
* @param array<string, callable(array<int|string, string|null>): string> $pattern
* @param string $subject
* @param int $count Set by method
* @param int $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
*/
public static function replaceCallbackArray(array $pattern, $subject, int $limit = -1, int &$count = null, int $flags = 0): string
{
Expand All @@ -172,7 +173,7 @@ public static function replaceCallbackArray(array $pattern, $subject, int $limit
}

/**
* @param int $flags PREG_SPLIT_NO_EMPTY or PREG_SPLIT_DELIM_CAPTURE
* @param int-mask<PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_OFFSET_CAPTURE> $flags PREG_SPLIT_NO_EMPTY or PREG_SPLIT_DELIM_CAPTURE
* @return list<string>
*/
public static function split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array
Expand All @@ -190,7 +191,7 @@ public static function split(string $pattern, string $subject, int $limit = -1,
}

/**
* @param int $flags PREG_SPLIT_NO_EMPTY or PREG_SPLIT_DELIM_CAPTURE, PREG_SPLIT_OFFSET_CAPTURE is always set
* @param int-mask<PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_OFFSET_CAPTURE> $flags PREG_SPLIT_NO_EMPTY or PREG_SPLIT_DELIM_CAPTURE, PREG_SPLIT_OFFSET_CAPTURE is always set
* @return list<array{string, int}>
* @phpstan-return list<array{string, int<0, max>}>
*/
Expand All @@ -208,7 +209,7 @@ public static function splitWithOffsets(string $pattern, string $subject, int $l
* @template T of string|\Stringable
* @param string $pattern
* @param array<T> $array
* @param int $flags PREG_GREP_INVERT
* @param int-mask<PREG_GREP_INVERT> $flags PREG_GREP_INVERT
* @return array<T>
*/
public static function grep(string $pattern, array $array, int $flags = 0): array
Expand All @@ -224,7 +225,7 @@ public static function grep(string $pattern, array $array, int $flags = 0): arra
/**
* @param non-empty-string $pattern
* @param array<string|null> $matches Set by method
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
*/
public static function isMatch(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): bool
{
Expand All @@ -234,7 +235,7 @@ public static function isMatch(string $pattern, string $subject, ?array &$matche
/**
* @param non-empty-string $pattern
* @param array<int|string, list<string|null>> $matches Set by method
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
*/
public static function isMatchAll(string $pattern, string $subject, ?array &$matches = null, int $flags = 0, int $offset = 0): bool
{
Expand All @@ -246,7 +247,7 @@ public static function isMatchAll(string $pattern, string $subject, ?array &$mat
*
* @param non-empty-string $pattern
* @param array<int|string, array{string|null, int}> $matches Set by method
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
*
* @phpstan-param array<int|string, array{string|null, int<-1, max>}> $matches
*/
Expand All @@ -260,7 +261,7 @@ public static function isMatchWithOffsets(string $pattern, string $subject, ?arr
*
* @param non-empty-string $pattern
* @param array<int|string, list<array{string|null, int}>> $matches Set by method
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
*
* @phpstan-param array<int|string, list<array{string|null, int<-1, max>}>> $matches
*/
Expand Down
15 changes: 8 additions & 7 deletions src/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function isMatch(string $pattern, string $subject, int $offset = 0

/**
* @param non-empty-string $pattern
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
* @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
*/
public static function match(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchResult
{
Expand All @@ -38,7 +38,7 @@ public static function match(string $pattern, string $subject, int $flags = 0, i
* Runs preg_match with PREG_OFFSET_CAPTURE
*
* @param non-empty-string $pattern
* @param int $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
*/
public static function matchWithOffsets(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchWithOffsetsResult
{
Expand All @@ -49,7 +49,7 @@ public static function matchWithOffsets(string $pattern, string $subject, int $f

/**
* @param non-empty-string $pattern
* @param int $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_SET_ORDER> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
*/
public static function matchAll(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchAllResult
{
Expand All @@ -68,7 +68,7 @@ public static function matchAll(string $pattern, string $subject, int $flags = 0
* Runs preg_match_all with PREG_OFFSET_CAPTURE
*
* @param non-empty-string $pattern
* @param int $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
*/
public static function matchAllWithOffsets(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchAllWithOffsetsResult
{
Expand All @@ -90,8 +90,9 @@ public static function replace($pattern, $replacement, $subject, int $limit = -1

/**
* @param string|string[] $pattern
* @param callable(array<int|string, string|null>): string $replacement
* @param string $subject
* @param int $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
*/
public static function replaceCallback($pattern, callable $replacement, $subject, int $limit = -1, int $flags = 0): ReplaceResult
{
Expand All @@ -101,9 +102,9 @@ public static function replaceCallback($pattern, callable $replacement, $subject
}

/**
* @param array<string, callable> $pattern
* @param array<string, callable(array<int|string, string|null>): string> $pattern
* @param string $subject
* @param int $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
*/
public static function replaceCallbackArray(array $pattern, $subject, int $limit = -1, int $flags = 0): ReplaceResult
{
Expand Down
10 changes: 10 additions & 0 deletions tests/PregTests/ReplaceCallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ public function testSuccess(): void
self::assertSame('abc(d)', $result);
}

public function testSuccessWithOffset(): void
{
$result = Preg::replaceCallback('{(?P<m>d)}', function ($match) {
return '('.$match[0][0].')';
}, 'abcd', -1, $count, PREG_OFFSET_CAPTURE);

self::assertSame(1, $count);
self::assertSame('abc(d)', $result);
}

public function testSuccessNoRef(): void
{
$result = Preg::replaceCallback('{(?P<m>d)}', function ($match) {
Expand Down

0 comments on commit 6af1968

Please sign in to comment.