Skip to content

Commit

Permalink
Improve PHPStan types
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Nov 3, 2022
1 parent 5330017 commit 562ca94
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
23 changes: 14 additions & 9 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
parameters:
ignoreErrors:
-
message: "#^Function preg_replace_callback invoked with 6 parameters, 3\\-5 required\\.$#"
count: 1
path: src/Preg.php
ignoreErrors:
-
message: "#^Function preg_replace_callback invoked with 6 parameters, 3\\-5 required\\.$#"
count: 1
path: src/Preg.php

-
message: "#^Function preg_replace_callback_array invoked with 5 parameters, 2\\-4 required\\.$#"
count: 1
path: src/Preg.php
-
message: "#^Function preg_replace_callback_array invoked with 5 parameters, 2\\-4 required\\.$#"
count: 1
path: src/Preg.php

-
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 or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
*/
public static function replaceCallback($pattern, callable $replacement, $subject, int $limit = -1, int &$count = null, int $flags = 0): string
{
Expand All @@ -151,10 +152,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 or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
*/
public static function replaceCallbackArray(array $pattern, $subject, int $limit = -1, int &$count = null, int $flags = 0): string
{
Expand All @@ -180,7 +181,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 @@ -198,7 +199,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 @@ -216,7 +217,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 @@ -232,7 +233,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 @@ -242,7 +243,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 @@ -254,7 +255,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 @@ -268,7 +269,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 or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
*/
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 or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
* @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
*/
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 562ca94

Please sign in to comment.