Skip to content

Commit

Permalink
chore: remove type verification at runtime
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Dec 19, 2021
1 parent 2cac74d commit 91e9c9a
Show file tree
Hide file tree
Showing 53 changed files with 153 additions and 303 deletions.
7 changes: 0 additions & 7 deletions config/psalm.xml
Expand Up @@ -11,13 +11,6 @@
</projectFiles>

<issueHandlers>
<!-- Redundant expressions should not be considered errors. -->
<!-- Sometimes we want to enforce things at runtime, as well using static analysis -->
<RedundantConditionGivenDocblockType errorLevel="suppress" />
<RedundantCondition errorLevel="suppress" />
<RedundantCast errorLevel="suppress" />
<RedundantCastGivenDocblockType errorLevel="suppress" />

<StringIncrement errorLevel="suppress" />
</issueHandlers>

Expand Down
2 changes: 1 addition & 1 deletion docs/component/channel.md
Expand Up @@ -12,7 +12,7 @@

#### `Functions`

- [bounded](./../../src/Psl/Channel/bounded.php#L22)
- [bounded](./../../src/Psl/Channel/bounded.php#L18)
- [unbounded](./../../src/Psl/Channel/unbounded.php#L16)

#### `Interfaces`
Expand Down
8 changes: 4 additions & 4 deletions docs/component/class.md
Expand Up @@ -14,9 +14,9 @@

- [defined](./../../src/Psl/Class/defined.php#L18)
- [exists](./../../src/Psl/Class/exists.php#L16)
- [has_constant](./../../src/Psl/Class/has_constant.php#L17)
- [has_method](./../../src/Psl/Class/has_method.php#L17)
- [is_abstract](./../../src/Psl/Class/is_abstract.php#L17)
- [is_final](./../../src/Psl/Class/is_final.php#L17)
- [has_constant](./../../src/Psl/Class/has_constant.php#L14)
- [has_method](./../../src/Psl/Class/has_method.php#L14)
- [is_abstract](./../../src/Psl/Class/is_abstract.php#L14)
- [is_final](./../../src/Psl/Class/is_final.php#L14)


2 changes: 1 addition & 1 deletion docs/component/file.md
Expand Up @@ -15,7 +15,7 @@
- [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)
- [temporary](./../../src/Psl/File/temporary.php#L17)
- [temporary](./../../src/Psl/File/temporary.php#L22)

#### `Interfaces`

Expand Down
4 changes: 2 additions & 2 deletions docs/component/math.md
Expand Up @@ -34,7 +34,7 @@
- [asin](./../../src/Psl/Math/asin.php#L14)
- [atan](./../../src/Psl/Math/atan.php#L14)
- [atan2](./../../src/Psl/Math/atan2.php#L14)
- [base_convert](./../../src/Psl/Math/base_convert.php#L30)
- [base_convert](./../../src/Psl/Math/base_convert.php#L31)
- [ceil](./../../src/Psl/Math/ceil.php#L14)
- [clamp](./../../src/Psl/Math/clamp.php#L24)
- [cos](./../../src/Psl/Math/cos.php#L14)
Expand All @@ -57,6 +57,6 @@
- [sum](./../../src/Psl/Math/sum.php#L14)
- [sum_floats](./../../src/Psl/Math/sum_floats.php#L14)
- [tan](./../../src/Psl/Math/tan.php#L14)
- [to_base](./../../src/Psl/Math/to_base.php#L23)
- [to_base](./../../src/Psl/Math/to_base.php#L20)


6 changes: 3 additions & 3 deletions docs/component/regex.md
Expand Up @@ -16,9 +16,9 @@
- [every_match](./../../src/Psl/Regex/every_match.php#L25)
- [first_match](./../../src/Psl/Regex/first_match.php#L24)
- [matches](./../../src/Psl/Regex/matches.php#L19)
- [replace](./../../src/Psl/Regex/replace.php#L26)
- [replace_every](./../../src/Psl/Regex/replace_every.php#L27)
- [replace_with](./../../src/Psl/Regex/replace_with.php#L26)
- [replace](./../../src/Psl/Regex/replace.php#L24)
- [replace_every](./../../src/Psl/Regex/replace_every.php#L25)
- [replace_with](./../../src/Psl/Regex/replace_with.php#L24)
- [split](./../../src/Psl/Regex/split.php#L29)


4 changes: 2 additions & 2 deletions docs/component/str-byte.md
Expand Up @@ -33,8 +33,8 @@
- [length](./../../src/Psl/Str/Byte/length.php#L14)
- [lowercase](./../../src/Psl/Str/Byte/lowercase.php#L14)
- [ord](./../../src/Psl/Str/Byte/ord.php#L12)
- [pad_left](./../../src/Psl/Str/Byte/pad_left.php#L27)
- [pad_right](./../../src/Psl/Str/Byte/pad_right.php#L27)
- [pad_left](./../../src/Psl/Str/Byte/pad_left.php#L24)
- [pad_right](./../../src/Psl/Str/Byte/pad_right.php#L24)
- [replace](./../../src/Psl/Str/Byte/replace.php#L15)
- [replace_ci](./../../src/Psl/Str/Byte/replace_ci.php#L15)
- [replace_every](./../../src/Psl/Str/Byte/replace_every.php#L17)
Expand Down
22 changes: 11 additions & 11 deletions docs/component/str.md
Expand Up @@ -43,29 +43,29 @@
- [is_empty](./../../src/Psl/Str/is_empty.php#L29)
- [is_utf8](./../../src/Psl/Str/is_utf8.php#L14)
- [join](./../../src/Psl/Str/join.php#L27)
- [length](./../../src/Psl/Str/length.php#L25)
- [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)
- [pad_left](./../../src/Psl/Str/pad_left.php#L37)
- [pad_right](./../../src/Psl/Str/pad_right.php#L37)
- [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)
- [replace](./../../src/Psl/Str/replace.php#L15)
- [replace_ci](./../../src/Psl/Str/replace_ci.php#L20)
- [replace_every](./../../src/Psl/Str/replace_every.php#L15)
- [replace_every_ci](./../../src/Psl/Str/replace_every_ci.php#L15)
- [reverse](./../../src/Psl/Str/reverse.php#L14)
- [search](./../../src/Psl/Str/search.php#L23)
- [search_ci](./../../src/Psl/Str/search_ci.php#L23)
- [search_last](./../../src/Psl/Str/search_last.php#L24)
- [search_last_ci](./../../src/Psl/Str/search_last_ci.php#L24)
- [slice](./../../src/Psl/Str/slice.php#L23)
- [splice](./../../src/Psl/Str/splice.php#L21)
- [split](./../../src/Psl/Str/split.php#L24)
- [search](./../../src/Psl/Str/search.php#L25)
- [search_ci](./../../src/Psl/Str/search_ci.php#L25)
- [search_last](./../../src/Psl/Str/search_last.php#L25)
- [search_last_ci](./../../src/Psl/Str/search_last_ci.php#L25)
- [slice](./../../src/Psl/Str/slice.php#L25)
- [splice](./../../src/Psl/Str/splice.php#L23)
- [split](./../../src/Psl/Str/split.php#L26)
- [starts_with](./../../src/Psl/Str/starts_with.php#L12)
- [starts_with_ci](./../../src/Psl/Str/starts_with_ci.php#L12)
- [strip_prefix](./../../src/Psl/Str/strip_prefix.php#L17)
- [strip_prefix](./../../src/Psl/Str/strip_prefix.php#L13)
- [strip_suffix](./../../src/Psl/Str/strip_suffix.php#L13)
- [to_int](./../../src/Psl/Str/to_int.php#L12)
- [trim](./../../src/Psl/Str/trim.php#L18)
Expand Down
8 changes: 5 additions & 3 deletions src/Psl/Async/Internal/AwaitableIterator.php
Expand Up @@ -9,6 +9,8 @@
use Psl\Async\Awaitable;
use Psl\Iter;

use function count;

/**
* The following class was derived from code of Amphp.
*
Expand Down Expand Up @@ -121,8 +123,8 @@ public function consume(): ?array
{
Psl\invariant(null === $this->queue->suspension, 'Concurrent consume() operations are not supported');

if (Iter\is_empty($this->queue->items)) {
if (Iter\is_empty($this->queue->pending) && $this->complete !== null) {
if (0 === count($this->queue->items)) {
if ($this->complete !== null && 0 === count($this->queue->pending)) {
return $this->complete->await();
}

Expand All @@ -132,7 +134,7 @@ public function consume(): ?array
return $this->queue->suspension->suspend();
}

$key = (int) Iter\first_key($this->queue->items);
$key = Iter\first_key($this->queue->items);
$item = $this->queue->items[$key];

unset($this->queue->items[$key]);
Expand Down
6 changes: 1 addition & 5 deletions src/Psl/Channel/Internal/ChannelState.php
Expand Up @@ -4,7 +4,6 @@

namespace Psl\Channel\Internal;

use Psl;
use Psl\Channel\ChannelInterface;
use Psl\Channel\Exception;

Expand Down Expand Up @@ -43,14 +42,11 @@ final class ChannelState implements ChannelInterface
private bool $closed = false;

/**
* @param positive-int|null $capacity
*
* @throws Psl\Exception\InvariantViolationException If $capacity is not a positive integer.
* @param null|positive-int $capacity
*/
public function __construct(
private ?int $capacity = null,
) {
Psl\invariant(null === $capacity || $capacity >= 1, '$capacity must be a positive integer.');
}

/**
Expand Down
4 changes: 0 additions & 4 deletions src/Psl/Channel/bounded.php
Expand Up @@ -4,8 +4,6 @@

namespace Psl\Channel;

use Psl;

/**
* Creates a bounded channel.
*
Expand All @@ -15,8 +13,6 @@
*
* @param positive-int $capacity
*
* @throws Psl\Exception\InvariantViolationException If $capacity is not a positive integer.
*
* @return array{0: ReceiverInterface<T>, 1: SenderInterface<T>}
*/
function bounded(int $capacity): array
Expand Down
5 changes: 0 additions & 5 deletions src/Psl/Class/has_constant.php
Expand Up @@ -4,20 +4,15 @@

namespace Psl\Class;

use Psl;
use ReflectionClass;

/**
* Checks if constant is defined in the given class.
*
* @param class-string $class_name
*
* @throws Psl\Exception\InvariantViolationException If $class_name does not exist.
*/
function has_constant(string $class_name, string $constant_name): bool
{
Psl\invariant(namespace\exists($class_name), 'Classname "%s" does not exist.', $class_name);

/** @psalm-suppress MissingThrowsDocblock */
return (new ReflectionClass($class_name))->hasConstant($constant_name);
}
5 changes: 0 additions & 5 deletions src/Psl/Class/has_method.php
Expand Up @@ -4,20 +4,15 @@

namespace Psl\Class;

use Psl;
use ReflectionClass;

/**
* Checks if method is defined in the given class.
*
* @param class-string $class_name
*
* @throws Psl\Exception\InvariantViolationException If $class_name does not exist.
*/
function has_method(string $class_name, string $method_name): bool
{
Psl\invariant(namespace\exists($class_name), 'Classname "%s" does not exist.', $class_name);

/** @psalm-suppress MissingThrowsDocblock */
return (new ReflectionClass($class_name))->hasMethod($method_name);
}
5 changes: 0 additions & 5 deletions src/Psl/Class/is_abstract.php
Expand Up @@ -4,20 +4,15 @@

namespace Psl\Class;

use Psl;
use ReflectionClass;

/**
* Checks if class is abstract.
*
* @param class-string $class_name
*
* @throws Psl\Exception\InvariantViolationException If $class_name does not exist.
*/
function is_abstract(string $class_name): bool
{
Psl\invariant(namespace\exists($class_name), 'Classname "%s" does not exist.', $class_name);

/** @psalm-suppress MissingThrowsDocblock */
return (new ReflectionClass($class_name))->isAbstract();
}
5 changes: 0 additions & 5 deletions src/Psl/Class/is_final.php
Expand Up @@ -4,20 +4,15 @@

namespace Psl\Class;

use Psl;
use ReflectionClass;

/**
* Checks if class is final.
*
* @param class-string $class_name
*
* @throws Psl\Exception\InvariantViolationException If $class_name does not exist.
*/
function is_final(string $class_name): bool
{
Psl\invariant(namespace\exists($class_name), 'Classname "%s" does not exist.', $class_name);

/** @psalm-suppress MissingThrowsDocblock */
return (new ReflectionClass($class_name))->isFinal();
}
6 changes: 1 addition & 5 deletions src/Psl/File/ReadWriteHandle.php
Expand Up @@ -45,11 +45,7 @@ public function __construct(string $path, WriteMode $write_mode = WriteMode::OPE
Filesystem\create_file($path);
}

/**
* @psalm-suppress UndefinedPropertyFetch
* @psalm-suppress MixedArgument
*/
$this->readWriteHandle = Internal\open($path, 'r' . ((string) $write_mode->value) . '+', read: true, write: false);
$this->readWriteHandle = Internal\open($path, 'r' . ($write_mode->value) . '+', read: true, write: false);

parent::__construct($this->readWriteHandle);
}
Expand Down
6 changes: 1 addition & 5 deletions src/Psl/File/WriteHandle.php
Expand Up @@ -40,11 +40,7 @@ public function __construct(string $path, WriteMode $write_mode = WriteMode::OPE
Psl\invariant_violation('File "%s" is not writable.', $path);
}

/**
* @psalm-suppress UndefinedPropertyFetch
* @psalm-suppress MixedArgument
*/
$this->writeHandle = Internal\open($path, (string) $write_mode->value, read: false, write: true);
$this->writeHandle = Internal\open($path, $write_mode->value, read: false, write: true);

parent::__construct($this->writeHandle);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Psl/File/temporary.php
Expand Up @@ -10,6 +10,11 @@
/**
* Create a temporary file and open it for read and write.
*
* @param non-empty-string|null $directory The directory where the temporary filename will be created.
* If no specified, `Env\temp_dir()` will be used to retrieve
* the system default temporary directory.
* @param non-empty-string|null $prefix The prefix of the generated temporary filename.
*
* @throws Psl\Exception\InvariantViolationException If $directory doesn't exist or is not writable.
* @throws Psl\Exception\InvariantViolationException If $prefix contains a directory separator.
* @throws Filesystem\Exception\RuntimeException If unable to create the file.
Expand Down
14 changes: 7 additions & 7 deletions src/Psl/Filesystem/create_temporary_file.php
Expand Up @@ -12,10 +12,10 @@
/**
* Create a temporary file.
*
* @param string|null $directory The directory where the temporary filename will be created.
* If no specified, `Env\temp_dir()` will be used to retrieve
* the system default temporary directory.
* @param string|null $prefix The prefix of the generated temporary filename.
* @param non-empty-string|null $directory The directory where the temporary filename will be created.
* If no specified, `Env\temp_dir()` will be used to retrieve
* the system default temporary directory.
* @param non-empty-string|null $prefix The prefix of the generated temporary filename.
*
* @throws Psl\Exception\InvariantViolationException If $directory doesn't exist or is not writable.
* @throws Psl\Exception\InvariantViolationException If $prefix contains a directory separator.
Expand All @@ -34,16 +34,16 @@ function create_temporary_file(?string $directory = null, ?string $prefix = null

if (null !== $prefix) {
Psl\invariant(
!Str\contains($prefix, ((string)SEPARATOR)),
!Str\contains($prefix, SEPARATOR),
'$prefix should not contain a directory separator ( "%s" ).',
((string)SEPARATOR)
SEPARATOR
);
} else {
$prefix = '';
}

try {
$filename = $directory . ((string)SEPARATOR) . $prefix . SecureRandom\string(8);
$filename = $directory . SEPARATOR . $prefix . SecureRandom\string(8);
// @codeCoverageIgnoreStart
} catch (SecureRandom\Exception\InsufficientEntropyException $e) {
throw new Exception\RuntimeException('Unable to gather enough entropy to generate filename.', 0, $e);
Expand Down
11 changes: 2 additions & 9 deletions src/Psl/IO/Internal/ResourceHandle.php
Expand Up @@ -83,15 +83,11 @@ public function __construct(mixed $stream, bool $read, bool $write, bool $seek,
$meta = stream_get_meta_data($stream);
$this->blocks = $meta['blocked'] || ($meta['wrapper_type'] ?? '') === 'plainfile';
if ($seek) {
$seekable = (bool)$meta['seekable'];

Psl\invariant($seekable, 'Handle is not seekable.');
Psl\invariant($meta['seekable'], 'Handle is not seekable.');
}

if ($read) {
$readable = str_contains($meta['mode'], 'r') || str_contains($meta['mode'], '+');

Psl\invariant($readable, 'Handle is not readable.');
Psl\invariant(str_contains($meta['mode'], 'r') || str_contains($meta['mode'], '+'), 'Handle is not readable.');

$deferred = &$this->readDeferred;
$this->readWatcher = Async\Scheduler::onReadable($stream, static function () use (&$deferred) {
Expand Down Expand Up @@ -301,9 +297,6 @@ public function tryRead(?int $max_bytes = null): string
$max_bytes = self::DEFAULT_READ_BUFFER_SIZE;
} elseif ($max_bytes > self::MAXIMUM_READ_BUFFER_SIZE) {
$max_bytes = self::MAXIMUM_READ_BUFFER_SIZE;
} else {
/** @psalm-suppress MissingThrowsDocblock */
Psl\invariant($max_bytes > 0, '$max_bytes must be null, or > 0');
}

/** @psalm-suppress PossiblyInvalidArgument */
Expand Down

0 comments on commit 91e9c9a

Please sign in to comment.