diff --git a/docs/component/env.md b/docs/component/env.md index 5c834660d..500ae43bc 100644 --- a/docs/component/env.md +++ b/docs/component/env.md @@ -15,13 +15,13 @@ - [args](./../../src/Psl/Env/args.php#L12) - [current_dir](./../../src/Psl/Env/current_dir.php#L16) - [current_exec](./../../src/Psl/Env/current_exec.php#L12) -- [get_var](./../../src/Psl/Env/get_var.php#L18) +- [get_var](./../../src/Psl/Env/get_var.php#L19) - [get_vars](./../../src/Psl/Env/get_vars.php#L14) - [join_paths](./../../src/Psl/Env/join_paths.php#L18) -- [remove_var](./../../src/Psl/Env/remove_var.php#L18) +- [remove_var](./../../src/Psl/Env/remove_var.php#L19) - [set_current_dir](./../../src/Psl/Env/set_current_dir.php#L16) -- [set_var](./../../src/Psl/Env/set_var.php#L19) +- [set_var](./../../src/Psl/Env/set_var.php#L20) - [split_paths](./../../src/Psl/Env/split_paths.php#L16) -- [temp_dir](./../../src/Psl/Env/temp_dir.php#L20) +- [temp_dir](./../../src/Psl/Env/temp_dir.php#L22) diff --git a/docs/component/file.md b/docs/component/file.md index 9241f01ef..63c4bbc54 100644 --- a/docs/component/file.md +++ b/docs/component/file.md @@ -12,9 +12,9 @@ #### `Functions` -- [open_read_only](./../../src/Psl/File/open_read_only.php#L14) -- [open_read_write](./../../src/Psl/File/open_read_write.php#L16) -- [open_write_only](./../../src/Psl/File/open_write_only.php#L16) +- [open_read_only](./../../src/Psl/File/open_read_only.php#L16) +- [open_read_write](./../../src/Psl/File/open_read_write.php#L18) +- [open_write_only](./../../src/Psl/File/open_write_only.php#L18) - [temporary](./../../src/Psl/File/temporary.php#L22) #### `Interfaces` diff --git a/docs/component/filesystem.md b/docs/component/filesystem.md index cc59c9400..174515e10 100644 --- a/docs/component/filesystem.md +++ b/docs/component/filesystem.md @@ -16,41 +16,41 @@ #### `Functions` -- [append_file](./../../src/Psl/Filesystem/append_file.php#L23) +- [append_file](./../../src/Psl/Filesystem/append_file.php#L26) - [canonicalize](./../../src/Psl/Filesystem/canonicalize.php#L15) - [change_group](./../../src/Psl/Filesystem/change_group.php#L20) -- [change_owner](./../../src/Psl/Filesystem/change_owner.php#L20) -- [change_permissions](./../../src/Psl/Filesystem/change_permissions.php#L19) -- [copy](./../../src/Psl/Filesystem/copy.php#L21) +- [change_owner](./../../src/Psl/Filesystem/change_owner.php#L22) +- [change_permissions](./../../src/Psl/Filesystem/change_permissions.php#L21) +- [copy](./../../src/Psl/Filesystem/copy.php#L24) - [create_directory](./../../src/Psl/Filesystem/create_directory.php#L17) -- [create_file](./../../src/Psl/Filesystem/create_file.php#L23) -- [create_hard_link](./../../src/Psl/Filesystem/create_hard_link.php#L21) -- [create_symbolic_link](./../../src/Psl/Filesystem/create_symbolic_link.php#L21) +- [create_file](./../../src/Psl/Filesystem/create_file.php#L24) +- [create_hard_link](./../../src/Psl/Filesystem/create_hard_link.php#L22) +- [create_symbolic_link](./../../src/Psl/Filesystem/create_symbolic_link.php#L22) - [create_temporary_file](./../../src/Psl/Filesystem/create_temporary_file.php#L26) -- [delete_directory](./../../src/Psl/Filesystem/delete_directory.php#L23) -- [delete_file](./../../src/Psl/Filesystem/delete_file.php#L20) +- [delete_directory](./../../src/Psl/Filesystem/delete_directory.php#L24) +- [delete_file](./../../src/Psl/Filesystem/delete_file.php#L22) - [exists](./../../src/Psl/Filesystem/exists.php#L19) -- [file_size](./../../src/Psl/Filesystem/file_size.php#L17) -- [get_access_time](./../../src/Psl/Filesystem/get_access_time.php#L18) +- [file_size](./../../src/Psl/Filesystem/file_size.php#L19) +- [get_access_time](./../../src/Psl/Filesystem/get_access_time.php#L20) - [get_basename](./../../src/Psl/Filesystem/get_basename.php#L23) -- [get_change_time](./../../src/Psl/Filesystem/get_change_time.php#L19) -- [get_directory](./../../src/Psl/Filesystem/get_directory.php#L27) +- [get_change_time](./../../src/Psl/Filesystem/get_change_time.php#L21) +- [get_directory](./../../src/Psl/Filesystem/get_directory.php#L24) - [get_extension](./../../src/Psl/Filesystem/get_extension.php#L16) - [get_filename](./../../src/Psl/Filesystem/get_filename.php#L18) -- [get_group](./../../src/Psl/Filesystem/get_group.php#L18) -- [get_inode](./../../src/Psl/Filesystem/get_inode.php#L18) -- [get_modification_time](./../../src/Psl/Filesystem/get_modification_time.php#L19) -- [get_owner](./../../src/Psl/Filesystem/get_owner.php#L18) -- [get_permissions](./../../src/Psl/Filesystem/get_permissions.php#L18) +- [get_group](./../../src/Psl/Filesystem/get_group.php#L20) +- [get_inode](./../../src/Psl/Filesystem/get_inode.php#L20) +- [get_modification_time](./../../src/Psl/Filesystem/get_modification_time.php#L21) +- [get_owner](./../../src/Psl/Filesystem/get_owner.php#L20) +- [get_permissions](./../../src/Psl/Filesystem/get_permissions.php#L20) - [is_directory](./../../src/Psl/Filesystem/is_directory.php#L22) - [is_executable](./../../src/Psl/Filesystem/is_executable.php#L20) - [is_file](./../../src/Psl/Filesystem/is_file.php#L22) - [is_readable](./../../src/Psl/Filesystem/is_readable.php#L20) -- [is_symbolic_link](./../../src/Psl/Filesystem/is_symbolic_link.php#L19) +- [is_symbolic_link](./../../src/Psl/Filesystem/is_symbolic_link.php#L21) - [is_writable](./../../src/Psl/Filesystem/is_writable.php#L20) -- [read_directory](./../../src/Psl/Filesystem/read_directory.php#L19) -- [read_file](./../../src/Psl/Filesystem/read_file.php#L23) -- [read_symbolic_link](./../../src/Psl/Filesystem/read_symbolic_link.php#L21) -- [write_file](./../../src/Psl/Filesystem/write_file.php#L23) +- [read_directory](./../../src/Psl/Filesystem/read_directory.php#L21) +- [read_file](./../../src/Psl/Filesystem/read_file.php#L24) +- [read_symbolic_link](./../../src/Psl/Filesystem/read_symbolic_link.php#L22) +- [write_file](./../../src/Psl/Filesystem/write_file.php#L25) diff --git a/docs/component/hash.md b/docs/component/hash.md index 1368287c5..05fdfd586 100644 --- a/docs/component/hash.md +++ b/docs/component/hash.md @@ -14,10 +14,10 @@ - [algorithms](./../../src/Psl/Hash/algorithms.php#L16) - [equals](./../../src/Psl/Hash/equals.php#L14) -- [hash](./../../src/Psl/Hash/hash.php#L17) +- [hash](./../../src/Psl/Hash/hash.php#L19) #### `Classes` -- [Context](./../../src/Psl/Hash/Context.php#L31) +- [Context](./../../src/Psl/Hash/Context.php#L30) diff --git a/docs/component/iter.md b/docs/component/iter.md index d774dd178..95a2a8e16 100644 --- a/docs/component/iter.md +++ b/docs/component/iter.md @@ -17,7 +17,7 @@ - [apply](./../../src/Psl/Iter/apply.php#L15) - [contains](./../../src/Psl/Iter/contains.php#L16) - [contains_key](./../../src/Psl/Iter/contains_key.php#L16) -- [count](./../../src/Psl/Iter/count.php#L21) +- [count](./../../src/Psl/Iter/count.php#L23) - [first](./../../src/Psl/Iter/first.php#L16) - [first_key](./../../src/Psl/Iter/first_key.php#L17) - [is_empty](./../../src/Psl/Iter/is_empty.php#L16) diff --git a/docs/component/secure-random.md b/docs/component/secure-random.md index ecd8ba366..30d6bf860 100644 --- a/docs/component/secure-random.md +++ b/docs/component/secure-random.md @@ -12,9 +12,9 @@ #### `Functions` -- [bytes](./../../src/Psl/SecureRandom/bytes.php#L24) +- [bytes](./../../src/Psl/SecureRandom/bytes.php#L22) - [float](./../../src/Psl/SecureRandom/float.php#L16) - [int](./../../src/Psl/SecureRandom/int.php#L23) -- [string](./../../src/Psl/SecureRandom/string.php#L29) +- [string](./../../src/Psl/SecureRandom/string.php#L28) diff --git a/docs/component/str-byte.md b/docs/component/str-byte.md index 04780b7c8..18a09ceb3 100644 --- a/docs/component/str-byte.md +++ b/docs/component/str-byte.md @@ -14,7 +14,7 @@ - [after](./../../src/Psl/Str/Byte/after.php#L14) - [after_ci](./../../src/Psl/Str/Byte/after_ci.php#L14) -- [after_last](./../../src/Psl/Str/Byte/after_last.php#L15) +- [after_last](./../../src/Psl/Str/Byte/after_last.php#L14) - [after_last_ci](./../../src/Psl/Str/Byte/after_last_ci.php#L14) - [before](./../../src/Psl/Str/Byte/before.php#L14) - [before_ci](./../../src/Psl/Str/Byte/before_ci.php#L14) @@ -23,7 +23,7 @@ - [capitalize](./../../src/Psl/Str/Byte/capitalize.php#L17) - [capitalize_words](./../../src/Psl/Str/Byte/capitalize_words.php#L17) - [chr](./../../src/Psl/Str/Byte/chr.php#L14) -- [chunk](./../../src/Psl/Str/Byte/chunk.php#L27) +- [chunk](./../../src/Psl/Str/Byte/chunk.php#L23) - [compare](./../../src/Psl/Str/Byte/compare.php#L19) - [compare_ci](./../../src/Psl/Str/Byte/compare_ci.php#L19) - [contains](./../../src/Psl/Str/Byte/contains.php#L21) @@ -41,14 +41,14 @@ - [replace_every_ci](./../../src/Psl/Str/Byte/replace_every_ci.php#L17) - [reverse](./../../src/Psl/Str/Byte/reverse.php#L14) - [rot13](./../../src/Psl/Str/Byte/rot13.php#L14) -- [search](./../../src/Psl/Str/Byte/search.php#L23) -- [search_ci](./../../src/Psl/Str/Byte/search_ci.php#L23) -- [search_last](./../../src/Psl/Str/Byte/search_last.php#L25) -- [search_last_ci](./../../src/Psl/Str/Byte/search_last_ci.php#L23) +- [search](./../../src/Psl/Str/Byte/search.php#L25) +- [search_ci](./../../src/Psl/Str/Byte/search_ci.php#L25) +- [search_last](./../../src/Psl/Str/Byte/search_last.php#L27) +- [search_last_ci](./../../src/Psl/Str/Byte/search_last_ci.php#L25) - [shuffle](./../../src/Psl/Str/Byte/shuffle.php#L14) -- [slice](./../../src/Psl/Str/Byte/slice.php#L22) -- [splice](./../../src/Psl/Str/Byte/splice.php#L23) -- [split](./../../src/Psl/Str/Byte/split.php#L25) +- [slice](./../../src/Psl/Str/Byte/slice.php#L24) +- [splice](./../../src/Psl/Str/Byte/splice.php#L25) +- [split](./../../src/Psl/Str/Byte/split.php#L22) - [starts_with](./../../src/Psl/Str/Byte/starts_with.php#L14) - [starts_with_ci](./../../src/Psl/Str/Byte/starts_with_ci.php#L14) - [strip_prefix](./../../src/Psl/Str/Byte/strip_prefix.php#L13) diff --git a/docs/component/str-grapheme.md b/docs/component/str-grapheme.md index ed3957bec..013537f19 100644 --- a/docs/component/str-grapheme.md +++ b/docs/component/str-grapheme.md @@ -14,7 +14,7 @@ - [after](./../../src/Psl/Str/Grapheme/after.php#L14) - [after_ci](./../../src/Psl/Str/Grapheme/after_ci.php#L14) -- [after_last](./../../src/Psl/Str/Grapheme/after_last.php#L15) +- [after_last](./../../src/Psl/Str/Grapheme/after_last.php#L14) - [after_last_ci](./../../src/Psl/Str/Grapheme/after_last_ci.php#L14) - [before](./../../src/Psl/Str/Grapheme/before.php#L14) - [before_ci](./../../src/Psl/Str/Grapheme/before_ci.php#L14) @@ -26,14 +26,14 @@ - [ends_with_ci](./../../src/Psl/Str/Grapheme/ends_with_ci.php#L17) - [length](./../../src/Psl/Str/Grapheme/length.php#L19) - [reverse](./../../src/Psl/Str/Grapheme/reverse.php#L15) -- [search](./../../src/Psl/Str/Grapheme/search.php#L24) -- [search_ci](./../../src/Psl/Str/Grapheme/search_ci.php#L24) -- [search_last](./../../src/Psl/Str/Grapheme/search_last.php#L25) -- [search_last_ci](./../../src/Psl/Str/Grapheme/search_last_ci.php#L25) -- [slice](./../../src/Psl/Str/Grapheme/slice.php#L21) +- [search](./../../src/Psl/Str/Grapheme/search.php#L26) +- [search_ci](./../../src/Psl/Str/Grapheme/search_ci.php#L26) +- [search_last](./../../src/Psl/Str/Grapheme/search_last.php#L27) +- [search_last_ci](./../../src/Psl/Str/Grapheme/search_last_ci.php#L27) +- [slice](./../../src/Psl/Str/Grapheme/slice.php#L23) - [starts_with](./../../src/Psl/Str/Grapheme/starts_with.php#L12) - [starts_with_ci](./../../src/Psl/Str/Grapheme/starts_with_ci.php#L12) - [strip_prefix](./../../src/Psl/Str/Grapheme/strip_prefix.php#L13) -- [strip_suffix](./../../src/Psl/Str/Grapheme/strip_suffix.php#L13) +- [strip_suffix](./../../src/Psl/Str/Grapheme/strip_suffix.php#L18) diff --git a/docs/component/str.md b/docs/component/str.md index 6f735c54f..a78d06d80 100644 --- a/docs/component/str.md +++ b/docs/component/str.md @@ -28,11 +28,11 @@ - [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) +- [chunk](./../../src/Psl/Str/chunk.php#L34) - [concat](./../../src/Psl/Str/concat.php#L20) - [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) +- [convert_encoding](./../../src/Psl/Str/convert_encoding.php#L14) - [detect_encoding](./../../src/Psl/Str/detect_encoding.php#L21) - [ends_with](./../../src/Psl/Str/ends_with.php#L35) - [ends_with_ci](./../../src/Psl/Str/ends_with_ci.php#L35) @@ -45,14 +45,14 @@ - [join](./../../src/Psl/Str/join.php#L27) - [length](./../../src/Psl/Str/length.php#L27) - [levenshtein](./../../src/Psl/Str/levenshtein.php#L29) -- [lowercase](./../../src/Psl/Str/lowercase.php#L34) -- [metaphone](./../../src/Psl/Str/metaphone.php#L23) -- [ord](./../../src/Psl/Str/ord.php#L26) +- [lowercase](./../../src/Psl/Str/lowercase.php#L30) +- [metaphone](./../../src/Psl/Str/metaphone.php#L19) +- [ord](./../../src/Psl/Str/ord.php#L22) - [pad_left](./../../src/Psl/Str/pad_left.php#L34) - [pad_right](./../../src/Psl/Str/pad_right.php#L34) -- [repeat](./../../src/Psl/Str/repeat.php#L28) +- [repeat](./../../src/Psl/Str/repeat.php#L26) - [replace](./../../src/Psl/Str/replace.php#L15) -- [replace_ci](./../../src/Psl/Str/replace_ci.php#L20) +- [replace_ci](./../../src/Psl/Str/replace_ci.php#L16) - [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) @@ -62,7 +62,7 @@ - [search_last_ci](./../../src/Psl/Str/search_last_ci.php#L25) - [slice](./../../src/Psl/Str/slice.php#L25) - [splice](./../../src/Psl/Str/splice.php#L23) -- [split](./../../src/Psl/Str/split.php#L26) +- [split](./../../src/Psl/Str/split.php#L22) - [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#L13) @@ -73,7 +73,7 @@ - [trim_right](./../../src/Psl/Str/trim_right.php#L18) - [truncate](./../../src/Psl/Str/truncate.php#L28) - [uppercase](./../../src/Psl/Str/uppercase.php#L14) -- [width](./../../src/Psl/Str/width.php#L18) +- [width](./../../src/Psl/Str/width.php#L14) - [wrap](./../../src/Psl/Str/wrap.php#L22) #### `Enums` diff --git a/docs/component/vec.md b/docs/component/vec.md index 9e2cf4b22..57b227ef7 100644 --- a/docs/component/vec.md +++ b/docs/component/vec.md @@ -28,7 +28,7 @@ - [partition](./../../src/Psl/Vec/partition.php#L18) - [range](./../../src/Psl/Vec/range.php#L51) - [reductions](./../../src/Psl/Vec/reductions.php#L27) -- [reproduce](./../../src/Psl/Vec/reproduce.php#L25) +- [reproduce](./../../src/Psl/Vec/reproduce.php#L22) - [reverse](./../../src/Psl/Vec/reverse.php#L22) - [shuffle](./../../src/Psl/Vec/shuffle.php#L26) - [sort](./../../src/Psl/Vec/sort.php#L23) diff --git a/src/Psl/Env/get_var.php b/src/Psl/Env/get_var.php index da50b96a1..145b0cd82 100644 --- a/src/Psl/Env/get_var.php +++ b/src/Psl/Env/get_var.php @@ -5,22 +5,22 @@ namespace Psl\Env; use Psl; -use Psl\Str; use function getenv; +use function str_contains; /** * Fetches the environment variable $key from the current process. * - * @throws Psl\Exception\InvariantViolationException If $key is empty, or contains an ASCII equals sign `=` - * or the NUL character `\0`. + * @param non-empty-string $key + * + * @throws Psl\Exception\InvariantViolationException If $key contains an ASCII equals sign `=`, or the NUL character `\0`. */ function get_var(string $key): ?string { - Psl\invariant( - !Str\is_empty($key) && !Str\contains($key, '=') && !Str\contains($key, "\0"), - 'Invalid environment variable key provided.' - ); + if (str_contains($key, '=') || str_contains($key, "\0")) { + Psl\invariant_violation('Invalid environment variable key provided.'); + } $value = getenv($key); diff --git a/src/Psl/Env/remove_var.php b/src/Psl/Env/remove_var.php index 65efcf4d2..e4b1a30e8 100644 --- a/src/Psl/Env/remove_var.php +++ b/src/Psl/Env/remove_var.php @@ -5,22 +5,22 @@ namespace Psl\Env; use Psl; -use Psl\Str; use function putenv; +use function str_contains; /** * Removes an environment variable from the environment of the currently running process. * - * @throws Psl\Exception\InvariantViolationException If $key is empty, or contains an ASCII equals sign `=` or - * the NUL character `\0`. + * @param non-empty-string $key + * + * @throws Psl\Exception\InvariantViolationException If contains an ASCII equals sign `=` or, the NUL character `\0`. */ function remove_var(string $key): void { - Psl\invariant( - !Str\is_empty($key) && !Str\contains($key, '=') && !Str\contains($key, "\0"), - 'Invalid environment variable key provided.' - ); + if (str_contains($key, '=') || str_contains($key, "\0")) { + Psl\invariant_violation('Invalid environment variable key provided.'); + } putenv($key); } diff --git a/src/Psl/Env/set_current_dir.php b/src/Psl/Env/set_current_dir.php index 2d9653e0c..369738e6e 100644 --- a/src/Psl/Env/set_current_dir.php +++ b/src/Psl/Env/set_current_dir.php @@ -15,5 +15,7 @@ */ function set_current_dir(string $directory): void { - Psl\invariant(chdir($directory), 'Unable to change directory'); + if (!@chdir($directory)) { + Psl\invariant_violation('Unable to change directory'); + } } diff --git a/src/Psl/Env/set_var.php b/src/Psl/Env/set_var.php index 1f28b3581..a25cc001c 100644 --- a/src/Psl/Env/set_var.php +++ b/src/Psl/Env/set_var.php @@ -5,25 +5,27 @@ namespace Psl\Env; use Psl; -use Psl\Str; use function putenv; +use function str_contains; /** * Sets the environment variable $key to the value $value for the currently running process. * - * @throws Psl\Exception\InvariantViolationException If $key is empty, contains an ASCII equals sign `=`, - * the NUL character `\0`, or when the $value contains - * the NUL character. + * @param non-empty-string $key + * + * @throws Psl\Exception\InvariantViolationException If $key contains an ASCII equals sign `=`, the NUL character `\0`, + * or when the $value contains the NUL character. */ function set_var(string $key, string $value): void { - Psl\invariant( - !Str\is_empty($key) && !Str\contains($key, '=') && !Str\contains($key, "\0"), - 'Invalid environment variable key provided.' - ); + if (str_contains($key, '=') || str_contains($key, "\0")) { + Psl\invariant_violation('Invalid environment variable key provided.'); + } - Psl\invariant(!Str\contains($value, "\0"), 'Invalid environment variable value provided.'); + if (str_contains($value, "\0")) { + Psl\invariant_violation('Invalid environment variable value provided.'); + } - putenv(Str\format('%s=%s', $key, $value)); + putenv($key . '=' . $value); } diff --git a/src/Psl/Env/temp_dir.php b/src/Psl/Env/temp_dir.php index 34f12e863..21b89aefe 100644 --- a/src/Psl/Env/temp_dir.php +++ b/src/Psl/Env/temp_dir.php @@ -16,8 +16,11 @@ * * @see https://www.php.net/manual/en/function.sys-get-temp-dir.php * @see https://github.com/php/php-src/blob/fd0b57d48bab3924a31d3d0b038f0d5de6eab3e3/main/php_open_temporary_file.c#L204 + * + * @return non-empty-string */ function temp_dir(): string { + /** @var non-empty-string */ return sys_get_temp_dir(); } diff --git a/src/Psl/File/ReadHandle.php b/src/Psl/File/ReadHandle.php index 4d4d52d2e..d4c62783c 100644 --- a/src/Psl/File/ReadHandle.php +++ b/src/Psl/File/ReadHandle.php @@ -15,7 +15,7 @@ final class ReadHandle extends Internal\AbstractHandleWrapper implements ReadHan private ReadHandleInterface $readHandle; /** - * @param resource|object $stream + * @param non-empty-string $path * * @throws Psl\Exception\InvariantViolationException If $path does not point to a file, or is not readable. */ diff --git a/src/Psl/File/ReadWriteHandle.php b/src/Psl/File/ReadWriteHandle.php index 0d99487e5..082f9d3cd 100644 --- a/src/Psl/File/ReadWriteHandle.php +++ b/src/Psl/File/ReadWriteHandle.php @@ -16,7 +16,7 @@ final class ReadWriteHandle extends Internal\AbstractHandleWrapper implements Re private ReadWriteHandleInterface $readWriteHandle; /** - * @param resource|object $stream + * @param non-empty-string $path * * @throws Psl\Exception\InvariantViolationException If $path points to a non-file node, or it not writeable. * @throws Filesystem\Exception\RuntimeException If unable to create $path when it does not exist. diff --git a/src/Psl/File/WriteHandle.php b/src/Psl/File/WriteHandle.php index b97772661..ca65700a7 100644 --- a/src/Psl/File/WriteHandle.php +++ b/src/Psl/File/WriteHandle.php @@ -15,7 +15,7 @@ final class WriteHandle extends Internal\AbstractHandleWrapper implements WriteH private WriteHandleInterface $writeHandle; /** - * @param resource|object $stream + * @param non-empty-string $path * * @throws Psl\Exception\InvariantViolationException If $filename points to a non-file node, or it not writeable. * @throws Filesystem\Exception\RuntimeException If unable to create $path when it does not exist. diff --git a/src/Psl/File/open_read_only.php b/src/Psl/File/open_read_only.php index b3f74d7c8..682bc0e56 100644 --- a/src/Psl/File/open_read_only.php +++ b/src/Psl/File/open_read_only.php @@ -9,6 +9,8 @@ /** * Open a file handle for read only. * + * @param non-empty-string $path + * * @throws Psl\Exception\InvariantViolationException If $path does not point to a file, or is not readable. * */ function open_read_only(string $path): ReadHandleInterface diff --git a/src/Psl/File/open_read_write.php b/src/Psl/File/open_read_write.php index 751812e50..115b361c7 100644 --- a/src/Psl/File/open_read_write.php +++ b/src/Psl/File/open_read_write.php @@ -10,6 +10,8 @@ /** * Open a file handle for read and write. * + * @param non-empty-string $path + * * @throws Psl\Exception\InvariantViolationException If $path points to a non-file node, or it not writeable. * @throws Filesystem\Exception\RuntimeException If unable to create $path when it does not exist. */ diff --git a/src/Psl/File/open_write_only.php b/src/Psl/File/open_write_only.php index 4fc7f7b91..57cd7e757 100644 --- a/src/Psl/File/open_write_only.php +++ b/src/Psl/File/open_write_only.php @@ -10,6 +10,8 @@ /** * Open a file handle for write only. * + * @param non-empty-string $filename + * * @throws Psl\Exception\InvariantViolationException If $filename points to a non-file node, or it not writeable. * @throws Filesystem\Exception\RuntimeException If unable to create $path when it does not exist. */ diff --git a/src/Psl/Filesystem/append_file.php b/src/Psl/Filesystem/append_file.php index 4e6fd4487..f06355dfd 100644 --- a/src/Psl/Filesystem/append_file.php +++ b/src/Psl/Filesystem/append_file.php @@ -16,6 +16,9 @@ * * If $file does not exist, it will be created. * + * @param non-empty-string $file + * @param non-empty-string $content + * * @throws Psl\Exception\InvariantViolationException If the file specified by * $file is a directory, or is not writeable. * @throws Exception\RuntimeException In case of an error. diff --git a/src/Psl/Filesystem/change_group.php b/src/Psl/Filesystem/change_group.php index bb866380a..947930b1c 100644 --- a/src/Psl/Filesystem/change_group.php +++ b/src/Psl/Filesystem/change_group.php @@ -19,7 +19,9 @@ */ function change_group(string $filename, int $group): void { - Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename); + if (!namespace\exists($filename)) { + Psl\invariant_violation('File "%s" does not exist.', $filename); + } if (is_symbolic_link($filename)) { $fun = static fn(): bool => lchgrp($filename, $group); diff --git a/src/Psl/Filesystem/change_owner.php b/src/Psl/Filesystem/change_owner.php index 9dacb845a..48a6d5864 100644 --- a/src/Psl/Filesystem/change_owner.php +++ b/src/Psl/Filesystem/change_owner.php @@ -14,12 +14,17 @@ /** * Change the owner of $filename. * + * @param non-empty-string $filename + * * @throws Exception\RuntimeException If unable to change the ownership for $filename. * @throws Psl\Exception\InvariantViolationException If $filename does not exist. */ function change_owner(string $filename, int $user): void { - Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename); + if (!namespace\exists($filename)) { + Psl\invariant_violation('File "%s" does not exist.', $filename); + } + if (is_symbolic_link($filename)) { $fun = static fn(): bool => lchown($filename, $user); } else { diff --git a/src/Psl/Filesystem/change_permissions.php b/src/Psl/Filesystem/change_permissions.php index 5538c5f28..e38a26c70 100644 --- a/src/Psl/Filesystem/change_permissions.php +++ b/src/Psl/Filesystem/change_permissions.php @@ -13,12 +13,16 @@ /** * Changes mode permission of $filename. * + * @param non-empty-string $filename + * * @throws Exception\RuntimeException If unable to change the mode for the given $filename. * @throws Psl\Exception\InvariantViolationException If $filename does not exists. */ function change_permissions(string $filename, int $permissions): void { - Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename); + if (!namespace\exists($filename)) { + Psl\invariant_violation('File "%s" does not exist.', $filename); + } [$success, $error] = Internal\box(static fn(): bool => chmod($filename, $permissions)); // @codeCoverageIgnoreStart diff --git a/src/Psl/Filesystem/copy.php b/src/Psl/Filesystem/copy.php index 0acb45d4c..a0ffa5177 100644 --- a/src/Psl/Filesystem/copy.php +++ b/src/Psl/Filesystem/copy.php @@ -15,12 +15,17 @@ /** * Change the group ownership of $filename. * + * @param non-empty-string $source + * @param non-empty-string $destination + * * @throws Exception\RuntimeException If unable to change the group ownership for $filename. * @throws Psl\Exception\InvariantViolationException If $source does not exist or is not readable. */ function copy(string $source, string $destination, bool $overwrite = false): void { - Psl\invariant(is_file($source) && is_readable($source), 'Source "%s" does not exist or is not readable.', $source); + if (!namespace\is_file($source) || !namespace\is_readable($source)) { + Psl\invariant_violation('Source "%s" does not exist, or is not readable.', $source); + } if (!$overwrite && is_file($destination)) { return; diff --git a/src/Psl/Filesystem/create_file.php b/src/Psl/Filesystem/create_file.php index 7c50426d6..f53d51907 100644 --- a/src/Psl/Filesystem/create_file.php +++ b/src/Psl/Filesystem/create_file.php @@ -13,6 +13,7 @@ /** * Create the file specified by $filename. * + * @param non-empty-string $filename * @param int|null $time The touch time as a Unix timestamp, * If not supplied the current system time is used. * @param int|null $access_time The access time as a Unix timestamp, diff --git a/src/Psl/Filesystem/create_hard_link.php b/src/Psl/Filesystem/create_hard_link.php index d125f28f4..1dfdbf152 100644 --- a/src/Psl/Filesystem/create_hard_link.php +++ b/src/Psl/Filesystem/create_hard_link.php @@ -13,15 +13,21 @@ /** * Create a hard link for $source. * - * @param string $source The file to create a hard link for. + * @param non-empty-string $source The file to create a hard link for. + * @param non-empty-string $destination * * @throws Psl\Exception\InvariantViolationException If $source is not a file, or does not exist. * @throws Exception\RuntimeException If unable to create a hard file. */ function create_hard_link(string $source, string $destination): void { - Psl\invariant(exists($source), 'Source file "%s" does not exist.', $source); - Psl\invariant(is_file($source), 'Source "%s" is not a file.', $source); + if (!namespace\exists($source)) { + Psl\invariant_violation('Source file "%s" does not exist.', $source); + } + + if (!namespace\is_file($source)) { + Psl\invariant_violation('Source "%s" is not a file.', $source); + } $destination_directory = get_directory($destination); if (!is_directory($destination_directory)) { diff --git a/src/Psl/Filesystem/create_symbolic_link.php b/src/Psl/Filesystem/create_symbolic_link.php index 4069ae074..d0e70636c 100644 --- a/src/Psl/Filesystem/create_symbolic_link.php +++ b/src/Psl/Filesystem/create_symbolic_link.php @@ -13,14 +13,18 @@ /** * Create a symbolic link for $source. * - * @param string $source The file to create a hard link for. + * @param non-empty-string $source The file to create a hard link for. + * @param non-empty-string $destination * * @throws Exception\RuntimeException If unable to create the symbolic link. * @throws Psl\Exception\InvariantViolationException If $source does not exist. */ function create_symbolic_link(string $source, string $destination): void { - Psl\invariant(exists($source), '$source does not exist.'); + if (!namespace\exists($source)) { + Psl\invariant_violation('Source file "%s" does not exist.', $source); + } + $destination_directory = get_directory($destination); if (!is_directory($destination_directory)) { create_directory($destination_directory); diff --git a/src/Psl/Filesystem/create_temporary_file.php b/src/Psl/Filesystem/create_temporary_file.php index 78436acaf..b87b73f3c 100644 --- a/src/Psl/Filesystem/create_temporary_file.php +++ b/src/Psl/Filesystem/create_temporary_file.php @@ -21,7 +21,7 @@ * @throws Psl\Exception\InvariantViolationException If $prefix contains a directory separator. * @throws Exception\RuntimeException If unable to create the file. * - * @return string The absolute path to the temporary file. + * @return non-empty-string The absolute path to the temporary file. */ function create_temporary_file(?string $directory = null, ?string $prefix = null): string { diff --git a/src/Psl/Filesystem/delete_directory.php b/src/Psl/Filesystem/delete_directory.php index a3859b9da..fb7e338b0 100644 --- a/src/Psl/Filesystem/delete_directory.php +++ b/src/Psl/Filesystem/delete_directory.php @@ -5,7 +5,6 @@ namespace Psl\Filesystem; use Psl; -use Psl\Fun; use Psl\Internal; use Psl\Iter; use Psl\Str; @@ -16,6 +15,8 @@ /** * Delete the directory specified by $directory. * + * @param non-empty-string $directory + * * @throws Exception\RuntimeException If unable to delete the directory. * @throws Psl\Exception\InvariantViolationException If the directory specified by * $directory does not exist. @@ -25,19 +26,29 @@ function delete_directory(string $directory, bool $recursive = false): void Psl\invariant(is_directory($directory), 'Directory "%s" does not exist.', $directory); if ($recursive && !is_symbolic_link($directory)) { + $nodes = read_directory($directory); [$symbolic_nodes, $nodes] = Vec\partition( - read_directory($directory), + $nodes, + /** + * @param non-empty-string $node + */ static fn(string $node): bool => is_symbolic_link($node) ); - Iter\apply($symbolic_nodes, static fn(string $node) => delete_file($node)); + Iter\apply( + $symbolic_nodes, + /** + * @param non-empty-string $node + */ + static fn(string $node) => delete_file($node) + ); + Iter\apply( $nodes, - Fun\when( - static fn(string $node) => is_directory($node), - static fn(string $node) => delete_directory($node, true), - static fn(string $node) => delete_file($node), - ) + /** + * @param non-empty-string $node + */ + static fn(string $node) => is_directory($node) ? delete_directory($node, true) : delete_file($node), ); } diff --git a/src/Psl/Filesystem/delete_file.php b/src/Psl/Filesystem/delete_file.php index 4877d6ed3..74f0060a1 100644 --- a/src/Psl/Filesystem/delete_file.php +++ b/src/Psl/Filesystem/delete_file.php @@ -13,6 +13,8 @@ /** * Delete the file specified by $filename. * + * @param non-empty-string $filename + * * @throws Exception\RuntimeException If unable to delete the file. * @throws Psl\Exception\InvariantViolationException If the file specified by * $filename does not exist. diff --git a/src/Psl/Filesystem/file_size.php b/src/Psl/Filesystem/file_size.php index e0d66dbeb..c617efb8f 100644 --- a/src/Psl/Filesystem/file_size.php +++ b/src/Psl/Filesystem/file_size.php @@ -11,12 +11,16 @@ /** * Ge the size of $filename. * + * @param non-empty-string $filename + * * @throws Psl\Exception\InvariantViolationException If $filename does not exist or is not readable. * @throws Exception\RuntimeException In case of an error. */ function file_size(string $filename): int { - Psl\invariant(is_file($filename) && is_readable($filename), 'File "%s" does not exist.', $filename); + if (!namespace\is_file($filename) || !namespace\is_readable($filename)) { + Psl\invariant_violation('File "%s" does not exist, or is not readable.', $filename); + } // @codeCoverageIgnoreStart [$size, $message] = Internal\box(static fn() => filesize($filename)); diff --git a/src/Psl/Filesystem/get_access_time.php b/src/Psl/Filesystem/get_access_time.php index d42a9bc7c..2734638ea 100644 --- a/src/Psl/Filesystem/get_access_time.php +++ b/src/Psl/Filesystem/get_access_time.php @@ -12,12 +12,16 @@ /** * Gets last access time of $filename. * + * @param non-empty-string $filename + * * @throws Psl\Exception\InvariantViolationException If $filename does not exist. * @throws Exception\RuntimeException In case of an error. */ function get_access_time(string $filename): int { - Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename); + if (!namespace\exists($filename)) { + Psl\invariant_violation('File "%s" does not exist.', $filename); + } [$result, $message] = Psl\Internal\box( /** diff --git a/src/Psl/Filesystem/get_change_time.php b/src/Psl/Filesystem/get_change_time.php index b143a99c1..4fff5bf5b 100644 --- a/src/Psl/Filesystem/get_change_time.php +++ b/src/Psl/Filesystem/get_change_time.php @@ -13,12 +13,16 @@ * Get the last time the inode of $filename * was changed ( e.g: permission change, ownership change .. etc ). * + * @param non-empty-string $filename + * * @throws Psl\Exception\InvariantViolationException If $filename does not exist. * @throws Exception\RuntimeException In case of an error. */ function get_change_time(string $filename): int { - Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename); + if (!namespace\exists($filename)) { + Psl\invariant_violation('File "%s" does not exist.', $filename); + } [$result, $message] = Psl\Internal\box( /** diff --git a/src/Psl/Filesystem/get_directory.php b/src/Psl/Filesystem/get_directory.php index 02fb21682..7d31101f1 100644 --- a/src/Psl/Filesystem/get_directory.php +++ b/src/Psl/Filesystem/get_directory.php @@ -4,8 +4,6 @@ namespace Psl\Filesystem; -use Psl; - use function dirname; /** @@ -16,18 +14,14 @@ * * In other environments, it is the forward slash `/`. * + * @param non-empty-string $path * @param positive-int $levels The number of parent directories to go up. * - * @throws Psl\Exception\InvariantViolationException If $levels is not a positive integer. - * * @return string the base name of the given path. * * @pure */ function get_directory(string $path, int $levels = 1): string { - /** @psalm-suppress RedundantCondition */ - Psl\invariant($levels > 0, '$levels must be a positive integer, %d given.', $levels); - return dirname($path, $levels); } diff --git a/src/Psl/Filesystem/get_group.php b/src/Psl/Filesystem/get_group.php index a3d1534fc..fcb40c1d1 100644 --- a/src/Psl/Filesystem/get_group.php +++ b/src/Psl/Filesystem/get_group.php @@ -12,12 +12,16 @@ /** * Get the group of $filename. * + * @param non-empty-string $filename + * * @throws Psl\Exception\InvariantViolationException If $filename does not exist. * @throws Exception\RuntimeException In case of an error. */ function get_group(string $filename): int { - Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename); + if (!namespace\exists($filename)) { + Psl\invariant_violation('File "%s" does not exist.', $filename); + } [$result, $message] = Psl\Internal\box( /** diff --git a/src/Psl/Filesystem/get_inode.php b/src/Psl/Filesystem/get_inode.php index 5282d2e54..50a42f2be 100644 --- a/src/Psl/Filesystem/get_inode.php +++ b/src/Psl/Filesystem/get_inode.php @@ -12,12 +12,16 @@ /** * Get the last time the content of $filename was modified. * + * @param non-empty-string $filename + * * @throws Psl\Exception\InvariantViolationException If $filename does not exist. * @throws Exception\RuntimeException In case of an error. */ function get_inode(string $filename): int { - Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename); + if (!namespace\exists($filename)) { + Psl\invariant_violation('File "%s" does not exist.', $filename); + } [$result, $message] = Psl\Internal\box( /** diff --git a/src/Psl/Filesystem/get_modification_time.php b/src/Psl/Filesystem/get_modification_time.php index 85079d575..7cb3d71f9 100644 --- a/src/Psl/Filesystem/get_modification_time.php +++ b/src/Psl/Filesystem/get_modification_time.php @@ -13,12 +13,16 @@ /** * Get the last time the content of $filename was modified. * + * @param non-empty-string $filename + * * @throws Psl\Exception\InvariantViolationException If $filename does not exist. * @throws Exception\RuntimeException In case of an error. */ function get_modification_time(string $filename): int { - Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename); + if (!namespace\exists($filename)) { + Psl\invariant_violation('File "%s" does not exist.', $filename); + } [$result, $message] = Internal\box( /** diff --git a/src/Psl/Filesystem/get_owner.php b/src/Psl/Filesystem/get_owner.php index 322204254..2585abf1f 100644 --- a/src/Psl/Filesystem/get_owner.php +++ b/src/Psl/Filesystem/get_owner.php @@ -12,12 +12,16 @@ /** * Get the owner of $filename. * + * @param non-empty-string $filename + * * @throws Psl\Exception\InvariantViolationException If $filename does not exist. * @throws Exception\RuntimeException In case of an error. */ function get_owner(string $filename): int { - Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename); + if (!namespace\exists($filename)) { + Psl\invariant_violation('File "%s" does not exist.', $filename); + } [$result, $message] = Psl\Internal\box( /** diff --git a/src/Psl/Filesystem/get_permissions.php b/src/Psl/Filesystem/get_permissions.php index bfd0e6e83..ea64860cb 100644 --- a/src/Psl/Filesystem/get_permissions.php +++ b/src/Psl/Filesystem/get_permissions.php @@ -12,12 +12,16 @@ /** * Get the permissions of $filename. * + * @param non-empty-string $filename + * * @throws Psl\Exception\InvariantViolationException If $filename does not exist. * @throws Exception\RuntimeException In case of an error. */ function get_permissions(string $filename): int { - Psl\invariant(exists($filename), 'File "%s" does not exist.', $filename); + if (!namespace\exists($filename)) { + Psl\invariant_violation('File "%s" does not exist.', $filename); + } [$result, $message] = Psl\Internal\box( /** diff --git a/src/Psl/Filesystem/is_symbolic_link.php b/src/Psl/Filesystem/is_symbolic_link.php index 033655e03..eaf96a3b2 100644 --- a/src/Psl/Filesystem/is_symbolic_link.php +++ b/src/Psl/Filesystem/is_symbolic_link.php @@ -15,6 +15,8 @@ * the current working directory. * * @return bool if the $symbolic_link exists and is a symbolic link, false otherwise. + * + * @psalm-assert-if-true non-empty-string $symbolic_link */ function is_symbolic_link(string $symbolic_link): bool { diff --git a/src/Psl/Filesystem/read_directory.php b/src/Psl/Filesystem/read_directory.php index 6c2674757..aaa575ae4 100644 --- a/src/Psl/Filesystem/read_directory.php +++ b/src/Psl/Filesystem/read_directory.php @@ -11,18 +11,24 @@ /** * Return a vec of files and directories inside the specified directory. * + * @param non-empty-string $directory + * * @throws Psl\Exception\InvariantViolationException If the directory specified by * $directory does not exist, or is not readable. * - * @return list + * @return list */ function read_directory(string $directory): array { - Psl\invariant(exists($directory), 'Directory "%s" does not exist.', $directory); - Psl\invariant(is_directory($directory), 'Directory "%s" is not a directory.', $directory); - Psl\invariant(is_readable($directory), 'Directory "%s" is not readable.', $directory); + if (!namespace\is_directory($directory)) { + Psl\invariant_violation('Directory "%s" is not a directory.', $directory); + } + + if (!namespace\is_readable($directory)) { + Psl\invariant_violation('Directory "%s" is not readable.', $directory); + } - /** @var list */ + /** @var list */ return Vec\values(new FilesystemIterator( $directory, FilesystemIterator::CURRENT_AS_PATHNAME | FilesystemIterator::SKIP_DOTS diff --git a/src/Psl/Filesystem/read_file.php b/src/Psl/Filesystem/read_file.php index fbde7fed7..fbab5731e 100644 --- a/src/Psl/Filesystem/read_file.php +++ b/src/Psl/Filesystem/read_file.php @@ -12,7 +12,8 @@ /** * Reads entire file into a string. * - * @param positive-int|0 $offset The offset where the reading starts. + * @param non-empty-string $file + * @param int<0, max> $offset The offset where the reading starts. * @param positive-int|null $length Maximum length of data read. * The default is to read until end of file is reached. * diff --git a/src/Psl/Filesystem/read_symbolic_link.php b/src/Psl/Filesystem/read_symbolic_link.php index aaa5e7598..8ed1f9401 100644 --- a/src/Psl/Filesystem/read_symbolic_link.php +++ b/src/Psl/Filesystem/read_symbolic_link.php @@ -13,15 +13,21 @@ /** * Returns the target of a symbolic link. * - * @throws Psl\Exception\InvariantViolationException If the link specified by - * $symbolic_link does not exist, or is not a symbolic link. - * @throws Exception\RuntimeException If unable to retrieve the target - * of $symbolic_link. + * @param non-empty-string $symbolic_link + * + * @throws Psl\Exception\InvariantViolationException If the link specified by $symbolic_link does not exist, + * or is not a symbolic link. + * @throws Exception\RuntimeException If unable to retrieve the target of $symbolic_link. */ function read_symbolic_link(string $symbolic_link): string { - Psl\invariant(exists($symbolic_link), 'Symbolic link "%s" does not exist.', $symbolic_link); - Psl\invariant(is_symbolic_link($symbolic_link), 'Symbolic link "%s" is not a symbolic link.', $symbolic_link); + if (!namespace\exists($symbolic_link)) { + Psl\invariant_violation('Symbolic link "%s" does not exist.', $symbolic_link); + } + + if (!namespace\is_symbolic_link($symbolic_link)) { + Psl\invariant_violation('Symbolic link "%s" is not a symbolic link.', $symbolic_link); + } [$result, $message] = Internal\box( /** diff --git a/src/Psl/Filesystem/write_file.php b/src/Psl/Filesystem/write_file.php index fd019cc44..d783028da 100644 --- a/src/Psl/Filesystem/write_file.php +++ b/src/Psl/Filesystem/write_file.php @@ -16,6 +16,8 @@ * * If $file does not exist, it will be created. * + * @param non-empty-string $file + * * @throws Psl\Exception\InvariantViolationException If the file specified by * $file is a directory, or is not writeable. * @throws Exception\RuntimeException In case of an error. diff --git a/src/Psl/Hash/Context.php b/src/Psl/Hash/Context.php index e3c39c1be..7844c7fc7 100644 --- a/src/Psl/Hash/Context.php +++ b/src/Psl/Hash/Context.php @@ -6,7 +6,6 @@ use HashContext; use Psl; -use Psl\Iter; use Psl\Str; use function hash_final; @@ -40,18 +39,18 @@ private function __construct(HashContext $internal_context) /** * Initialize an incremental hashing context. * + * @param non-empty-string $algorithm + * * @throws Psl\Exception\InvariantViolationException If the given algorithm is unsupported. * * @pure */ public static function forAlgorithm(string $algorithm): Context { - /** @psalm-suppress ImpureFunctionCall */ - Psl\invariant( - Iter\contains(algorithms(), $algorithm), - 'Expected a valid hashing algorithm, "%s" given.', - $algorithm, - ); + if (!in_array($algorithm, namespace\algorithms(), true)) { + Psl\invariant_violation('Expected a valid hashing algorithm, "%s" given.', $algorithm); + } + $internal_context = hash_init($algorithm); return new self($internal_context); @@ -60,20 +59,19 @@ public static function forAlgorithm(string $algorithm): Context /** * Initialize an incremental HMAC hashing context. * + * @param non-empty-string $algorithm + * @param non-empty-string $key + * * @throws Psl\Exception\InvariantViolationException If the given algorithm is unsupported. * * @pure */ public static function hmac(string $algorithm, string $key): Context { - /** @psalm-suppress ImpureFunctionCall */ - Psl\invariant( - Iter\contains(Hmac\algorithms(), $algorithm), - 'Expected a hashing algorithms suitable for HMAC, "%s" given.', - $algorithm - ); - - Psl\invariant(!Str\is_empty($key), 'Expected a non-empty shared secret key.'); + + if (!in_array($algorithm, Hmac\algorithms(), true)) { + Psl\invariant_violation('Expected a hashing algorithms suitable for HMAC, "%s" given.', $algorithm); + } $internal_context = hash_init($algorithm, HASH_HMAC, $key); diff --git a/src/Psl/Hash/Hmac/hash.php b/src/Psl/Hash/Hmac/hash.php index 851ed1559..8cad7db8c 100644 --- a/src/Psl/Hash/Hmac/hash.php +++ b/src/Psl/Hash/Hmac/hash.php @@ -10,6 +10,9 @@ /** * Generate a keyed hash value using the HMAC method. * + * @param non-empty-string $algorithm + * @param non-empty-string $key + * * @throws Psl\Exception\InvariantViolationException If the given algorithm is unsupported. * @throws Hash\Exception\RuntimeException If unable to pump data into the hashing context. * diff --git a/src/Psl/Hash/hash.php b/src/Psl/Hash/hash.php index 71b2ad1a0..6908e315e 100644 --- a/src/Psl/Hash/hash.php +++ b/src/Psl/Hash/hash.php @@ -9,6 +9,8 @@ /** * Generate a hash value (message digest). * + * @param non-empty-string $algorithm + * * @throws Psl\Exception\InvariantViolationException If the given algorithm is unsupported. * @throws Exception\RuntimeException If unable to pump data into the hashing context. * diff --git a/src/Psl/Iter/Iterator.php b/src/Psl/Iter/Iterator.php index 0ed4e8c23..b355ec6de 100644 --- a/src/Psl/Iter/Iterator.php +++ b/src/Psl/Iter/Iterator.php @@ -39,6 +39,13 @@ final class Iterator implements Countable, SeekableIterator */ private int $position = 0; + /** + * The size of the generator. + * + * @var int<0, max> + */ + private ?int $count = null; + /** * @param Generator $generator */ @@ -221,7 +228,13 @@ public function count(): int $this->generator = null; } + if (null !== $this->count) { + return $this->count; + } + /** @var int<0, max> */ - return count($this->entries); + $this->count = count($this->entries); + + return $this->count; } } diff --git a/src/Psl/Iter/count.php b/src/Psl/Iter/count.php index f028a869f..379b2625c 100644 --- a/src/Psl/Iter/count.php +++ b/src/Psl/Iter/count.php @@ -17,10 +17,13 @@ * @template T * * @param iterable $iterable + * + * @return int<0, max> */ function count(iterable $iterable): int { if (is_countable($iterable)) { + /** @var int<0, max> */ return \count($iterable); } diff --git a/src/Psl/PseudoRandom/int.php b/src/Psl/PseudoRandom/int.php index 6457bd551..e7ce56646 100644 --- a/src/Psl/PseudoRandom/int.php +++ b/src/Psl/PseudoRandom/int.php @@ -18,7 +18,9 @@ */ function int(int $min = Math\INT64_MIN, int $max = Math\INT64_MAX): int { - Psl\invariant($min <= $max, 'Expected $min (%d) to be less than or equal to $max (%d).', $min, $max); + if ($min > $max) { + Psl\invariant_violation('Expected $min (%d) to be less than or equal to $max (%d).', $min, $max); + } return mt_rand($min, $max); } diff --git a/src/Psl/Regex/split.php b/src/Psl/Regex/split.php index d85c401f6..f45e21aa6 100644 --- a/src/Psl/Regex/split.php +++ b/src/Psl/Regex/split.php @@ -15,8 +15,8 @@ * Split $subject by $pattern. * * @param non-empty-string $pattern The pattern to split $subject by. - * @param null|int $limit If specified, then only substrings up to limit are - * returned with the rest of the string being placed in the last substring. + * @param null|int<0, max> $limit If specified, then only substrings up to limit are + * returned with the rest of the string being placed in the last substring. * * @throws Exception\InvalidPatternException If $pattern is invalid. * @throws Exception\RuntimeException In case of an unexpected error. @@ -28,7 +28,6 @@ */ function split(string $subject, string $pattern, ?int $limit = null): array { - Psl\invariant($limit === null || $limit > 0, '$limit must be a positive integer.'); $result = Internal\call_preg( 'preg_split', static fn() => preg_split($pattern, $subject, $limit ?? -1, PREG_SPLIT_NO_EMPTY), diff --git a/src/Psl/SecureRandom/bytes.php b/src/Psl/SecureRandom/bytes.php index 0ab1f7c74..46599c45a 100644 --- a/src/Psl/SecureRandom/bytes.php +++ b/src/Psl/SecureRandom/bytes.php @@ -5,7 +5,6 @@ namespace Psl\SecureRandom; use Exception as PHPException; -use Psl; use Psl\Str; use Psl\Type; @@ -14,16 +13,14 @@ /** * Returns a cryptographically secure random bytes. * - * @param 0|positive-int $length The number of bytes to generate. + * @param int<0, max> $length The number of bytes to generate. * * @throws Exception\InsufficientEntropyException If it was not possible to gather sufficient entropy. - * @throws Psl\Exception\InvariantViolationException If $length is negative. * * @psalm-external-mutation-free */ function bytes(int $length): string { - Psl\invariant($length >= 0, 'Expected a non-negative length.'); if (0 === $length) { return ''; } diff --git a/src/Psl/SecureRandom/int.php b/src/Psl/SecureRandom/int.php index e8ef53d70..a18c49c01 100644 --- a/src/Psl/SecureRandom/int.php +++ b/src/Psl/SecureRandom/int.php @@ -22,7 +22,13 @@ */ function int(int $min = Math\INT64_MIN, int $max = Math\INT64_MAX): int { - Psl\invariant($min <= $max, 'Expected $min (%d) to be less than or equal to $max (%d).', $min, $max); + if ($max < $min) { + Psl\invariant_violation('Expected $min (%d) to be less than or equal to $max (%d).', $min, $max); + } + + if ($min === $max) { + return $min; + } try { return random_int($min, $max); diff --git a/src/Psl/SecureRandom/string.php b/src/Psl/SecureRandom/string.php index 399db14ae..57a220c92 100644 --- a/src/Psl/SecureRandom/string.php +++ b/src/Psl/SecureRandom/string.php @@ -18,17 +18,15 @@ * If the alphabet argument is not specified, the returned string will be composed of * the alphanumeric characters. * - * @param 0|positive-int $length The length of the string to generate. + * @param int<0, max> $length The length of the string to generate. * * @throws Exception\InsufficientEntropyException If it was not possible to gather sufficient entropy. - * @throws Psl\Exception\InvariantViolationException If a negative $length is given, or $alphabet length is - * outside the [2^1, 2^56] range. + * @throws Psl\Exception\InvariantViolationException If $alphabet length is outside the [2^1, 2^56] range. * * @psalm-external-mutation-free */ function string(int $length, ?string $alphabet = null): string { - Psl\invariant($length >= 0, 'Expected a non-negative length.', $length); if (0 === $length) { return ''; } diff --git a/src/Psl/Str/Byte/after_last.php b/src/Psl/Str/Byte/after_last.php index 684062b86..6601ca09a 100644 --- a/src/Psl/Str/Byte/after_last.php +++ b/src/Psl/Str/Byte/after_last.php @@ -8,7 +8,6 @@ /** * @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds. - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. * * @pure */ diff --git a/src/Psl/Str/Byte/chunk.php b/src/Psl/Str/Byte/chunk.php index 61bacbcbc..315d98d74 100644 --- a/src/Psl/Str/Byte/chunk.php +++ b/src/Psl/Str/Byte/chunk.php @@ -4,16 +4,12 @@ namespace Psl\Str\Byte; -use Psl; - use function str_split; /** * Returns an array containing the string split into chunks of the given size. * - * @param int $chunk_length maximum length of the chunk - * - * @throws Psl\Exception\InvariantViolationException If $chunk_length is negative. + * @param positive-int $chunk_length maximum length of the chunk * * @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 @@ -26,7 +22,6 @@ */ function chunk(string $string, int $chunk_length = 1): array { - Psl\invariant($chunk_length >= 1, 'Expected a non-negative chunk size.'); if ('' === $string) { return []; } diff --git a/src/Psl/Str/Byte/search.php b/src/Psl/Str/Byte/search.php index a03408828..907188218 100644 --- a/src/Psl/Str/Byte/search.php +++ b/src/Psl/Str/Byte/search.php @@ -19,6 +19,8 @@ * @pure * * @throws Psl\Exception\InvariantViolationException If $offset is out-of-bounds. + * + * @return null|int<0, max> */ function search(string $haystack, string $needle, int $offset = 0): ?int { @@ -28,5 +30,6 @@ function search(string $haystack, string $needle, int $offset = 0): ?int return null; } + /** @var null|int<0, max> */ return false === ($pos = strpos($haystack, $needle, $offset)) ? null : $pos; } diff --git a/src/Psl/Str/Byte/search_ci.php b/src/Psl/Str/Byte/search_ci.php index b9af7f9a1..330a53872 100644 --- a/src/Psl/Str/Byte/search_ci.php +++ b/src/Psl/Str/Byte/search_ci.php @@ -19,6 +19,8 @@ * @pure * * @throws Psl\Exception\InvariantViolationException If $offset is out-of-bounds. + * + * @return null|int<0, max> */ function search_ci(string $haystack, string $needle, int $offset = 0): ?int { @@ -28,5 +30,6 @@ function search_ci(string $haystack, string $needle, int $offset = 0): ?int return null; } + /** @var null|int<0, max> */ return false === ($pos = stripos($haystack, $needle, $offset)) ? null : $pos; } diff --git a/src/Psl/Str/Byte/search_last.php b/src/Psl/Str/Byte/search_last.php index 55ea26754..6f6388a0a 100644 --- a/src/Psl/Str/Byte/search_last.php +++ b/src/Psl/Str/Byte/search_last.php @@ -21,6 +21,8 @@ * @pure * * @throws Psl\Exception\InvariantViolationException If $offset is out-of-bounds. + * + * @return null|int<0, max> */ function search_last(string $haystack, string $needle, int $offset = 0): ?int { @@ -31,5 +33,6 @@ function search_last(string $haystack, string $needle, int $offset = 0): ?int $haystack_length = length($haystack); Psl\invariant($offset >= -$haystack_length && $offset <= $haystack_length, 'Offset is out-of-bounds.'); + /** @var null|int<0, max> */ return false === ($pos = strrpos($haystack, $needle, $offset)) ? null : $pos; } diff --git a/src/Psl/Str/Byte/search_last_ci.php b/src/Psl/Str/Byte/search_last_ci.php index 34e28a0a7..56f6753d1 100644 --- a/src/Psl/Str/Byte/search_last_ci.php +++ b/src/Psl/Str/Byte/search_last_ci.php @@ -19,6 +19,8 @@ * @pure * * @throws Psl\Exception\InvariantViolationException If $offset is out-of-bounds. + * + * @return null|int<0, max> */ function search_last_ci(string $haystack, string $needle, int $offset = 0): ?int { @@ -29,5 +31,6 @@ function search_last_ci(string $haystack, string $needle, int $offset = 0): ?int $haystack_length = length($haystack); Psl\invariant($offset >= -$haystack_length && $offset <= $haystack_length, 'Offset is out-of-bounds.'); + /** @var null|int<0, max> */ return false === ($pos = strripos($haystack, $needle, $offset)) ? null : $pos; } diff --git a/src/Psl/Str/Byte/slice.php b/src/Psl/Str/Byte/slice.php index 9dd00e464..d8a621366 100644 --- a/src/Psl/Str/Byte/slice.php +++ b/src/Psl/Str/Byte/slice.php @@ -15,13 +15,14 @@ * If no length is given, the slice will contain the rest of the * string. If the length is zero, the empty string will be returned. * + * @param int<0, max> $length + * * @pure * - * @throws Psl\Exception\InvariantViolationException If $length is negative, or the $offset is out-of-bounds. + * @throws Psl\Exception\InvariantViolationException If $offset is out-of-bounds. */ function slice(string $string, int $offset, ?int $length = null): string { - Psl\invariant(null === $length || $length >= 0, 'Expected a non-negative length.'); $offset = Psl\Internal\validate_offset($offset, length($string)); return null === $length ? substr($string, $offset) : substr($string, $offset, $length); diff --git a/src/Psl/Str/Byte/splice.php b/src/Psl/Str/Byte/splice.php index ce3a8d2a1..5d27a646c 100644 --- a/src/Psl/Str/Byte/splice.php +++ b/src/Psl/Str/Byte/splice.php @@ -16,13 +16,14 @@ * remainder of the string will be replaced. If the length is zero, the * replacement will be inserted at the offset. * + * @param int<0, max> $length + * * @pure * - * @throws Psl\Exception\InvariantViolationException If $length is negative, or $offset is out-of-bounds. + * @throws Psl\Exception\InvariantViolationException If $offset is out-of-bounds. */ function splice(string $string, string $replacement, int $offset, ?int $length = null): string { - Psl\invariant(null === $length || $length >= 0, 'Expected a non-negative length.'); $offset = Psl\Internal\validate_offset($offset, length($string)); return null === $length diff --git a/src/Psl/Str/Byte/split.php b/src/Psl/Str/Byte/split.php index 94fa12e29..013566ab9 100644 --- a/src/Psl/Str/Byte/split.php +++ b/src/Psl/Str/Byte/split.php @@ -4,9 +4,6 @@ namespace Psl\Str\Byte; -use Psl; -use Psl\Type; - use function explode; /** @@ -16,7 +13,7 @@ * If the limit is provided, the array will only contain that many elements, where * the last element is the remainder of the string. * - * @throws Psl\Exception\InvariantViolationException If $limit is negative. + * @param null|positive-int $limit * * @return list * @@ -33,25 +30,20 @@ function split(string $string, string $delimiter, ?int $limit = null): array return [$string]; } - Psl\invariant($limit > 1, 'Expected a non-negative limit.'); - $result = chunk(slice($string, 0, $limit - 1)); - $result[] = slice($string, $limit - 1); + /** @var positive-int $length */ + $length = $limit - 1; + + $result = chunk(slice($string, 0, $length)); + $result[] = slice($string, $length); return $result; } if (null === $limit) { - /** @var list|false $result */ - $result = explode($delimiter, $string); - } else { - /** @var list|false $result */ - $result = explode($delimiter, $string, $limit); + /** @var list */ + return explode($delimiter, $string); } - /** - * @psalm-suppress MissingThrowsDocblock - should not throw - * @psalm-suppress ImpureFunctionCall - see https://github.com/azjezz/psl/issues/130 - * @psalm-suppress ImpureMethodCall - see https://github.com/azjezz/psl/issues/130 - */ - return Type\vec(Type\string())->coerce($result); + /** @var list */ + return explode($delimiter, $string, $limit); } diff --git a/src/Psl/Str/Byte/strip_suffix.php b/src/Psl/Str/Byte/strip_suffix.php index 5464a0354..1b7b17fa4 100644 --- a/src/Psl/Str/Byte/strip_suffix.php +++ b/src/Psl/Str/Byte/strip_suffix.php @@ -12,10 +12,31 @@ */ function strip_suffix(string $string, string $suffix): string { - if ('' === $suffix || !ends_with($string, $suffix)) { + if ($string === '' || $suffix === '') { return $string; } - /** @psalm-suppress MissingThrowsDocblock - $offset is within-bounds. */ - return slice($string, 0, length($string) - length($suffix)); + if ($string === $suffix) { + return ''; + } + + $suffix_length = length($suffix); + $string_length = length($string); + // if $suffix_length is greater than $string_length, return $string as it can't contain $suffix. + // if $suffix_length and $string_length are the same, return $string as $suffix is not $string. + if ($suffix_length >= $string_length) { + return $string; + } + + if (!ends_with($string, $suffix)) { + return $string; + } + + /** + * $string_length is greater than $suffix_length, so the result is always int<0, max>. + * + * @psalm-suppress ArgumentTypeCoercion + * @psalm-suppress InvalidArgument + */ + return slice($string, 0, $string_length - $suffix_length); } diff --git a/src/Psl/Str/Grapheme/after_last.php b/src/Psl/Str/Grapheme/after_last.php index d66afb566..57fb49205 100644 --- a/src/Psl/Str/Grapheme/after_last.php +++ b/src/Psl/Str/Grapheme/after_last.php @@ -8,7 +8,6 @@ /** * @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds. - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. * * @pure */ diff --git a/src/Psl/Str/Grapheme/ends_with.php b/src/Psl/Str/Grapheme/ends_with.php index b3321fe46..6df01b904 100644 --- a/src/Psl/Str/Grapheme/ends_with.php +++ b/src/Psl/Str/Grapheme/ends_with.php @@ -11,7 +11,7 @@ * * @pure * - * @throws Psl\Exception\InvariantViolationException If unable to convert $string to UTF-16, + * @throws Psl\Exception\InvariantViolationException If unable to convert $string or $suffix to UTF-16, * or split it into graphemes. */ function ends_with(string $string, string $suffix): bool diff --git a/src/Psl/Str/Grapheme/ends_with_ci.php b/src/Psl/Str/Grapheme/ends_with_ci.php index f23688ab1..9a16acd98 100644 --- a/src/Psl/Str/Grapheme/ends_with_ci.php +++ b/src/Psl/Str/Grapheme/ends_with_ci.php @@ -11,7 +11,7 @@ * * @pure * - * @throws Psl\Exception\InvariantViolationException If unable to convert $string to UTF-16, + * @throws Psl\Exception\InvariantViolationException If unable to convert $string or to UTF-16, * or split it into graphemes. */ function ends_with_ci(string $string, string $suffix): bool diff --git a/src/Psl/Str/Grapheme/search.php b/src/Psl/Str/Grapheme/search.php index b05fc31bb..a76ad493c 100644 --- a/src/Psl/Str/Grapheme/search.php +++ b/src/Psl/Str/Grapheme/search.php @@ -20,6 +20,8 @@ * @pure * * @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds. + * + * @return null|int<0, max> */ function search(string $haystack, string $needle, int $offset = 0): ?int { @@ -29,6 +31,7 @@ function search(string $haystack, string $needle, int $offset = 0): ?int $offset = Psl\Internal\validate_offset($offset, length($haystack)); + /** @var null|int<0, max> */ return false === ($pos = grapheme_strpos($haystack, $needle, $offset)) ? null : $pos; diff --git a/src/Psl/Str/Grapheme/search_ci.php b/src/Psl/Str/Grapheme/search_ci.php index f84b83096..44231bbf9 100644 --- a/src/Psl/Str/Grapheme/search_ci.php +++ b/src/Psl/Str/Grapheme/search_ci.php @@ -20,6 +20,8 @@ * @pure * * @throws Psl\Exception\InvariantViolationException If $offset is out-of-bounds. + * + * @return null|int<0, max> */ function search_ci(string $haystack, string $needle, int $offset = 0): ?int { @@ -29,6 +31,7 @@ function search_ci(string $haystack, string $needle, int $offset = 0): ?int $offset = Psl\Internal\validate_offset($offset, length($haystack)); + /** @var null|int<0, max> */ return false === ($pos = grapheme_stripos($haystack, $needle, $offset)) ? null : $pos; diff --git a/src/Psl/Str/Grapheme/search_last.php b/src/Psl/Str/Grapheme/search_last.php index 1abedc5dc..4f4de1971 100644 --- a/src/Psl/Str/Grapheme/search_last.php +++ b/src/Psl/Str/Grapheme/search_last.php @@ -21,6 +21,8 @@ * @pure * * @throws Psl\Exception\InvariantViolationException If the $offset is out-of-bounds. + * + * @return null|int<0, max> */ function search_last(string $haystack, string $needle, int $offset = 0): ?int { @@ -31,6 +33,7 @@ function search_last(string $haystack, string $needle, int $offset = 0): ?int $haystack_length = length($haystack); Psl\invariant($offset >= -$haystack_length && $offset <= $haystack_length, 'Offset is out-of-bounds.'); + /** @var null|int<0, max> */ return false === ($pos = grapheme_strrpos($haystack, $needle, $offset)) ? null : $pos; diff --git a/src/Psl/Str/Grapheme/search_last_ci.php b/src/Psl/Str/Grapheme/search_last_ci.php index 00041848a..f913d2085 100644 --- a/src/Psl/Str/Grapheme/search_last_ci.php +++ b/src/Psl/Str/Grapheme/search_last_ci.php @@ -21,6 +21,8 @@ * @pure * * @throws Psl\Exception\InvariantViolationException If the offset is out-of-bounds. + * + * @return null|int<0, max> */ function search_last_ci(string $haystack, string $needle, int $offset = 0): ?int { @@ -31,6 +33,7 @@ function search_last_ci(string $haystack, string $needle, int $offset = 0): ?int $haystack_length = length($haystack); Psl\invariant($offset >= -$haystack_length && $offset <= $haystack_length, 'Offset is out-of-bounds.'); + /** @var null|int<0, max> */ return false === ($pos = grapheme_strripos($haystack, $needle, $offset)) ? null : $pos; diff --git a/src/Psl/Str/Grapheme/slice.php b/src/Psl/Str/Grapheme/slice.php index 68e9d531f..9bf652958 100644 --- a/src/Psl/Str/Grapheme/slice.php +++ b/src/Psl/Str/Grapheme/slice.php @@ -14,13 +14,14 @@ * * If the length is zero, the empty string will be returned. * + * @param null|int<0, max> $length + * * @pure * - * @throws Psl\Exception\InvariantViolationException If a negative $length is given, or $offset is out-of-bounds. + * @throws Psl\Exception\InvariantViolationException If $offset is out-of-bounds. */ function slice(string $string, int $offset, ?int $length = null): string { - Psl\invariant(null === $length || $length >= 0, 'Expected a non-negative length.'); $string_length = length($string); $offset = Psl\Internal\validate_offset($offset, $string_length); diff --git a/src/Psl/Str/Grapheme/strip_suffix.php b/src/Psl/Str/Grapheme/strip_suffix.php index 641d86292..1a59f9706 100644 --- a/src/Psl/Str/Grapheme/strip_suffix.php +++ b/src/Psl/Str/Grapheme/strip_suffix.php @@ -4,18 +4,44 @@ namespace Psl\Str\Grapheme; +use Psl; + /** * Returns the string with the given suffix removed, or the string itself if * it doesn't end with the suffix. * + * @throws Psl\Exception\InvariantViolationException If unable to convert $string, or $suffix to UTF-16, + * or split them into graphemes. + * * @pure */ function strip_suffix(string $string, string $suffix): string { - if ('' === $suffix || !ends_with($string, $suffix)) { + if ($string === '' || $suffix === '') { + return $string; + } + + if ($string === $suffix) { + return ''; + } + + $suffix_length = length($suffix); + $string_length = length($string); + // if $suffix_length is greater than $string_length, return $string as it can't contain $suffix. + // if $suffix_length and $string_length are the same, return $string as $suffix is not $string. + if ($suffix_length >= $string_length) { + return $string; + } + + if (!ends_with($string, $suffix)) { return $string; } - /** @psalm-suppress MissingThrowsDocblock */ - return slice($string, 0, length($string) - length($suffix)); + /** + * $string_length is greater than $suffix_length, so the result is always int<0, max>. + * + * @psalm-suppress ArgumentTypeCoercion + * @psalm-suppress InvalidArgument + */ + return slice($string, 0, $string_length - $suffix_length); } diff --git a/src/Psl/Str/chunk.php b/src/Psl/Str/chunk.php index 9a5bdbd7a..0b4a43be4 100644 --- a/src/Psl/Str/chunk.php +++ b/src/Psl/Str/chunk.php @@ -4,8 +4,6 @@ namespace Psl\Str; -use Psl; - /** * Returns an array containing the string split into chunks of the given size. * @@ -23,34 +21,22 @@ * Str\chunk('مرحبا', 2) * => Arr('مر', 'حب', 'ا') * - * @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 ). + * @param int<1, 65535> $chunk_length maximum length of the chunk * - * @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 - * one character in length. - * If the $chunk_length length exceeds the length of string, the entire string is returned - * as the first (and only) array element. + * @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 + * one character in length. + * If the $chunk_length length exceeds the length of string, the entire string is returned + * as the first (and only) array element. * * @pure */ 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) { return []; } - Psl\invariant(65535 >= $chunk_length, 'Maximum chunk length must not exceed 65535.'); - - - /** - * @psalm-suppress UndefinedPropertyFetch - * @psalm-suppress MixedArgument - * - * @var list - */ + /** @var list */ return mb_str_split($string, $chunk_length, $encoding->value); } diff --git a/src/Psl/Str/convert_encoding.php b/src/Psl/Str/convert_encoding.php index 1847afd68..96dd3e715 100644 --- a/src/Psl/Str/convert_encoding.php +++ b/src/Psl/Str/convert_encoding.php @@ -4,22 +4,14 @@ namespace Psl\Str; -use Psl; -use Psl\Internal; - use function mb_convert_encoding; /** * Convert character encoding of the giving string. * * @pure - * - * @throws Psl\Exception\InvariantViolationException If either one of $from_encoding and $to_encoding is invalid. */ -function convert_encoding(string $string, string $from_encoding, string $to_encoding): string +function convert_encoding(string $string, Encoding $from_encoding, Encoding $to_encoding): string { - Psl\invariant(Internal\is_encoding_valid($from_encoding), '$from_encoding is invalid.'); - Psl\invariant(Internal\is_encoding_valid($to_encoding), '$to_encoding is invalid.'); - - return mb_convert_encoding($string, $to_encoding, $from_encoding); + return mb_convert_encoding($string, $to_encoding->value, $from_encoding->value); } diff --git a/src/Psl/Str/lowercase.php b/src/Psl/Str/lowercase.php index 5b84fd673..cce3a9e6f 100644 --- a/src/Psl/Str/lowercase.php +++ b/src/Psl/Str/lowercase.php @@ -4,8 +4,6 @@ namespace Psl\Str; -use Psl; - use function mb_strtolower; /** @@ -28,14 +26,8 @@ * => Str('سيف') * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ function lowercase(string $string, Encoding $encoding = Encoding::UTF_8): string { - /** - * @psalm-suppress UndefinedPropertyFetch - * @psalm-suppress MixedArgument - */ return mb_strtolower($string, $encoding->value); } diff --git a/src/Psl/Str/metaphone.php b/src/Psl/Str/metaphone.php index 72f9eba33..15a6e916f 100644 --- a/src/Psl/Str/metaphone.php +++ b/src/Psl/Str/metaphone.php @@ -4,17 +4,13 @@ namespace Psl\Str; -use Psl; - use function metaphone as php_metaphone; /** * Calculate the metaphone key of a string. * - * @param int $phonemes This parameter restricts the returned metaphone key to phonemes characters in length. - * the default value of 0 means no restriction. - * - * @throws Psl\Exception\InvariantViolationException If $phonemes is negative. + * @param int<0, max> $phonemes This parameter restricts the returned metaphone key to phonemes characters in length. + * the default value of 0 means no restriction. * * @return string the metaphone key as a string * @@ -22,7 +18,5 @@ */ function metaphone(string $string, int $phonemes = 0): string { - Psl\invariant($phonemes >= 0, 'Expected non-negative phonemes, got %d.', $phonemes); - return php_metaphone($string, $phonemes); } diff --git a/src/Psl/Str/ord.php b/src/Psl/Str/ord.php index 77f014111..3830fd5c7 100644 --- a/src/Psl/Str/ord.php +++ b/src/Psl/Str/ord.php @@ -4,8 +4,6 @@ namespace Psl\Str; -use Psl; - use function mb_ord; /** @@ -20,14 +18,8 @@ * => Int(1604) * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ function ord(string $character, Encoding $encoding = Encoding::UTF_8): int { - /** - * @psalm-suppress UndefinedPropertyFetch - * @psalm-suppress MixedArgument - */ return mb_ord($character, $encoding->value); } diff --git a/src/Psl/Str/repeat.php b/src/Psl/Str/repeat.php index 16261ce48..d94dee89b 100644 --- a/src/Psl/Str/repeat.php +++ b/src/Psl/Str/repeat.php @@ -4,8 +4,6 @@ namespace Psl\Str; -use Psl; - use function str_repeat; /** @@ -21,13 +19,11 @@ * Str\repeat('?', 5) * => Str('?????') * - * @pure + * @param int<0, max> $multiplier * - * @throws Psl\Exception\InvariantViolationException If $multiplier is negative. + * @pure */ function repeat(string $string, int $multiplier): string { - Psl\invariant($multiplier >= 0, 'Expected a non-negative multiplier'); - return str_repeat($string, $multiplier); } diff --git a/src/Psl/Str/replace_ci.php b/src/Psl/Str/replace_ci.php index 55fbf96f9..d548ba265 100644 --- a/src/Psl/Str/replace_ci.php +++ b/src/Psl/Str/replace_ci.php @@ -4,8 +4,6 @@ namespace Psl\Str; -use Psl; - use function preg_quote; use function preg_split; @@ -14,8 +12,6 @@ * `$replacement` (case-insensitive). * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ function replace_ci(string $haystack, string $needle, string $replacement, Encoding $encoding = Encoding::UTF_8): string { diff --git a/src/Psl/Str/split.php b/src/Psl/Str/split.php index c27c8a2a9..543965b2c 100644 --- a/src/Psl/Str/split.php +++ b/src/Psl/Str/split.php @@ -4,7 +4,6 @@ namespace Psl\Str; -use Psl; use Psl\Math; /** @@ -16,9 +15,6 @@ * * @param null|positive-int $limit * - * @throws Psl\Exception\InvariantViolationException If a negative $limit is given. - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. - * * @return list * * @pure @@ -34,11 +30,11 @@ function split(string $string, string $delimiter, ?int $limit = null, Encoding $ return [$string]; } - /** - * @psalm-suppress ArgumentTypeCoercion - $limit is int<1, max> here - */ - $result = chunk(slice($string, 0, $limit - 1, $encoding), 1, $encoding); - $result[] = slice($string, $limit - 1, null, $encoding); + /** @var positive-int $length */ + $length = $limit - 1; + + $result = chunk(slice($string, 0, $length, $encoding), 1, $encoding); + $result[] = slice($string, $length, null, $encoding); return $result; } @@ -48,6 +44,11 @@ function split(string $string, string $delimiter, ?int $limit = null, Encoding $ $tail = $string; $chunks = []; + /** + * $offset is within bounded. + * + * @psalm-suppress MissingThrowsDocblock + */ $position = search($tail, $delimiter, 0, $encoding); while (1 < $limit && null !== $position) { $result = slice($tail, 0, $position, $encoding); @@ -55,7 +56,12 @@ function split(string $string, string $delimiter, ?int $limit = null, Encoding $ $tail = slice($tail, length($result, $encoding) + length($delimiter, $encoding), null, $encoding); $limit--; - $position = search($tail, $delimiter, 0, $encoding); + /** + * $offset is within bounded. + * + * @psalm-suppress MissingThrowsDocblock + */ + $position = search($tail, $delimiter, encoding: $encoding); } $chunks[] = $tail; diff --git a/src/Psl/Str/width.php b/src/Psl/Str/width.php index 0c14c6563..d145db885 100644 --- a/src/Psl/Str/width.php +++ b/src/Psl/Str/width.php @@ -4,22 +4,14 @@ namespace Psl\Str; -use Psl; - use function mb_strwidth; /** * Return width of length. * * @pure - * - * @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided. */ function width(string $string, Encoding $encoding = Encoding::UTF_8): int { - /** - * @psalm-suppress UndefinedPropertyFetch - * @psalm-suppress MixedArgument - */ return mb_strwidth($string, $encoding->value); } diff --git a/src/Psl/Vec/reproduce.php b/src/Psl/Vec/reproduce.php index 0d5e22cf1..5ad1e7c21 100644 --- a/src/Psl/Vec/reproduce.php +++ b/src/Psl/Vec/reproduce.php @@ -4,8 +4,6 @@ namespace Psl\Vec; -use Psl; - /** * Produce a list of values generated using the given factory. * @@ -16,16 +14,13 @@ * * @template T * + * @param positive-int $size * @param (callable(int): T) $factory * - * @throws Psl\Exception\InvariantViolationException If $size is lower than 1. - * * @return list */ function reproduce(int $size, callable $factory): array { - Psl\invariant($size >= 1, 'The number of times you want to reproduce must be at least 1.'); - $result = []; for ($i = 1; $i <= $size; $i++) { $result[] = $factory($i); diff --git a/tests/unit/Env/CurrentDirTest.php b/tests/unit/Env/CurrentDirTest.php index c7c1f6351..950b330f4 100644 --- a/tests/unit/Env/CurrentDirTest.php +++ b/tests/unit/Env/CurrentDirTest.php @@ -6,15 +6,27 @@ use PHPUnit\Framework\TestCase; use Psl\Env; +use Psl\Exception\InvariantViolationException; final class CurrentDirTest extends TestCase { public function testCurrentDir(): void { - static::assertSame(getcwd(), Env\current_dir()); + $cwd = Env\current_dir(); + static::assertSame(getcwd(), $cwd); Env\set_current_dir(__DIR__); static::assertSame(__DIR__, Env\current_dir()); + + Env\set_current_dir($cwd); + } + + public function testChangeCurrentDirectoryThrowsIfUnableTo(): void + { + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('Unable to change directory'); + + Env\set_current_dir('/foo/bar/baz/qux'); } } diff --git a/tests/unit/Env/GetVarTest.php b/tests/unit/Env/GetVarTest.php index 874887e99..763394ce9 100644 --- a/tests/unit/Env/GetVarTest.php +++ b/tests/unit/Env/GetVarTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Psl\Env; +use Psl\Exception\InvariantViolationException; final class GetVarTest extends TestCase { @@ -22,4 +23,20 @@ public function testGetVar(): void Env\remove_var('FOO'); } + + public function testRemoveVarThrowsIfTheKeyIsInvalid(): void + { + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('Invalid environment variable key provided.'); + + Env\get_var('a=b'); + } + + public function testRemoveVarThrowsIfTheKeyContainsNUL(): void + { + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('Invalid environment variable key provided.'); + + Env\get_var("\0"); + } } diff --git a/tests/unit/Env/RemoveVarTest.php b/tests/unit/Env/RemoveVarTest.php index 513a99fa3..58d21387f 100644 --- a/tests/unit/Env/RemoveVarTest.php +++ b/tests/unit/Env/RemoveVarTest.php @@ -34,14 +34,6 @@ public function testRemoveVarThrowsIfTheKeyIsInvalid(): void Env\remove_var('a=b'); } - public function testRemoveVarThrowsIfTheKeyIsEmpty(): void - { - $this->expectException(InvariantViolationException::class); - $this->expectExceptionMessage('Invalid environment variable key provided.'); - - Env\remove_var(''); - } - public function testRemoveVarThrowsIfTheKeyContainsNUL(): void { $this->expectException(InvariantViolationException::class); diff --git a/tests/unit/Env/SetVarTest.php b/tests/unit/Env/SetVarTest.php new file mode 100644 index 000000000..7461cc7ef --- /dev/null +++ b/tests/unit/Env/SetVarTest.php @@ -0,0 +1,36 @@ +expectException(InvariantViolationException::class); + $this->expectExceptionMessage('Invalid environment variable key provided.'); + + Env\set_var('a=b', 'foo'); + } + + public function testSetVarThrowsIfTheKeyContainsNUL(): void + { + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('Invalid environment variable key provided.'); + + Env\set_var("\0", 'foo'); + } + + public function testSetVarThrowsIfTheValueContainsNUL(): void + { + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('Invalid environment variable value provided.'); + + Env\set_var('foo', "\0"); + } +} diff --git a/tests/unit/Filesystem/FileTest.php b/tests/unit/Filesystem/FileTest.php index f8e53ed35..c8571619d 100644 --- a/tests/unit/Filesystem/FileTest.php +++ b/tests/unit/Filesystem/FileTest.php @@ -11,6 +11,8 @@ use function time; +use const PHP_OS_FAMILY; + final class FileTest extends AbstractFilesystemTest { protected string $function = 'file'; @@ -180,6 +182,124 @@ public function testFileModificationAndAccessTime(): void static::assertSame($access_time, Filesystem\get_access_time($file)); } + public function testGetModificationTimeOfNonExistingFile(): void + { + $filename = Str\join([$this->directory, 'non-existing'], Filesystem\SEPARATOR); + + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('File "' . $filename . '" does not exist.'); + + Filesystem\get_modification_time($filename); + } + + public function testGetAccessTimeOfNonExistingFile(): void + { + $filename = Str\join([$this->directory, 'non-existing'], Filesystem\SEPARATOR); + + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('File "' . $filename . '" does not exist.'); + + Filesystem\get_access_time($filename); + } + + public function testGetChangeTimeOfNonExistingFile(): void + { + $filename = Str\join([$this->directory, 'non-existing'], Filesystem\SEPARATOR); + + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('File "' . $filename . '" does not exist.'); + + Filesystem\get_change_time($filename); + } + + public function testGetInodeOfNonExistingFile(): void + { + $filename = Str\join([$this->directory, 'non-existing'], Filesystem\SEPARATOR); + + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('File "' . $filename . '" does not exist.'); + + Filesystem\get_inode($filename); + } + + public function testFileSizeThrowsForNonExistingFile(): void + { + $filename = Str\join([$this->directory, 'non-existing'], Filesystem\SEPARATOR); + + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('File "' . $filename . '" does not exist, or is not readable.'); + + Filesystem\file_size($filename); + } + + public function testCopyThrowsForNonExistingFile(): void + { + $filename = Str\join([$this->directory, 'non-existing'], Filesystem\SEPARATOR); + + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('Source "' . $filename . '" does not exist, or is not readable.'); + + Filesystem\copy($filename, '/foo/bar'); + } + + public function testCreateSymbolicLinkThrowsForNonExistingFile(): void + { + $filename = Str\join([$this->directory, 'non-existing'], Filesystem\SEPARATOR); + + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('Source file "' . $filename . '" does not exist.'); + + Filesystem\create_symbolic_link($filename, '/foo/bar'); + } + + public function testFileSizeThrowsForNonReadableFile(): void + { + if (PHP_OS_FAMILY === 'Windows') { + // executable bit on windows. + static::markTestSkipped('Test can only be executed under *nix OS.'); + } + + $filename = Str\join([$this->directory, 'non-readable.txt'], Filesystem\SEPARATOR); + Filesystem\create_file($filename); + $permissions = Filesystem\get_permissions($filename) & 0777; + Filesystem\change_permissions($filename, 0111); + + static::assertFalse(Filesystem\is_readable($filename)); + + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('File "' . $filename . '" does not exist, or is not readable.'); + + try { + Filesystem\file_size($filename); + } finally { + Filesystem\change_permissions($filename, $permissions); + } + } + + public function testCopyThrowsForNonReadableFile(): void + { + if (PHP_OS_FAMILY === 'Windows') { + // executable bit on windows. + static::markTestSkipped('Test can only be executed under *nix OS.'); + } + + $filename = Str\join([$this->directory, 'non-readable.txt'], Filesystem\SEPARATOR); + Filesystem\create_file($filename); + $permissions = Filesystem\get_permissions($filename) & 0777; + Filesystem\change_permissions($filename, 0111); + + static::assertFalse(Filesystem\is_readable($filename)); + + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('Source "' . $filename . '" does not exist, or is not readable.'); + + try { + Filesystem\copy($filename, '/foo/bar'); + } finally { + Filesystem\change_permissions($filename, $permissions); + } + } + public function testFileAccessTime(): void { $file = Str\join([$this->directory, 'write.txt'], Filesystem\SEPARATOR); diff --git a/tests/unit/Filesystem/PathTest.php b/tests/unit/Filesystem/PathTest.php index 633253e73..abf32fda4 100644 --- a/tests/unit/Filesystem/PathTest.php +++ b/tests/unit/Filesystem/PathTest.php @@ -5,7 +5,6 @@ namespace Psl\Tests\Unit\Filesystem; use PHPUnit\Framework\TestCase; -use Psl\Exception\InvariantViolationException; use Psl\Filesystem; final class PathTest extends TestCase @@ -57,12 +56,4 @@ public function testGetDirectoryWithMultipleLevels(): void Filesystem\get_directory('/home/azjezz/Projects/psl/src/Psl', 3) ); } - - public function testGetDirectoryWithNegativeLevelThrows(): void - { - $this->expectException(InvariantViolationException::class); - $this->expectExceptionMessage('$levels must be a positive integer, -3 given.'); - - Filesystem\get_directory('/home/azjezz/Projects/psl/src/Psl', -3); - } } diff --git a/tests/unit/Filesystem/PermissionsTest.php b/tests/unit/Filesystem/PermissionsTest.php index 64c834a32..ad567b2d0 100644 --- a/tests/unit/Filesystem/PermissionsTest.php +++ b/tests/unit/Filesystem/PermissionsTest.php @@ -4,6 +4,7 @@ namespace Psl\Tests\Unit\Filesystem; +use Psl\Exception\InvariantViolationException; use Psl\Filesystem; use Psl\Str; @@ -60,4 +61,24 @@ public function testChangePermissions(): void Filesystem\change_permissions($filename, $permissions); } } + + public function testChangePermissionsThrowsForNonExistingFile(): void + { + $filename = Str\join([$this->directory, 'non-existing'], Filesystem\SEPARATOR); + + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('File "' . $filename . '" does not exist.'); + + Filesystem\change_permissions($filename, 0111); + } + + public function testGetPermissionsThrowsForNonExistingFile(): void + { + $filename = Str\join([$this->directory, 'non-existing'], Filesystem\SEPARATOR); + + $this->expectException(InvariantViolationException::class); + $this->expectExceptionMessage('File "' . $filename . '" does not exist.'); + + Filesystem\get_permissions($filename); + } } diff --git a/tests/unit/Filesystem/ReadDirectoryTest.php b/tests/unit/Filesystem/ReadDirectoryTest.php index 00c4aa813..e82c020d9 100644 --- a/tests/unit/Filesystem/ReadDirectoryTest.php +++ b/tests/unit/Filesystem/ReadDirectoryTest.php @@ -38,7 +38,7 @@ public function testReadDirectory(): void public function testReadDirectoryThrowsIfDirectoryDoesNotExist(): void { $this->expectException(InvariantViolationException::class); - $this->expectExceptionMessage('Directory "' . Env\temp_dir() . '/foo-bar-baz' . '" does not exist.'); + $this->expectExceptionMessage('Directory "' . Env\temp_dir() . '/foo-bar-baz' . '" is not a directory.'); Filesystem\read_directory(Env\temp_dir() . '/foo-bar-baz'); } diff --git a/tests/unit/Hash/ContextTest.php b/tests/unit/Hash/ContextTest.php index 92257e323..ee822574c 100644 --- a/tests/unit/Hash/ContextTest.php +++ b/tests/unit/Hash/ContextTest.php @@ -44,14 +44,6 @@ public function testHmacThrowsForInvalidAlgorithm(): void Hash\Context::hmac('base64', 'secret'); } - public function testHmacThrowsForEmptySecretKey(): void - { - $this->expectException(InvariantViolationException::class); - $this->expectExceptionMessage('Expected a non-empty shared secret key.'); - - Hash\Context::hmac('sha1', ''); - } - public function testContextIsImmutable(): void { $first = Hash\Context::forAlgorithm('md5'); diff --git a/tests/unit/Hash/Hmac/HashTest.php b/tests/unit/Hash/Hmac/HashTest.php index 08a21b10a..2918890c3 100644 --- a/tests/unit/Hash/Hmac/HashTest.php +++ b/tests/unit/Hash/Hmac/HashTest.php @@ -26,13 +26,6 @@ public function testHashThrowsForInvalidAlgorithm(): void Hmac\hash('Hello', 'base64', 'real-secret'); } - public function testHashThrowsForEmptySharedSecret(): void - { - $this->expectException(InvariantViolationException::class); - - Hmac\hash('Hello', 'sha1', ''); - } - /** * @return Generator */ diff --git a/tests/unit/SecureRandom/BytesTest.php b/tests/unit/SecureRandom/BytesTest.php index c5d2e0091..54ed062ab 100644 --- a/tests/unit/SecureRandom/BytesTest.php +++ b/tests/unit/SecureRandom/BytesTest.php @@ -5,7 +5,6 @@ namespace Psl\Tests\Unit\SecureRandom; use PHPUnit\Framework\TestCase; -use Psl\Exception; use Psl\SecureRandom; use Psl\Str; @@ -22,12 +21,4 @@ public function testBytesEarlyReturnForZeroLength(): void { static::assertSame('', SecureRandom\bytes(0)); } - - public function testBytesThrowsForNegativeLength(): void - { - $this->expectException(Exception\InvariantViolationException::class); - $this->expectExceptionMessage('Expected a non-negative length.'); - - SecureRandom\bytes(-1); - } } diff --git a/tests/unit/SecureRandom/IntTest.php b/tests/unit/SecureRandom/IntTest.php index ac56f00f7..dcdac4786 100644 --- a/tests/unit/SecureRandom/IntTest.php +++ b/tests/unit/SecureRandom/IntTest.php @@ -38,6 +38,14 @@ public function testIntWithASpecificRange(): void static::assertLessThanOrEqual(1200, $random); } + public function testIntWithAnEqualRange(): void + { + $random = SecureRandom\int(20, 20); + + static::assertIsInt($random); + static::assertSame(20, $random); + } + public function testIntWithMinGreaterThanMax(): void { $this->expectException(Exception\InvariantViolationException::class); diff --git a/tests/unit/SecureRandom/StringTest.php b/tests/unit/SecureRandom/StringTest.php index 2a30fee03..c0246a613 100644 --- a/tests/unit/SecureRandom/StringTest.php +++ b/tests/unit/SecureRandom/StringTest.php @@ -36,14 +36,6 @@ public function testStringEarlyReturnForZeroLength(): void static::assertSame('', SecureRandom\string(0)); } - public function testStringThrowsForNegativeLength(): void - { - $this->expectException(Exception\InvariantViolationException::class); - $this->expectExceptionMessage('Expected a non-negative length.'); - - SecureRandom\string(-1); - } - public function testStringAlphabetMin(): void { $this->expectException(Exception\InvariantViolationException::class); @@ -51,14 +43,4 @@ public function testStringAlphabetMin(): void SecureRandom\string(32, 'a'); } - - // public function testStringAlphabetMax(): void - // { - // $this->markTestSkipped('Memory exhausting'); - // - // $this->expectException(Exception\InvariantViolationException::class); - // $this->expectExceptionMessage('Expected $alphabet\'s length to be in [2^1, 2^56]'); - // - // Random\string(32, Str\repeat('a', (2 ** 56) + 1)); - // } } diff --git a/tests/unit/Str/Byte/SliceTest.php b/tests/unit/Str/Byte/SliceTest.php index bc935e0f0..cdc8247d0 100644 --- a/tests/unit/Str/Byte/SliceTest.php +++ b/tests/unit/Str/Byte/SliceTest.php @@ -30,13 +30,6 @@ public function provideData(): array ]; } - public function testSliceThrowsForNegativeLength(): void - { - $this->expectException(Exception\InvariantViolationException::class); - - Byte\slice('Hello', 0, -1); - } - public function testSliceThrowsForOutOfBoundOffset(): void { $this->expectException(Exception\InvariantViolationException::class); diff --git a/tests/unit/Str/ConvertEncodingTest.php b/tests/unit/Str/ConvertEncodingTest.php index a694c35bc..7ddcb8cdd 100644 --- a/tests/unit/Str/ConvertEncodingTest.php +++ b/tests/unit/Str/ConvertEncodingTest.php @@ -5,7 +5,6 @@ namespace Psl\Tests\Unit\Str; use PHPUnit\Framework\TestCase; -use Psl\Exception\InvariantViolationException; use Psl\Str; final class ConvertEncodingTest extends TestCase @@ -16,8 +15,8 @@ final class ConvertEncodingTest extends TestCase public function testConvertEncoding( ?string $expected, string $string, - string $from_encoding, - string $to_encoding + Str\Encoding $from_encoding, + Str\Encoding $to_encoding ): void { static::assertSame($expected, Str\convert_encoding($string, $from_encoding, $to_encoding)); } @@ -25,23 +24,7 @@ public function testConvertEncoding( public function provideData(): array { return [ - ['åäö', 'åäö', 'ISO-8859-1', 'UTF-8'], + ['åäö', 'åäö', Str\Encoding::ISO_8859_1, Str\Encoding::UTF_8], ]; } - - public function testConvertEncodingThrowsForInvalidFromEncoding(): void - { - $this->expectException(InvariantViolationException::class); - $this->expectExceptionMessage('$from_encoding is invalid.'); - - Str\convert_encoding('Hello, World!', 'foobar', 'UTF-8'); - } - - public function testConvertEncodingThrowsForInvalidToEncoding(): void - { - $this->expectException(InvariantViolationException::class); - $this->expectExceptionMessage('$to_encoding is invalid.'); - - Str\convert_encoding('Hello, World!', 'ASCII', 'UTF-1337'); - } } diff --git a/tests/unit/Str/Grapheme/SliceTest.php b/tests/unit/Str/Grapheme/SliceTest.php index 514d34be3..b15e711d6 100644 --- a/tests/unit/Str/Grapheme/SliceTest.php +++ b/tests/unit/Str/Grapheme/SliceTest.php @@ -39,13 +39,6 @@ public function provideData(): array ]; } - public function testSliceThrowsForNegativeLength(): void - { - $this->expectException(Exception\InvariantViolationException::class); - - Grapheme\slice('Hello', 0, -1); - } - public function testSliceThrowsForOutOfBoundOffset(): void { $this->expectException(Exception\InvariantViolationException::class); diff --git a/tests/unit/Str/MetaphoneTest.php b/tests/unit/Str/MetaphoneTest.php index a4e98eaa2..bebeb1ac8 100644 --- a/tests/unit/Str/MetaphoneTest.php +++ b/tests/unit/Str/MetaphoneTest.php @@ -5,7 +5,6 @@ namespace Psl\Tests\Unit\Str; use PHPUnit\Framework\TestCase; -use Psl; use Psl\Str; final class MetaphoneTest extends TestCase @@ -28,12 +27,4 @@ public function provideData(): array ['', '1337'], ]; } - - public function testThrowsIfPhonemeIsNegative(): void - { - $this->expectException(Psl\Exception\InvariantViolationException::class); - $this->expectExceptionMessage('Expected non-negative phonemes, got -1.'); - - Str\metaphone('foo', -1); - } } diff --git a/tests/unit/Vec/ReproduceTest.php b/tests/unit/Vec/ReproduceTest.php index 0addbd841..95847c88d 100644 --- a/tests/unit/Vec/ReproduceTest.php +++ b/tests/unit/Vec/ReproduceTest.php @@ -5,7 +5,6 @@ namespace Psl\Tests\Unit\Vec; use PHPUnit\Framework\TestCase; -use Psl\Exception\InvariantViolationException; use Psl\Fun; use Psl\Vec; @@ -16,12 +15,4 @@ public function testReproduce(): void static::assertSame([1], (Vec\reproduce(1, Fun\identity()))); static::assertSame([1, 2, 3], Vec\reproduce(3, Fun\identity())); } - - public function testThrowsIfNumberIsLowerThan1(): void - { - $this->expectException(InvariantViolationException::class); - $this->expectExceptionMessage('The number of times you want to reproduce must be at least 1.'); - - Vec\reproduce(0, Fun\identity()); - } }