Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove more type verification at runtime, and narrow down argument/return types #319

Merged
merged 1 commit into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/component/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


6 changes: 3 additions & 3 deletions docs/component/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
46 changes: 23 additions & 23 deletions docs/component/filesystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


4 changes: 2 additions & 2 deletions docs/component/hash.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


2 changes: 1 addition & 1 deletion docs/component/iter.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions docs/component/secure-random.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


18 changes: 9 additions & 9 deletions docs/component/str-byte.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions docs/component/str-grapheme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)


18 changes: 9 additions & 9 deletions docs/component/str.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion docs/component/vec.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions src/Psl/Env/get_var.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 7 additions & 7 deletions src/Psl/Env/remove_var.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 3 additions & 1 deletion src/Psl/Env/set_current_dir.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
22 changes: 12 additions & 10 deletions src/Psl/Env/set_var.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
3 changes: 3 additions & 0 deletions src/Psl/Env/temp_dir.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion src/Psl/File/ReadHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/File/ReadWriteHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading