From 73f176b85963b580817b4637468e6bf41a30df38 Mon Sep 17 00:00:00 2001 From: azjezz Date: Mon, 8 Nov 2021 01:16:29 +0100 Subject: [PATCH] feat(str): use enum for encoding Signed-off-by: azjezz --- config/.phpcs.xml | 1 + docs/component/encoding-base64.md | 2 +- docs/component/str.md | 80 ++++++++++++++------------ src/Psl/Encoding/Hex/decode.php | 3 +- src/Psl/Internal/Loader.php | 1 + src/Psl/Internal/validate_offset.php | 2 +- src/Psl/Str/Encoding.php | 86 ++++++++++++++++++++++++++++ src/Psl/Str/after.php | 3 +- src/Psl/Str/after_ci.php | 3 +- src/Psl/Str/after_last.php | 3 +- src/Psl/Str/after_last_ci.php | 3 +- src/Psl/Str/before.php | 3 +- src/Psl/Str/before_ci.php | 3 +- src/Psl/Str/before_last.php | 3 +- src/Psl/Str/before_last_ci.php | 3 +- src/Psl/Str/capitalize.php | 6 +- src/Psl/Str/capitalize_words.php | 13 ++--- src/Psl/Str/chr.php | 18 ++---- src/Psl/Str/chunk.php | 15 +++-- src/Psl/Str/contains.php | 5 +- src/Psl/Str/contains_ci.php | 3 +- src/Psl/Str/ends_with.php | 7 +-- src/Psl/Str/ends_with_ci.php | 7 +-- src/Psl/Str/fold.php | 5 +- src/Psl/Str/length.php | 13 ++--- src/Psl/Str/lowercase.php | 9 ++- src/Psl/Str/ord.php | 9 ++- src/Psl/Str/pad_left.php | 5 +- src/Psl/Str/pad_right.php | 5 +- src/Psl/Str/replace.php | 6 +- src/Psl/Str/replace_ci.php | 2 +- src/Psl/Str/replace_every.php | 6 +- src/Psl/Str/replace_every_ci.php | 6 +- src/Psl/Str/reverse.php | 2 +- src/Psl/Str/search.php | 10 ++-- src/Psl/Str/search_ci.php | 10 ++-- src/Psl/Str/search_last.php | 9 ++- src/Psl/Str/search_last_ci.php | 9 ++- src/Psl/Str/slice.php | 10 ++-- src/Psl/Str/splice.php | 3 +- src/Psl/Str/split.php | 2 +- src/Psl/Str/starts_with.php | 7 +-- src/Psl/Str/starts_with_ci.php | 7 +-- src/Psl/Str/strip_prefix.php | 2 +- src/Psl/Str/strip_suffix.php | 6 +- src/Psl/Str/truncate.php | 9 ++- src/Psl/Str/uppercase.php | 13 ++--- src/Psl/Str/width.php | 9 ++- src/Psl/Str/wrap.php | 5 +- tests/unit/Str/AfterCiTest.php | 33 +++++------ tests/unit/Str/AfterLastCiTest.php | 33 ++++++----- tests/unit/Str/AfterLastTest.php | 33 ++++++----- tests/unit/Str/AfterTest.php | 33 ++++++----- tests/unit/Str/BeforeCiTest.php | 47 ++++++++------- tests/unit/Str/BeforeLastCiTest.php | 43 +++++++------- tests/unit/Str/BeforeLastTest.php | 45 +++++++-------- tests/unit/Str/BeforeTest.php | 45 +++++++-------- 57 files changed, 410 insertions(+), 354 deletions(-) create mode 100644 src/Psl/Str/Encoding.php diff --git a/config/.phpcs.xml b/config/.phpcs.xml index e04428b9..f7de73e0 100644 --- a/config/.phpcs.xml +++ b/config/.phpcs.xml @@ -8,6 +8,7 @@ ../tests */src/Psl/File/(WriteMode|LockType).php + */src/Psl/Str/Encoding.php diff --git a/docs/component/encoding-base64.md b/docs/component/encoding-base64.md index 8d31ae4d..d1106fd4 100644 --- a/docs/component/encoding-base64.md +++ b/docs/component/encoding-base64.md @@ -12,7 +12,7 @@ #### `Functions` -- [decode](./../../src/Psl/Encoding/Base64/decode.php#L27) +- [decode](./../../src/Psl/Encoding/Base64/decode.php#L26) - [encode](./../../src/Psl/Encoding/Base64/encode.php#L18) diff --git a/docs/component/str.md b/docs/component/str.md index 3cc4a69e..3c51ee3f 100644 --- a/docs/component/str.md +++ b/docs/component/str.md @@ -17,63 +17,67 @@ #### `Functions` -- [after](./../../src/Psl/Str/after.php#L15) -- [after_ci](./../../src/Psl/Str/after_ci.php#L15) -- [after_last](./../../src/Psl/Str/after_last.php#L15) -- [after_last_ci](./../../src/Psl/Str/after_last_ci.php#L15) -- [before](./../../src/Psl/Str/before.php#L15) -- [before_ci](./../../src/Psl/Str/before_ci.php#L15) -- [before_last](./../../src/Psl/Str/before_last.php#L15) -- [before_last_ci](./../../src/Psl/Str/before_last_ci.php#L15) -- [capitalize](./../../src/Psl/Str/capitalize.php#L33) -- [capitalize_words](./../../src/Psl/Str/capitalize_words.php#L35) -- [chr](./../../src/Psl/Str/chr.php#L27) -- [chunk](./../../src/Psl/Str/chunk.php#L40) +- [after](./../../src/Psl/Str/after.php#L14) +- [after_ci](./../../src/Psl/Str/after_ci.php#L14) +- [after_last](./../../src/Psl/Str/after_last.php#L14) +- [after_last_ci](./../../src/Psl/Str/after_last_ci.php#L14) +- [before](./../../src/Psl/Str/before.php#L14) +- [before_ci](./../../src/Psl/Str/before_ci.php#L14) +- [before_last](./../../src/Psl/Str/before_last.php#L14) +- [before_last_ci](./../../src/Psl/Str/before_last_ci.php#L14) +- [capitalize](./../../src/Psl/Str/capitalize.php#L29) +- [capitalize_words](./../../src/Psl/Str/capitalize_words.php#L30) +- [chr](./../../src/Psl/Str/chr.php#L22) +- [chunk](./../../src/Psl/Str/chunk.php#L39) - [concat](./../../src/Psl/Str/concat.php#L20) -- [contains](./../../src/Psl/Str/contains.php#L42) -- [contains_ci](./../../src/Psl/Str/contains_ci.php#L42) +- [contains](./../../src/Psl/Str/contains.php#L41) +- [contains_ci](./../../src/Psl/Str/contains_ci.php#L41) - [convert_encoding](./../../src/Psl/Str/convert_encoding.php#L19) - [detect_encoding](./../../src/Psl/Str/detect_encoding.php#L21) -- [ends_with](./../../src/Psl/Str/ends_with.php#L39) -- [ends_with_ci](./../../src/Psl/Str/ends_with_ci.php#L39) -- [fold](./../../src/Psl/Str/fold.php#L22) +- [ends_with](./../../src/Psl/Str/ends_with.php#L35) +- [ends_with_ci](./../../src/Psl/Str/ends_with_ci.php#L35) +- [fold](./../../src/Psl/Str/fold.php#L19) - [format](./../../src/Psl/Str/format.php#L26) - [format_number](./../../src/Psl/Str/format_number.php#L19) - [from_code_points](./../../src/Psl/Str/from_code_points.php#L20) - [is_empty](./../../src/Psl/Str/is_empty.php#L29) - [is_utf8](./../../src/Psl/Str/is_utf8.php#L14) - [join](./../../src/Psl/Str/join.php#L27) -- [length](./../../src/Psl/Str/length.php#L30) +- [length](./../../src/Psl/Str/length.php#L25) - [levenshtein](./../../src/Psl/Str/levenshtein.php#L29) -- [lowercase](./../../src/Psl/Str/lowercase.php#L35) +- [lowercase](./../../src/Psl/Str/lowercase.php#L34) - [metaphone](./../../src/Psl/Str/metaphone.php#L23) -- [ord](./../../src/Psl/Str/ord.php#L27) -- [pad_left](./../../src/Psl/Str/pad_left.php#L38) -- [pad_right](./../../src/Psl/Str/pad_right.php#L38) +- [ord](./../../src/Psl/Str/ord.php#L26) +- [pad_left](./../../src/Psl/Str/pad_left.php#L37) +- [pad_right](./../../src/Psl/Str/pad_right.php#L37) - [repeat](./../../src/Psl/Str/repeat.php#L28) -- [replace](./../../src/Psl/Str/replace.php#L19) +- [replace](./../../src/Psl/Str/replace.php#L15) - [replace_ci](./../../src/Psl/Str/replace_ci.php#L20) -- [replace_every](./../../src/Psl/Str/replace_every.php#L19) -- [replace_every_ci](./../../src/Psl/Str/replace_every_ci.php#L19) +- [replace_every](./../../src/Psl/Str/replace_every.php#L15) +- [replace_every_ci](./../../src/Psl/Str/replace_every_ci.php#L15) - [reverse](./../../src/Psl/Str/reverse.php#L14) -- [search](./../../src/Psl/Str/search.php#L25) -- [search_ci](./../../src/Psl/Str/search_ci.php#L25) -- [search_last](./../../src/Psl/Str/search_last.php#L25) -- [search_last_ci](./../../src/Psl/Str/search_last_ci.php#L25) -- [slice](./../../src/Psl/Str/slice.php#L25) -- [splice](./../../src/Psl/Str/splice.php#L22) +- [search](./../../src/Psl/Str/search.php#L23) +- [search_ci](./../../src/Psl/Str/search_ci.php#L23) +- [search_last](./../../src/Psl/Str/search_last.php#L24) +- [search_last_ci](./../../src/Psl/Str/search_last_ci.php#L24) +- [slice](./../../src/Psl/Str/slice.php#L23) +- [splice](./../../src/Psl/Str/splice.php#L21) - [split](./../../src/Psl/Str/split.php#L24) -- [starts_with](./../../src/Psl/Str/starts_with.php#L16) -- [starts_with_ci](./../../src/Psl/Str/starts_with_ci.php#L16) +- [starts_with](./../../src/Psl/Str/starts_with.php#L12) +- [starts_with_ci](./../../src/Psl/Str/starts_with_ci.php#L12) - [strip_prefix](./../../src/Psl/Str/strip_prefix.php#L17) -- [strip_suffix](./../../src/Psl/Str/strip_suffix.php#L17) +- [strip_suffix](./../../src/Psl/Str/strip_suffix.php#L13) - [to_int](./../../src/Psl/Str/to_int.php#L12) - [trim](./../../src/Psl/Str/trim.php#L18) - [trim_left](./../../src/Psl/Str/trim_left.php#L18) - [trim_right](./../../src/Psl/Str/trim_right.php#L18) -- [truncate](./../../src/Psl/Str/truncate.php#L29) -- [uppercase](./../../src/Psl/Str/uppercase.php#L19) -- [width](./../../src/Psl/Str/width.php#L19) -- [wrap](./../../src/Psl/Str/wrap.php#L23) +- [truncate](./../../src/Psl/Str/truncate.php#L28) +- [uppercase](./../../src/Psl/Str/uppercase.php#L14) +- [width](./../../src/Psl/Str/width.php#L18) +- [wrap](./../../src/Psl/Str/wrap.php#L22) + +#### `Enums` + +- [Encoding](./../../src/Psl/Str/Encoding.php#L7) diff --git a/src/Psl/Encoding/Hex/decode.php b/src/Psl/Encoding/Hex/decode.php index ddf226f1..fdd6fd27 100644 --- a/src/Psl/Encoding/Hex/decode.php +++ b/src/Psl/Encoding/Hex/decode.php @@ -27,8 +27,7 @@ function decode(string $hexadecimal): string ); } - /** @psalm-suppress MissingThrowsDocblock */ - $hex_len = Str\length($hexadecimal, '8bit'); + $hex_len = Str\length($hexadecimal, Str\Encoding::ASCII_8BIT); if (($hex_len & 1) !== 0) { throw new Exception\RangeException( 'Expected an even number of hexadecimal characters.', diff --git a/src/Psl/Internal/Loader.php b/src/Psl/Internal/Loader.php index d8bb43bc..5c713d10 100644 --- a/src/Psl/Internal/Loader.php +++ b/src/Psl/Internal/Loader.php @@ -648,6 +648,7 @@ final class Loader public const ENUMS = [ 'Psl\File\LockType', 'Psl\File\WriteMode', + 'Psl\Str\Encoding', ]; public const TYPE_CONSTANTS = 1; diff --git a/src/Psl/Internal/validate_offset.php b/src/Psl/Internal/validate_offset.php index 252ddbef..f06cc492 100644 --- a/src/Psl/Internal/validate_offset.php +++ b/src/Psl/Internal/validate_offset.php @@ -20,7 +20,7 @@ * * @return ($assert is true ? bool : int) */ -function validate_offset(int $offset, int $length, bool $assert = false) +function validate_offset(int $offset, int $length, bool $assert = false): int|bool { $original_offset = $offset; diff --git a/src/Psl/Str/Encoding.php b/src/Psl/Str/Encoding.php new file mode 100644 index 00000000..64b16673 --- /dev/null +++ b/src/Psl/Str/Encoding.php @@ -0,0 +1,86 @@ + Str('1337) * * @pure - * - * @throws Exception\InvariantViolationException If an invalid $encoding is provided. */ -function capitalize(string $string, ?string $encoding = null): string +function capitalize(string $string, Encoding $encoding = Encoding::UTF_8): string { if ('' === $string) { return ''; diff --git a/src/Psl/Str/capitalize_words.php b/src/Psl/Str/capitalize_words.php index 79cd0bc0..1e9893cc 100644 --- a/src/Psl/Str/capitalize_words.php +++ b/src/Psl/Str/capitalize_words.php @@ -4,9 +4,6 @@ namespace Psl\Str; -use Psl\Exception; -use Psl\Internal; - use function mb_convert_case; use const MB_CASE_TITLE; @@ -29,10 +26,12 @@ * => Str('مرحبا بكم') * * @pure - * - * @throws Exception\InvariantViolationException If an invalid $encoding is provided. */ -function capitalize_words(string $string, ?string $encoding = null): string +function capitalize_words(string $string, Encoding $encoding = Encoding::UTF_8): string { - return mb_convert_case($string, MB_CASE_TITLE, Internal\internal_encoding($encoding)); + /** + * @psalm-suppress UndefinedPropertyFetch + * @psalm-suppress MixedArgument + */ + return mb_convert_case($string, MB_CASE_TITLE, $encoding->value); } diff --git a/src/Psl/Str/chr.php b/src/Psl/Str/chr.php index 7e5fedc6..184e49ea 100644 --- a/src/Psl/Str/chr.php +++ b/src/Psl/Str/chr.php @@ -4,9 +4,6 @@ namespace Psl\Str; -use Psl; -use Psl\Internal; - use function mb_chr; /** @@ -21,15 +18,12 @@ * => Str('ل') * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function chr(int $codepoint, ?string $encoding = null): string +function chr(int $codepoint, Encoding $encoding = Encoding::UTF_8): string { - $char = mb_chr($codepoint, Internal\internal_encoding($encoding)); - - /** @psalm-suppress MissingThrowsDocblock */ - Psl\invariant(is_string($char), 'Unexpected Error.'); - - return $char; + /** + * @psalm-suppress UndefinedPropertyFetch + * @psalm-suppress MixedArgument + */ + return (string) mb_chr($codepoint, $encoding->value); } diff --git a/src/Psl/Str/chunk.php b/src/Psl/Str/chunk.php index 5985eeee..9a5bdbd7 100644 --- a/src/Psl/Str/chunk.php +++ b/src/Psl/Str/chunk.php @@ -5,7 +5,6 @@ namespace Psl\Str; use Psl; -use Psl\Internal; /** * Returns an array containing the string split into chunks of the given size. @@ -27,7 +26,7 @@ * @param int $chunk_length maximum length of the chunk * * @throws Psl\Exception\InvariantViolationException If the given $chunk_length is negative or above - * the limit ( 65535 ), or an invalid $encoding is provided. + * the limit ( 65535 ). * * @return list if $chunk_length parameter is specified, the returned array will be broken down * into chunks with each being $chunk_length in length, otherwise each chunk will be @@ -37,7 +36,7 @@ * * @pure */ -function chunk(string $string, int $chunk_length = 1, ?string $encoding = null): array +function chunk(string $string, int $chunk_length = 1, Encoding $encoding = Encoding::UTF_8): array { Psl\invariant($chunk_length >= 1, 'Expected a non-negative chunk size.'); if ('' === $string) { @@ -46,6 +45,12 @@ function chunk(string $string, int $chunk_length = 1, ?string $encoding = null): Psl\invariant(65535 >= $chunk_length, 'Maximum chunk length must not exceed 65535.'); - /** @var list */ - return mb_str_split($string, $chunk_length, Internal\internal_encoding($encoding)); + + /** + * @psalm-suppress UndefinedPropertyFetch + * @psalm-suppress MixedArgument + * + * @var list + */ + return mb_str_split($string, $chunk_length, $encoding->value); } diff --git a/src/Psl/Str/contains.php b/src/Psl/Str/contains.php index ef9cc030..655a5d76 100644 --- a/src/Psl/Str/contains.php +++ b/src/Psl/Str/contains.php @@ -36,10 +36,9 @@ * * @pure * - * @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds, or - * an invalid $encoding is provided. + * @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds. */ -function contains(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): bool +function contains(string $haystack, string $needle, int $offset = 0, Encoding $encoding = Encoding::UTF_8): bool { if ('' === $needle) { return Psl\Internal\validate_offset($offset, length($haystack, $encoding), true); diff --git a/src/Psl/Str/contains_ci.php b/src/Psl/Str/contains_ci.php index 8fb2b61f..ecf7473c 100644 --- a/src/Psl/Str/contains_ci.php +++ b/src/Psl/Str/contains_ci.php @@ -37,9 +37,8 @@ * @pure * * @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds. - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function contains_ci(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): bool +function contains_ci(string $haystack, string $needle, int $offset = 0, Encoding $encoding = Encoding::UTF_8): bool { if ('' === $needle) { return Psl\Internal\validate_offset($offset, length($haystack, $encoding), true); diff --git a/src/Psl/Str/ends_with.php b/src/Psl/Str/ends_with.php index 045ef7b9..98343bc5 100644 --- a/src/Psl/Str/ends_with.php +++ b/src/Psl/Str/ends_with.php @@ -4,8 +4,6 @@ namespace Psl\Str; -use Psl; - /** * Returns whether the string ends with the given suffix. * @@ -33,10 +31,8 @@ * => Bool(false) * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function ends_with(string $string, string $suffix, ?string $encoding = null): bool +function ends_with(string $string, string $suffix, Encoding $encoding = Encoding::UTF_8): bool { if ($suffix === $string) { return true; @@ -48,6 +44,7 @@ function ends_with(string $string, string $suffix, ?string $encoding = null): bo return false; } + /** @psalm-suppress MissingThrowsDocblock */ $position = search_last($string, $suffix, 0, $encoding); if (null === $position) { return false; diff --git a/src/Psl/Str/ends_with_ci.php b/src/Psl/Str/ends_with_ci.php index 568ab593..d27c3b77 100644 --- a/src/Psl/Str/ends_with_ci.php +++ b/src/Psl/Str/ends_with_ci.php @@ -4,8 +4,6 @@ namespace Psl\Str; -use Psl; - /** * Returns whether the string ends with the given suffix (case-insensitive). * @@ -33,10 +31,8 @@ * => Bool(false) * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function ends_with_ci(string $string, string $suffix, ?string $encoding = null): bool +function ends_with_ci(string $string, string $suffix, Encoding $encoding = Encoding::UTF_8): bool { if ($suffix === $string) { return true; @@ -48,6 +44,7 @@ function ends_with_ci(string $string, string $suffix, ?string $encoding = null): return false; } + /** @psalm-suppress MissingThrowsDocblock */ $position = search_last_ci($string, $suffix, 0, $encoding); if (null === $position) { return false; diff --git a/src/Psl/Str/fold.php b/src/Psl/Str/fold.php index 06091e43..2004ba0a 100644 --- a/src/Psl/Str/fold.php +++ b/src/Psl/Str/fold.php @@ -4,7 +4,6 @@ namespace Psl\Str; -use Psl; use Psl\Internal; /** @@ -16,10 +15,8 @@ * => Str('ss') * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function fold(string $string, ?string $encoding = null): string +function fold(string $string, Encoding $encoding = Encoding::UTF_8): string { foreach (Internal\CASE_FOLD as $k => $v) { $string = replace($string, $k, $v, $encoding); diff --git a/src/Psl/Str/length.php b/src/Psl/Str/length.php index cf452e9b..73c93540 100644 --- a/src/Psl/Str/length.php +++ b/src/Psl/Str/length.php @@ -4,9 +4,6 @@ namespace Psl\Str; -use Psl; -use Psl\Internal; - use function mb_strlen; /** @@ -24,10 +21,12 @@ * => Int(4) * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function length(string $string, ?string $encoding = null): int +function length(string $string, Encoding $encoding = Encoding::UTF_8): int { - return mb_strlen($string, Internal\internal_encoding($encoding)); + /** + * @psalm-suppress UndefinedPropertyFetch + * @psalm-suppress MixedArgument + */ + return mb_strlen($string, $encoding->value); } diff --git a/src/Psl/Str/lowercase.php b/src/Psl/Str/lowercase.php index 698d1aea..5b84fd67 100644 --- a/src/Psl/Str/lowercase.php +++ b/src/Psl/Str/lowercase.php @@ -5,7 +5,6 @@ namespace Psl\Str; use Psl; -use Psl\Internal; use function mb_strtolower; @@ -32,7 +31,11 @@ * * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function lowercase(string $string, ?string $encoding = null): string +function lowercase(string $string, Encoding $encoding = Encoding::UTF_8): string { - return mb_strtolower($string, Internal\internal_encoding($encoding)); + /** + * @psalm-suppress UndefinedPropertyFetch + * @psalm-suppress MixedArgument + */ + return mb_strtolower($string, $encoding->value); } diff --git a/src/Psl/Str/ord.php b/src/Psl/Str/ord.php index 7b410506..77f01411 100644 --- a/src/Psl/Str/ord.php +++ b/src/Psl/Str/ord.php @@ -5,7 +5,6 @@ namespace Psl\Str; use Psl; -use Psl\Internal; use function mb_ord; @@ -24,7 +23,11 @@ * * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function ord(string $character, ?string $encoding = null): int +function ord(string $character, Encoding $encoding = Encoding::UTF_8): int { - return mb_ord($character, Internal\internal_encoding($encoding)); + /** + * @psalm-suppress UndefinedPropertyFetch + * @psalm-suppress MixedArgument + */ + return mb_ord($character, $encoding->value); } diff --git a/src/Psl/Str/pad_left.php b/src/Psl/Str/pad_left.php index 0b2a3929..2fb0be6e 100644 --- a/src/Psl/Str/pad_left.php +++ b/src/Psl/Str/pad_left.php @@ -32,10 +32,9 @@ * * @pure * - * @throws Psl\Exception\InvariantViolationException If the $pad_string is empty, a negative $total_length is given, - * or an invalid $encoding is provided. + * @throws Psl\Exception\InvariantViolationException If the $pad_string is empty, a negative $total_length is given. */ -function pad_left(string $string, int $total_length, string $pad_string = ' ', ?string $encoding = null): string +function pad_left(string $string, int $total_length, string $pad_string = ' ', Encoding $encoding = Encoding::UTF_8): string { Psl\invariant('' !== $pad_string, 'Expected a non-empty pad string.'); Psl\invariant($total_length >= 0, 'Expected a non-negative total length.'); diff --git a/src/Psl/Str/pad_right.php b/src/Psl/Str/pad_right.php index 7ad36b1d..503393a0 100644 --- a/src/Psl/Str/pad_right.php +++ b/src/Psl/Str/pad_right.php @@ -32,10 +32,9 @@ * * @pure * - * @throws Psl\Exception\InvariantViolationException If the $pad_string is empty, a negative $total_length is given, - * or an invalid $encoding is provided. + * @throws Psl\Exception\InvariantViolationException If the $pad_string is empty, a negative $total_length is given. */ -function pad_right(string $string, int $total_length, string $pad_string = ' ', ?string $encoding = null): string +function pad_right(string $string, int $total_length, string $pad_string = ' ', Encoding $encoding = Encoding::UTF_8): string { Psl\invariant('' !== $pad_string, 'Expected a non-empty pad string.'); Psl\invariant($total_length >= 0, 'Expected a non-negative total length.'); diff --git a/src/Psl/Str/replace.php b/src/Psl/Str/replace.php index 4bc72362..87687ed3 100644 --- a/src/Psl/Str/replace.php +++ b/src/Psl/Str/replace.php @@ -4,8 +4,6 @@ namespace Psl\Str; -use Psl; - use function str_replace; /** @@ -13,10 +11,8 @@ * `$replacement`. * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function replace(string $haystack, string $needle, string $replacement, ?string $encoding = null): string +function replace(string $haystack, string $needle, string $replacement, Encoding $encoding = Encoding::UTF_8): string { if ('' === $needle || null === search($haystack, $needle, 0, $encoding)) { return $haystack; diff --git a/src/Psl/Str/replace_ci.php b/src/Psl/Str/replace_ci.php index 3471f09a..55fbf96f 100644 --- a/src/Psl/Str/replace_ci.php +++ b/src/Psl/Str/replace_ci.php @@ -17,7 +17,7 @@ * * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function replace_ci(string $haystack, string $needle, string $replacement, ?string $encoding = null): string +function replace_ci(string $haystack, string $needle, string $replacement, Encoding $encoding = Encoding::UTF_8): string { if ('' === $needle || null === search_ci($haystack, $needle, 0, $encoding)) { return $haystack; diff --git a/src/Psl/Str/replace_every.php b/src/Psl/Str/replace_every.php index 51700c96..d00ca677 100644 --- a/src/Psl/Str/replace_every.php +++ b/src/Psl/Str/replace_every.php @@ -4,8 +4,6 @@ namespace Psl\Str; -use Psl; - /** * Returns the '$haystack' string with all occurrences of the keys of * `$replacements` replaced by the corresponding values. @@ -13,10 +11,8 @@ * @param array $replacements * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function replace_every(string $haystack, array $replacements, ?string $encoding = null): string +function replace_every(string $haystack, array $replacements, Encoding $encoding = Encoding::UTF_8): string { foreach ($replacements as $needle => $replacement) { $haystack = replace($haystack, $needle, $replacement, $encoding); diff --git a/src/Psl/Str/replace_every_ci.php b/src/Psl/Str/replace_every_ci.php index 8ecac311..f1b23e43 100644 --- a/src/Psl/Str/replace_every_ci.php +++ b/src/Psl/Str/replace_every_ci.php @@ -4,8 +4,6 @@ namespace Psl\Str; -use Psl; - /** * Returns the 'haystack' string with all occurrences of the keys of * `$replacements` replaced by the corresponding values (case-insensitive). @@ -13,10 +11,8 @@ * @param array $replacements * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function replace_every_ci(string $haystack, array $replacements, ?string $encoding = null): string +function replace_every_ci(string $haystack, array $replacements, Encoding $encoding = Encoding::UTF_8): string { foreach ($replacements as $needle => $replacement) { if ('' === $needle || null === search_ci($haystack, $needle, 0, $encoding)) { diff --git a/src/Psl/Str/reverse.php b/src/Psl/Str/reverse.php index 41be85f0..bbca1a9f 100644 --- a/src/Psl/Str/reverse.php +++ b/src/Psl/Str/reverse.php @@ -11,7 +11,7 @@ * * @psalm-pure */ -function reverse(string $string, ?string $encoding = null): string +function reverse(string $string, Encoding $encoding = Encoding::UTF_8): string { $chunks = chunk($string, encoding: $encoding); diff --git a/src/Psl/Str/search.php b/src/Psl/Str/search.php index d2f92dc0..5c229fc3 100644 --- a/src/Psl/Str/search.php +++ b/src/Psl/Str/search.php @@ -5,7 +5,6 @@ namespace Psl\Str; use Psl; -use Psl\Internal; use function mb_strpos; @@ -20,9 +19,8 @@ * @pure * * @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds. - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function search(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): ?int +function search(string $haystack, string $needle, int $offset = 0, Encoding $encoding = Encoding::UTF_8): ?int { if ('' === $needle) { return null; @@ -30,7 +28,11 @@ function search(string $haystack, string $needle, int $offset = 0, ?string $enco $offset = Psl\Internal\validate_offset($offset, length($haystack, $encoding)); - return false === ($pos = mb_strpos($haystack, $needle, $offset, Internal\internal_encoding($encoding))) ? + /** + * @psalm-suppress UndefinedPropertyFetch + * @psalm-suppress MixedArgument + */ + return false === ($pos = mb_strpos($haystack, $needle, $offset, $encoding->value)) ? null : $pos; } diff --git a/src/Psl/Str/search_ci.php b/src/Psl/Str/search_ci.php index d128946a..c47e25e1 100644 --- a/src/Psl/Str/search_ci.php +++ b/src/Psl/Str/search_ci.php @@ -5,7 +5,6 @@ namespace Psl\Str; use Psl; -use Psl\Internal; use function mb_stripos; @@ -20,9 +19,8 @@ * @pure * * @throws Psl\Exception\InvariantViolationException If $offset is out-of-bounds. - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function search_ci(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): ?int +function search_ci(string $haystack, string $needle, int $offset = 0, Encoding $encoding = Encoding::UTF_8): ?int { if ('' === $needle) { return null; @@ -30,7 +28,11 @@ function search_ci(string $haystack, string $needle, int $offset = 0, ?string $e $offset = Psl\Internal\validate_offset($offset, length($haystack, $encoding)); - return false === ($pos = mb_stripos($haystack, $needle, $offset, Internal\internal_encoding($encoding))) ? + /** + * @psalm-suppress UndefinedPropertyFetch + * @psalm-suppress MixedArgument + */ + return false === ($pos = mb_stripos($haystack, $needle, $offset, $encoding->value)) ? null : $pos; } diff --git a/src/Psl/Str/search_last.php b/src/Psl/Str/search_last.php index 852653e6..fa0c72de 100644 --- a/src/Psl/Str/search_last.php +++ b/src/Psl/Str/search_last.php @@ -5,7 +5,6 @@ namespace Psl\Str; use Psl; -use Psl\Internal; use function mb_strrpos; @@ -22,7 +21,7 @@ * @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds. * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function search_last(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): ?int +function search_last(string $haystack, string $needle, int $offset = 0, Encoding $encoding = Encoding::UTF_8): ?int { if ('' === $needle) { return null; @@ -31,7 +30,11 @@ function search_last(string $haystack, string $needle, int $offset = 0, ?string $haystack_length = length($haystack, $encoding); Psl\invariant($offset >= -$haystack_length && $offset <= $haystack_length, 'Offset is out-of-bounds.'); - return false === ($pos = mb_strrpos($haystack, $needle, $offset, Internal\internal_encoding($encoding))) ? + /** + * @psalm-suppress UndefinedPropertyFetch + * @psalm-suppress MixedArgument + */ + return false === ($pos = mb_strrpos($haystack, $needle, $offset, $encoding->value)) ? null : $pos; } diff --git a/src/Psl/Str/search_last_ci.php b/src/Psl/Str/search_last_ci.php index 37776b60..e11bec7f 100644 --- a/src/Psl/Str/search_last_ci.php +++ b/src/Psl/Str/search_last_ci.php @@ -5,7 +5,6 @@ namespace Psl\Str; use Psl; -use Psl\Internal; use function mb_strripos; @@ -22,7 +21,7 @@ * @throws Psl\Exception\InvariantViolationException If the offset is out-of-bounds. * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function search_last_ci(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): ?int +function search_last_ci(string $haystack, string $needle, int $offset = 0, Encoding $encoding = Encoding::UTF_8): ?int { if ('' === $needle) { return null; @@ -31,7 +30,11 @@ function search_last_ci(string $haystack, string $needle, int $offset = 0, ?stri $haystack_length = length($haystack, $encoding); Psl\invariant($offset >= -$haystack_length && $offset <= $haystack_length, 'Offset is out-of-bounds.'); - return false === ($pos = mb_strripos($haystack, $needle, $offset, Internal\internal_encoding($encoding))) ? + /** + * @psalm-suppress UndefinedPropertyFetch + * @psalm-suppress MixedArgument + */ + return false === ($pos = mb_strripos($haystack, $needle, $offset, $encoding->value)) ? null : $pos; } diff --git a/src/Psl/Str/slice.php b/src/Psl/Str/slice.php index 27b4cfff..482341ab 100644 --- a/src/Psl/Str/slice.php +++ b/src/Psl/Str/slice.php @@ -5,7 +5,6 @@ namespace Psl\Str; use Psl; -use Psl\Internal; use function mb_substr; @@ -20,9 +19,8 @@ * @pure * * @throws Psl\Exception\InvariantViolationException If a negative $length is given. - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function slice(string $string, int $offset, ?int $length = null, ?string $encoding = null): string +function slice(string $string, int $offset, ?int $length = null, Encoding $encoding = Encoding::UTF_8): string { Psl\invariant(null === $length || $length >= 0, 'Expected a non-negative length.'); $string_length = length($string, $encoding); @@ -32,5 +30,9 @@ function slice(string $string, int $offset, ?int $length = null, ?string $encodi return $string; } - return (string) mb_substr($string, $offset, $length, Internal\internal_encoding($encoding)); + /** + * @psalm-suppress UndefinedPropertyFetch + * @psalm-suppress MixedArgument + */ + return (string) mb_substr($string, $offset, $length, $encoding->value); } diff --git a/src/Psl/Str/splice.php b/src/Psl/Str/splice.php index 18ff09cf..055aba00 100644 --- a/src/Psl/Str/splice.php +++ b/src/Psl/Str/splice.php @@ -17,14 +17,13 @@ * @pure * * @throws Psl\Exception\InvariantViolationException If a negative $length is given. - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ function splice( string $string, string $replacement, int $offset = 0, ?int $length = null, - ?string $encoding = null + Encoding $encoding = Encoding::UTF_8 ): string { Psl\invariant(null === $length || $length >= 0, 'Expected a non-negative length.'); $total_length = length($string, $encoding); diff --git a/src/Psl/Str/split.php b/src/Psl/Str/split.php index 5f0b898d..6fabd07f 100644 --- a/src/Psl/Str/split.php +++ b/src/Psl/Str/split.php @@ -21,7 +21,7 @@ * * @pure */ -function split(string $string, string $delimiter, ?int $limit = null, ?string $encoding = null): array +function split(string $string, string $delimiter, ?int $limit = null, Encoding $encoding = Encoding::UTF_8): array { Psl\invariant(null === $limit || $limit >= 1, 'Expected a non-negative limit'); if ('' === $delimiter) { diff --git a/src/Psl/Str/starts_with.php b/src/Psl/Str/starts_with.php index 97c9f489..c0e80232 100644 --- a/src/Psl/Str/starts_with.php +++ b/src/Psl/Str/starts_with.php @@ -4,16 +4,13 @@ namespace Psl\Str; -use Psl; - /** * Returns whether the string starts with the given prefix. * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function starts_with(string $string, string $prefix, ?string $encoding = null): bool +function starts_with(string $string, string $prefix, Encoding $encoding = Encoding::UTF_8): bool { + /** @psalm-suppress MissingThrowsDocblock */ return 0 === search($string, $prefix, 0, $encoding); } diff --git a/src/Psl/Str/starts_with_ci.php b/src/Psl/Str/starts_with_ci.php index df30b2a7..ebf7e5f9 100644 --- a/src/Psl/Str/starts_with_ci.php +++ b/src/Psl/Str/starts_with_ci.php @@ -4,16 +4,13 @@ namespace Psl\Str; -use Psl; - /** * Returns whether the string starts with the given prefix (case-insensitive). * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function starts_with_ci(string $string, string $prefix, ?string $encoding = null): bool +function starts_with_ci(string $string, string $prefix, Encoding $encoding = Encoding::UTF_8): bool { + /** @psalm-suppress MissingThrowsDocblock */ return 0 === search_ci($string, $prefix, 0, $encoding); } diff --git a/src/Psl/Str/strip_prefix.php b/src/Psl/Str/strip_prefix.php index 9082ed6b..4564ce40 100644 --- a/src/Psl/Str/strip_prefix.php +++ b/src/Psl/Str/strip_prefix.php @@ -14,7 +14,7 @@ * * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function strip_prefix(string $string, string $prefix, ?string $encoding = null): string +function strip_prefix(string $string, string $prefix, Encoding $encoding = Encoding::UTF_8): string { if ('' === $prefix || !starts_with($string, $prefix, $encoding)) { return $string; diff --git a/src/Psl/Str/strip_suffix.php b/src/Psl/Str/strip_suffix.php index 5c1f7bb6..f8ac5b56 100644 --- a/src/Psl/Str/strip_suffix.php +++ b/src/Psl/Str/strip_suffix.php @@ -4,17 +4,13 @@ namespace Psl\Str; -use Psl; - /** * Returns the string with the given suffix removed, or the string itself if * it doesn't end with the suffix. * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function strip_suffix(string $string, string $suffix, ?string $encoding = null): string +function strip_suffix(string $string, string $suffix, Encoding $encoding = Encoding::UTF_8): string { if ('' === $suffix || !ends_with($string, $suffix, $encoding)) { return $string; diff --git a/src/Psl/Str/truncate.php b/src/Psl/Str/truncate.php index f04f2a6e..b3d1b24c 100644 --- a/src/Psl/Str/truncate.php +++ b/src/Psl/Str/truncate.php @@ -19,7 +19,6 @@ * when string is truncated * * @throws Psl\Exception\InvariantViolationException If the offset is out-of-bounds. - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. * * @return string The truncated string. If trim_marker is set, * trim_marker is appended to the return value. @@ -31,9 +30,13 @@ function truncate( int $offset, int $width, ?string $trim_marker = null, - ?string $encoding = null + Encoding $encoding = Encoding::UTF_8 ): string { $offset = Internal\validate_offset($offset, length($string, $encoding)); - return mb_strimwidth($string, $offset, $width, $trim_marker ?? '', Internal\internal_encoding($encoding)); + /** + * @psalm-suppress UndefinedPropertyFetch + * @psalm-suppress MixedArgument + */ + return mb_strimwidth($string, $offset, $width, $trim_marker ?? '', $encoding->value); } diff --git a/src/Psl/Str/uppercase.php b/src/Psl/Str/uppercase.php index b3c1c3f3..9e8ae621 100644 --- a/src/Psl/Str/uppercase.php +++ b/src/Psl/Str/uppercase.php @@ -4,19 +4,18 @@ namespace Psl\Str; -use Psl; -use Psl\Internal; - use function mb_strtoupper; /** * Returns the string with all alphabetic characters converted to uppercase. * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function uppercase(string $string, ?string $encoding = null): string +function uppercase(string $string, Encoding $encoding = Encoding::UTF_8): string { - return mb_strtoupper($string, Internal\internal_encoding($encoding)); + /** + * @psalm-suppress UndefinedPropertyFetch + * @psalm-suppress MixedArgument + */ + return mb_strtoupper($string, $encoding->value); } diff --git a/src/Psl/Str/width.php b/src/Psl/Str/width.php index fa170caf..0c14c656 100644 --- a/src/Psl/Str/width.php +++ b/src/Psl/Str/width.php @@ -5,7 +5,6 @@ namespace Psl\Str; use Psl; -use Psl\Internal; use function mb_strwidth; @@ -16,7 +15,11 @@ * * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ -function width(string $string, ?string $encoding = null): int +function width(string $string, Encoding $encoding = Encoding::UTF_8): int { - return mb_strwidth($string, Internal\internal_encoding($encoding)); + /** + * @psalm-suppress UndefinedPropertyFetch + * @psalm-suppress MixedArgument + */ + return mb_strwidth($string, $encoding->value); } diff --git a/src/Psl/Str/wrap.php b/src/Psl/Str/wrap.php index 484f8446..bb53f8aa 100644 --- a/src/Psl/Str/wrap.php +++ b/src/Psl/Str/wrap.php @@ -13,8 +13,7 @@ * @param bool $cut If the cut is set to true, the string is always wrapped at or before the specified width. * so if you have a word that is larger than the given width, it is broken apart. * - * @throws Psl\Exception\InvariantViolationException If $break is empty, $width is 0 and $cut is set to true, - * or an invalid $encoding is provided. + * @throws Psl\Exception\InvariantViolationException If $break is empty, $width is 0 and $cut is set to true. * * @return string the given string wrapped at the specified column * @@ -25,7 +24,7 @@ function wrap( int $width = 75, string $break = "\n", bool $cut = false, - ?string $encoding = null + Encoding $encoding = Encoding::UTF_8 ): string { if ('' === $string) { return ''; diff --git a/tests/unit/Str/AfterCiTest.php b/tests/unit/Str/AfterCiTest.php index aaa3acbd..83c31cac 100644 --- a/tests/unit/Str/AfterCiTest.php +++ b/tests/unit/Str/AfterCiTest.php @@ -17,7 +17,7 @@ public function testAfter( string $haystack, string $needle, int $offset, - ?string $encoding + Str\Encoding $encoding ): void { static::assertSame($expected, Str\after_ci($haystack, $needle, $offset, $encoding)); } @@ -25,21 +25,22 @@ public function testAfter( public function provideData(): array { return [ - [null, '', '', 0, null], - ['orld!', 'Hello, World!', 'W', 0, null], - ['!', '🤷!', '🤷', 0, null], - [null, 'مرحبا بكم', '', 0, null], - [null, 'مرحبا بكم', 'ß', 0, null], - [', wôrld!', 'héllö, wôrld!', 'héllö', 0, null], - [', wôrld!', 'ḫéllö, wôrld!', 'ḫéllö', 0, null], - [', wôrld!', 'ḫéllö, wôrld!', 'Ḫéllö', 0, null], - [', wôrld!', 'Ḫéllö, wôrld!', 'Ḫéllö', 0, null], - [', wôrld!', 'Ḫéllö, wôrld!', 'ḫéllö', 0, null], - ['好', '你好', '你', 0, null], - ['にちは世界', 'こんにちは世界', 'こん', 0, null], - ['สดี', 'สวัสดี', 'วั', 0, null], - [', world!', 'Hello, world!', 'o', 0, null], - ['rld!', 'Hello, world!', 'o', 7, null], + [null, '', '', 0, Str\Encoding::UTF_8], + ['orld!', 'Hello, World!', 'W', 0, Str\Encoding::UTF_8], + ['!', '🤷!', '🤷', 0, Str\Encoding::UTF_8], + [null, 'مرحبا بكم', '', 0, Str\Encoding::UTF_8], + [null, 'مرحبا بكم', 'ß', 0, Str\Encoding::UTF_8], + [', wôrld!', 'héllö, wôrld!', 'héllö', 0, Str\Encoding::UTF_8], + [', wôrld!', 'ḫéllö, wôrld!', 'ḫéllö', 0, Str\Encoding::UTF_8], + [', wôrld!', 'ḫéllö, wôrld!', 'Ḫéllö', 0, Str\Encoding::UTF_8], + [', wôrld!', 'Ḫéllö, wôrld!', 'Ḫéllö', 0, Str\Encoding::UTF_8], + [', wôrld!', 'Ḫéllö, wôrld!', 'ḫéllö', 0, Str\Encoding::UTF_8], + ['好', '你好', '你', 0, Str\Encoding::UTF_8], + ['にちは世界', 'こんにちは世界', 'こん', 0, Str\Encoding::UTF_8], + ['สดี', 'สวัสดี', 'วั', 0, Str\Encoding::UTF_8], + [', world!', 'Hello, world!', 'o', 0, Str\Encoding::UTF_8], + ['rld!', 'Hello, world!', 'o', 7, Str\Encoding::UTF_8], + ['rld!', 'Hello, world!', 'o', 7, Str\Encoding::ASCII], ]; } } diff --git a/tests/unit/Str/AfterLastCiTest.php b/tests/unit/Str/AfterLastCiTest.php index 23424e8f..87b13f89 100644 --- a/tests/unit/Str/AfterLastCiTest.php +++ b/tests/unit/Str/AfterLastCiTest.php @@ -17,29 +17,28 @@ public function testAfter( string $haystack, string $needle, int $offset, - ?string $encoding ): void { - static::assertSame($expected, Str\after_last_ci($haystack, $needle, $offset, $encoding)); + static::assertSame($expected, Str\after_last_ci($haystack, $needle, $offset)); } public function provideData(): array { return [ - [null, '', '', 0, null], - ['orld!', 'Hello, World!', 'W', 0, null], - ['!', '🤷!', '🤷', 0, null], - [null, 'مرحبا بكم', '', 0, null], - [null, 'مرحبا بكم', 'ß', 0, null], - [', wôrld!', 'héllö, wôrld!', 'héllö', 0, null], - [', wôrld!', 'ḫéllö, wôrld!', 'ḫéllö', 0, null], - [', wôrld!', 'ḫéllö, wôrld!', 'Ḫéllö', 0, null], - [', wôrld!', 'Ḫéllö, wôrld!', 'Ḫéllö', 0, null], - [', wôrld!', 'Ḫéllö, wôrld!', 'ḫéllö', 0, null], - ['好', '你好', '你', 0, null], - ['にちは世界', 'こんにちは世界', 'こん', 0, null], - ['สดี', 'สวัสดี', 'วั', 0, null], - ['rld!', 'Hello, world!', 'o', 0, null], - ['rld!', 'Hello, world!', 'o', 7, null], + [null, '', '', 0], + ['orld!', 'Hello, World!', 'W', 0], + ['!', '🤷!', '🤷', 0], + [null, 'مرحبا بكم', '', 0], + [null, 'مرحبا بكم', 'ß', 0], + [', wôrld!', 'héllö, wôrld!', 'héllö', 0], + [', wôrld!', 'ḫéllö, wôrld!', 'ḫéllö', 0], + [', wôrld!', 'ḫéllö, wôrld!', 'Ḫéllö', 0], + [', wôrld!', 'Ḫéllö, wôrld!', 'Ḫéllö', 0], + [', wôrld!', 'Ḫéllö, wôrld!', 'ḫéllö', 0], + ['好', '你好', '你', 0], + ['にちは世界', 'こんにちは世界', 'こん', 0], + ['สดี', 'สวัสดี', 'วั', 0], + ['rld!', 'Hello, world!', 'o', 0], + ['rld!', 'Hello, world!', 'o', 7], ]; } } diff --git a/tests/unit/Str/AfterLastTest.php b/tests/unit/Str/AfterLastTest.php index 8de81dbd..ef95da6d 100644 --- a/tests/unit/Str/AfterLastTest.php +++ b/tests/unit/Str/AfterLastTest.php @@ -17,29 +17,28 @@ public function testAfter( string $haystack, string $needle, int $offset, - ?string $encoding ): void { - static::assertSame($expected, Str\after_last($haystack, $needle, $offset, $encoding)); + static::assertSame($expected, Str\after_last($haystack, $needle, $offset)); } public function provideData(): array { return [ - [null, '', '', 0, null], - ['orld!', 'Hello, World!', 'W', 0, null], - ['!', '🤷!', '🤷', 0, null], - [null, 'مرحبا بكم', '', 0, null], - [null, 'مرحبا بكم', 'ß', 0, null], - [', wôrld!', 'héllö, wôrld!', 'héllö', 0, null], - [', wôrld!', 'ḫéllö, wôrld!', 'ḫéllö', 0, null], - [null, 'ḫéllö, wôrld!', 'Ḫéllö', 0, null], - [', wôrld!', 'Ḫéllö, wôrld!', 'Ḫéllö', 0, null], - [null, 'Ḫéllö, wôrld!', 'ḫéllö', 0, null], - ['好', '你好', '你', 0, null], - ['にちは世界', 'こんにちは世界', 'こん', 0, null], - ['สดี', 'สวัสดี', 'วั', 0, null], - ['rld!', 'Hello, world!', 'o', 0, null], - ['rld!', 'Hello, world!', 'o', 7, null], + [null, '', '', 0], + ['orld!', 'Hello, World!', 'W', 0], + ['!', '🤷!', '🤷', 0], + [null, 'مرحبا بكم', '', 0], + [null, 'مرحبا بكم', 'ß', 0], + [', wôrld!', 'héllö, wôrld!', 'héllö', 0], + [', wôrld!', 'ḫéllö, wôrld!', 'ḫéllö', 0], + [null, 'ḫéllö, wôrld!', 'Ḫéllö', 0], + [', wôrld!', 'Ḫéllö, wôrld!', 'Ḫéllö', 0], + [null, 'Ḫéllö, wôrld!', 'ḫéllö', 0], + ['好', '你好', '你', 0], + ['にちは世界', 'こんにちは世界', 'こん', 0], + ['สดี', 'สวัสดี', 'วั', 0], + ['rld!', 'Hello, world!', 'o', 0], + ['rld!', 'Hello, world!', 'o', 7], ]; } } diff --git a/tests/unit/Str/AfterTest.php b/tests/unit/Str/AfterTest.php index 4cda656e..ae794e80 100644 --- a/tests/unit/Str/AfterTest.php +++ b/tests/unit/Str/AfterTest.php @@ -17,30 +17,29 @@ public function testAfter( string $haystack, string $needle, int $offset, - ?string $encoding ): void { - static::assertSame($expected, Str\after($haystack, $needle, $offset, $encoding)); + static::assertSame($expected, Str\after($haystack, $needle, $offset)); } public function provideData(): array { return [ - [null, '', '', 0, null], - ['orld!', 'Hello, World!', 'W', 0, null], - ['!', '🤷!', '🤷', 0, null], - [null, 'مرحبا بكم', '', 0, null], - [null, 'مرحبا بكم', 'ß', 0, null], - [', wôrld!', 'héllö, wôrld!', 'héllö', 0, null], - [', wôrld!', 'ḫéllö, wôrld!', 'ḫéllö', 0, null], - [null, 'ḫéllö, wôrld!', 'Ḫéllö', 0, null], - [', wôrld!', 'Ḫéllö, wôrld!', 'Ḫéllö', 0, null], - [null, 'Ḫéllö, wôrld!', 'ḫéllö', 0, null], - ['好', '你好', '你', 0, null], - ['にちは世界', 'こんにちは世界', 'こん', 0, null], - ['สดี', 'สวัสดี', 'วั', 0, null], - [', world!', 'Hello, world!', 'o', 0, null], - ['rld!', 'Hello, world!', 'o', 7, null], + [null, '', '', 0], + ['orld!', 'Hello, World!', 'W', 0], + ['!', '🤷!', '🤷', 0], + [null, 'مرحبا بكم', '', 0], + [null, 'مرحبا بكم', 'ß', 0], + [', wôrld!', 'héllö, wôrld!', 'héllö', 0], + [', wôrld!', 'ḫéllö, wôrld!', 'ḫéllö', 0], + [null, 'ḫéllö, wôrld!', 'Ḫéllö', 0], + [', wôrld!', 'Ḫéllö, wôrld!', 'Ḫéllö', 0], + [null, 'Ḫéllö, wôrld!', 'ḫéllö', 0], + ['好', '你好', '你', 0], + ['にちは世界', 'こんにちは世界', 'こん', 0], + ['สดี', 'สวัสดี', 'วั', 0], + [', world!', 'Hello, world!', 'o', 0], + ['rld!', 'Hello, world!', 'o', 7], ]; } } diff --git a/tests/unit/Str/BeforeCiTest.php b/tests/unit/Str/BeforeCiTest.php index 333971cd..1e82674f 100644 --- a/tests/unit/Str/BeforeCiTest.php +++ b/tests/unit/Str/BeforeCiTest.php @@ -17,36 +17,35 @@ public function testAfter( string $haystack, string $needle, int $offset, - ?string $encoding ): void { - static::assertSame($expected, Str\before_ci($haystack, $needle, $offset, $encoding)); + static::assertSame($expected, Str\before_ci($haystack, $needle, $offset)); } public function provideData(): array { return [ - [null, '', '', 0, null], - ['Hello, ', 'Hello, World!', 'W', 0, null], - ['', '🤷!', '🤷', 0, null], - [null, 'مرحبا بكم', '', 0, null], - [null, 'مرحبا بكم', 'ß', 0, null], - ['', 'héllö, wôrld!', 'héllö', 0, null], - ['', 'ḫéllö, wôrld!', 'ḫéllö', 0, null], - ['', 'ḫéllö, wôrld!', 'Ḫéllö', 0, null], - ['', 'Ḫéllö, wôrld!', 'Ḫéllö', 0, null], - ['', 'Ḫéllö, wôrld!', 'ḫéllö', 0, null], - ['héllö, ', 'héllö, wôrld!', 'w', 0, null], - ['ḫéllö, ', 'ḫéllö, wôrld!', 'w', 0, null], - ['ḫéllö, ', 'ḫéllö, wôrld!', 'w', 0, null], - ['Ḫéllö, ', 'Ḫéllö, Wôrld!', 'w', 0, null], - ['Ḫéllö, ', 'Ḫéllö, wôrld!', 'w', 0, null], - ['你', '你好', '好', 0, null], - ['', '你好', '你', 0, null], - ['こんにちは世', 'こんにちは世界', '界', 0, null], - ['こんに', 'こんにちは世界', 'ち', 0, null], - ['ส ว ั ส ด', 'ส ว ั ส ด ี', ' ี', 0, null], - ['Hell', 'Hello, world!', 'o', 0, null], - ['Hello, w', 'Hello, world!', 'o', 7, null], + [null, '', '', 0], + ['Hello, ', 'Hello, World!', 'W', 0], + ['', '🤷!', '🤷', 0], + [null, 'مرحبا بكم', '', 0], + [null, 'مرحبا بكم', 'ß', 0], + ['', 'héllö, wôrld!', 'héllö', 0], + ['', 'ḫéllö, wôrld!', 'ḫéllö', 0], + ['', 'ḫéllö, wôrld!', 'Ḫéllö', 0], + ['', 'Ḫéllö, wôrld!', 'Ḫéllö', 0], + ['', 'Ḫéllö, wôrld!', 'ḫéllö', 0], + ['héllö, ', 'héllö, wôrld!', 'w', 0], + ['ḫéllö, ', 'ḫéllö, wôrld!', 'w', 0], + ['ḫéllö, ', 'ḫéllö, wôrld!', 'w', 0], + ['Ḫéllö, ', 'Ḫéllö, Wôrld!', 'w', 0], + ['Ḫéllö, ', 'Ḫéllö, wôrld!', 'w', 0], + ['你', '你好', '好', 0], + ['', '你好', '你', 0], + ['こんにちは世', 'こんにちは世界', '界', 0], + ['こんに', 'こんにちは世界', 'ち', 0], + ['ส ว ั ส ด', 'ส ว ั ส ด ี', ' ี', 0], + ['Hell', 'Hello, world!', 'o', 0], + ['Hello, w', 'Hello, world!', 'o', 7], ]; } } diff --git a/tests/unit/Str/BeforeLastCiTest.php b/tests/unit/Str/BeforeLastCiTest.php index 599c59c6..614c7cba 100644 --- a/tests/unit/Str/BeforeLastCiTest.php +++ b/tests/unit/Str/BeforeLastCiTest.php @@ -17,34 +17,33 @@ public function testAfter( string $haystack, string $needle, int $offset, - ?string $encoding ): void { - static::assertSame($expected, Str\before_last_ci($haystack, $needle, $offset, $encoding)); + static::assertSame($expected, Str\before_last_ci($haystack, $needle, $offset)); } public function provideData(): array { return [ - [null, '', '', 0, null], - ['Hello, ', 'Hello, World!', 'W', 0, null], - ['', '🤷!', '🤷', 0, null], - [null, 'مرحبا بكم', '', 0, null], - [null, 'مرحبا بكم', 'ß', 0, null], - ['', 'héllö, wôrld!', 'héllö', 0, null], - ['', 'ḫéllö, wôrld!', 'ḫéllö', 0, null], - ['', 'ḫéllö, wôrld!', 'Ḫéllö', 0, null], - ['', 'Ḫéllö, wôrld!', 'Ḫéllö', 0, null], - ['', 'Ḫéllö, wôrld!', 'ḫéllö', 0, null], - ['héllö, ', 'héllö, Wôrld!', 'wôrld', 0, null], - ['ḫéllö, ', 'ḫéllö, wôrld!', 'Wôrld', 0, null], - ['ḫéllö, ', 'ḫéllö, Wôrld!', 'wôrld', 0, null], - ['Ḫéllö, ', 'Ḫéllö, wôrld!', 'Wôrld', 0, null], - ['Ḫéllö, ', 'Ḫéllö, Wôrld!', 'wôrld', 0, null], - ['你', '你好', '好', 0, null], - ['こんにちは', 'こんにちは世界', '世界', 0, null], - ['สวัส', 'สวัสดี', 'ดี', 0, null], - ['Hello, w', 'Hello, world!', 'o', 0, null], - ['Hello, w', 'Hello, world!', 'o', 7, null], + [null, '', '', 0], + ['Hello, ', 'Hello, World!', 'W', 0], + ['', '🤷!', '🤷', 0], + [null, 'مرحبا بكم', '', 0], + [null, 'مرحبا بكم', 'ß', 0], + ['', 'héllö, wôrld!', 'héllö', 0], + ['', 'ḫéllö, wôrld!', 'ḫéllö', 0], + ['', 'ḫéllö, wôrld!', 'Ḫéllö', 0], + ['', 'Ḫéllö, wôrld!', 'Ḫéllö', 0], + ['', 'Ḫéllö, wôrld!', 'ḫéllö', 0], + ['héllö, ', 'héllö, Wôrld!', 'wôrld', 0], + ['ḫéllö, ', 'ḫéllö, wôrld!', 'Wôrld', 0], + ['ḫéllö, ', 'ḫéllö, Wôrld!', 'wôrld', 0], + ['Ḫéllö, ', 'Ḫéllö, wôrld!', 'Wôrld', 0], + ['Ḫéllö, ', 'Ḫéllö, Wôrld!', 'wôrld', 0], + ['你', '你好', '好', 0], + ['こんにちは', 'こんにちは世界', '世界', 0], + ['สวัส', 'สวัสดี', 'ดี', 0], + ['Hello, w', 'Hello, world!', 'o', 0], + ['Hello, w', 'Hello, world!', 'o', 7], ]; } } diff --git a/tests/unit/Str/BeforeLastTest.php b/tests/unit/Str/BeforeLastTest.php index ba242a11..80264646 100644 --- a/tests/unit/Str/BeforeLastTest.php +++ b/tests/unit/Str/BeforeLastTest.php @@ -17,35 +17,34 @@ public function testAfter( string $haystack, string $needle, int $offset, - ?string $encoding ): void { - static::assertSame($expected, Str\before_last($haystack, $needle, $offset, $encoding)); + static::assertSame($expected, Str\before_last($haystack, $needle, $offset)); } public function provideData(): array { return [ - [null, '', '', 0, null], - ['Hello, ', 'Hello, World!', 'W', 0, null], - ['', '🤷!', '🤷', 0, null], - [null, 'مرحبا بكم', '', 0, null], - [null, 'مرحبا بكم', 'ß', 0, null], - ['', 'héllö, wôrld!', 'héllö', 0, null], - ['', 'ḫéllö, wôrld!', 'ḫéllö', 0, null], - [null, 'ḫéllö, wôrld!', 'Ḫéllö', 0, null], - ['', 'Ḫéllö, wôrld!', 'Ḫéllö', 0, null], - [null, 'Ḫéllö, wôrld!', 'ḫéllö', 0, null], - [null, 'héllö, Wôrld!', 'wôrld', 0, null], - [null, 'ḫéllö, wôrld!', 'Wôrld', 0, null], - [null, 'ḫéllö, Wôrld!', 'wôrld', 0, null], - [null, 'Ḫéllö, wôrld!', 'Wôrld', 0, null], - [null, 'Ḫéllö, Wôrld!', 'wôrld', 0, null], - ['Ḫéllö, ', 'Ḫéllö, Wôrld!', 'Wôrld', 0, null], - ['你', '你好', '好', 0, null], - ['こんにちは', 'こんにちは世界', '世界', 0, null], - ['สวัส', 'สวัสดี', 'ดี', 0, null], - ['Hello, w', 'Hello, world!', 'o', 0, null], - ['Hello, w', 'Hello, world!', 'o', 7, null], + [null, '', '', 0], + ['Hello, ', 'Hello, World!', 'W', 0], + ['', '🤷!', '🤷', 0], + [null, 'مرحبا بكم', '', 0], + [null, 'مرحبا بكم', 'ß', 0], + ['', 'héllö, wôrld!', 'héllö', 0], + ['', 'ḫéllö, wôrld!', 'ḫéllö', 0], + [null, 'ḫéllö, wôrld!', 'Ḫéllö', 0], + ['', 'Ḫéllö, wôrld!', 'Ḫéllö', 0], + [null, 'Ḫéllö, wôrld!', 'ḫéllö', 0], + [null, 'héllö, Wôrld!', 'wôrld', 0], + [null, 'ḫéllö, wôrld!', 'Wôrld', 0], + [null, 'ḫéllö, Wôrld!', 'wôrld', 0], + [null, 'Ḫéllö, wôrld!', 'Wôrld', 0], + [null, 'Ḫéllö, Wôrld!', 'wôrld', 0], + ['Ḫéllö, ', 'Ḫéllö, Wôrld!', 'Wôrld', 0], + ['你', '你好', '好', 0], + ['こんにちは', 'こんにちは世界', '世界', 0], + ['สวัส', 'สวัสดี', 'ดี', 0], + ['Hello, w', 'Hello, world!', 'o', 0], + ['Hello, w', 'Hello, world!', 'o', 7], ]; } } diff --git a/tests/unit/Str/BeforeTest.php b/tests/unit/Str/BeforeTest.php index 87812498..7caa7e78 100644 --- a/tests/unit/Str/BeforeTest.php +++ b/tests/unit/Str/BeforeTest.php @@ -17,36 +17,35 @@ public function testAfter( string $haystack, string $needle, int $offset, - ?string $encoding ): void { - static::assertSame($expected, Str\before($haystack, $needle, $offset, $encoding)); + static::assertSame($expected, Str\before($haystack, $needle, $offset)); } public function provideData(): array { return [ - [null, '', '', 0, null], - ['Hello, ', 'Hello, World!', 'W', 0, null], - ['', '🤷!', '🤷', 0, null], - [null, 'مرحبا بكم', '', 0, null], - [null, 'مرحبا بكم', 'ß', 0, null], - ['', 'héllö, wôrld!', 'héllö', 0, null], - ['', 'ḫéllö, wôrld!', 'ḫéllö', 0, null], - [null, 'ḫéllö, wôrld!', 'Ḫéllö', 0, null], - ['', 'Ḫéllö, wôrld!', 'Ḫéllö', 0, null], - [null, 'Ḫéllö, wôrld!', 'ḫéllö', 0, null], - [null, 'héllö, Wôrld!', 'wôrld', 0, null], - [null, 'ḫéllö, wôrld!', 'Wôrld', 0, null], - [null, 'ḫéllö, Wôrld!', 'wôrld', 0, null], - [null, 'Ḫéllö, wôrld!', 'Wôrld', 0, null], - [null, 'Ḫéllö, Wôrld!', 'wôrld', 0, null], - ['Ḫéllö, ', 'Ḫéllö, Wôrld!', 'Wôrld', 0, null], - ['你', '你好', '好', 0, null], - ['こんにちは', 'こんにちは世界', '世界', 0, null], - ['สวัส', 'สวัสดี', 'ดี', 0, null], - ['Hell', 'Hello, world!', 'o', 0, null], - ['Hello, w', 'Hello, world!', 'o', 7, null], + [null, '', '', 0], + ['Hello, ', 'Hello, World!', 'W', 0], + ['', '🤷!', '🤷', 0], + [null, 'مرحبا بكم', '', 0], + [null, 'مرحبا بكم', 'ß', 0], + ['', 'héllö, wôrld!', 'héllö', 0], + ['', 'ḫéllö, wôrld!', 'ḫéllö', 0], + [null, 'ḫéllö, wôrld!', 'Ḫéllö', 0], + ['', 'Ḫéllö, wôrld!', 'Ḫéllö', 0], + [null, 'Ḫéllö, wôrld!', 'ḫéllö', 0], + [null, 'héllö, Wôrld!', 'wôrld', 0], + [null, 'ḫéllö, wôrld!', 'Wôrld', 0], + [null, 'ḫéllö, Wôrld!', 'wôrld', 0], + [null, 'Ḫéllö, wôrld!', 'Wôrld', 0], + [null, 'Ḫéllö, Wôrld!', 'wôrld', 0], + ['Ḫéllö, ', 'Ḫéllö, Wôrld!', 'Wôrld', 0], + ['你', '你好', '好', 0], + ['こんにちは', 'こんにちは世界', '世界', 0], + ['สวัส', 'สวัสดี', 'ดี', 0], + ['Hell', 'Hello, world!', 'o', 0], + ['Hello, w', 'Hello, world!', 'o', 7], ]; } }