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: miscellaneous changes #458

Merged
merged 1 commit into from
Apr 4, 2024
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
1 change: 1 addition & 0 deletions docs/component/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
- [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)
- [is_readonly](./../../src/Psl/Class/is_readonly.php#L14)


4 changes: 2 additions & 2 deletions docs/component/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
- [current_exec](./../../src/Psl/Env/current_exec.php#L12)
- [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)
- [join_paths](./../../src/Psl/Env/join_paths.php#L20)
- [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#L20)
- [split_paths](./../../src/Psl/Env/split_paths.php#L16)
- [split_paths](./../../src/Psl/Env/split_paths.php#L18)
- [temp_dir](./../../src/Psl/Env/temp_dir.php#L21)


2 changes: 1 addition & 1 deletion docs/component/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#### `Classes`

- [Address](./../../src/Psl/Network/Address.php#L7)
- [Address](./../../src/Psl/Network/Address.php#L10)
- [SocketOptions](./../../src/Psl/Network/SocketOptions.php#L14)

#### `Enums`
Expand Down
6 changes: 3 additions & 3 deletions docs/component/option.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

#### `Functions`

- [from_nullable](./../../src/Psl/Option/from_nullable.php#L16)
- [none](./../../src/Psl/Option/none.php#L12)
- [some](./../../src/Psl/Option/some.php#L16)
- [from_nullable](./../../src/Psl/Option/from_nullable.php#L18)
- [none](./../../src/Psl/Option/none.php#L14)
- [some](./../../src/Psl/Option/some.php#L18)

#### `Classes`

Expand Down
2 changes: 1 addition & 1 deletion docs/component/random-sequence.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

- [MersenneTwisterPHPVariantSequence](./../../src/Psl/RandomSequence/MersenneTwisterPHPVariantSequence.php#L10)
- [MersenneTwisterSequence](./../../src/Psl/RandomSequence/MersenneTwisterSequence.php#L10)
- [SecureSequence](./../../src/Psl/RandomSequence/SecureSequence.php#L15)
- [SecureSequence](./../../src/Psl/RandomSequence/SecureSequence.php#L13)


4 changes: 3 additions & 1 deletion src/Psl/Async/Awaitable.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* @implements PromiseInterface<T>
*/
final class Awaitable implements PromiseInterface
final readonly class Awaitable implements PromiseInterface
{
private State $state;

Expand Down Expand Up @@ -117,6 +117,8 @@ public static function error(Throwable $throwable): self

/**
* @return bool True if the operation has completed.
*
* @psalm-mutation-free
*/
public function isComplete(): bool
{
Expand Down
6 changes: 5 additions & 1 deletion src/Psl/Async/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @template T
*/
final class Deferred
final readonly class Deferred
{
/**
* @var Internal\State<T>
Expand Down Expand Up @@ -62,6 +62,8 @@ public function error(Throwable $throwable): void

/**
* @return bool True if the operation has completed.
*
* @psalm-mutation-free
*/
public function isComplete(): bool
{
Expand All @@ -70,6 +72,8 @@ public function isComplete(): bool

/**
* @return Awaitable<T> The awaitable associated with this Deferred.
*
* @psalm-mutation-free
*/
public function getAwaitable(): Awaitable
{
Expand Down
4 changes: 4 additions & 0 deletions src/Psl/Async/Internal/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public function error(Throwable $throwable): void

/**
* Suppress the `Throwable`s thrown to the loop error handler if and operation error is not handled by a callback.
*
* @psalm-external-mutation-free
*/
public function ignore(): void
{
Expand All @@ -141,6 +143,8 @@ public function ignore(): void

/**
* @return bool True if the operation has completed.
*
* @psalm-mutation-free
*/
public function isComplete(): bool
{
Expand Down
2 changes: 0 additions & 2 deletions src/Psl/Async/OptionalIncrementalTimeout.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public function __construct(?float $timeout, Closure $handler)
* If the timeout has already been exceeded, the handler is invoked, and its return value is provided.
*
* @return float|null The remaining time in seconds, null if no timeout is set, or the handler's return value if the timeout is exceeded.
*
* @external-mutation-free
*/
public function getRemaining(): ?float
{
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Async/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @codeCoverageIgnore
*/
final class Scheduler
final readonly class Scheduler
{
private function __construct()
{
Expand Down
10 changes: 5 additions & 5 deletions src/Psl/Channel/ChannelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface ChannelInterface extends Countable
*
* @return null|positive-int
*
* @mutation-free
* @psalm-mutation-free
*/
public function getCapacity(): ?int;

Expand All @@ -30,7 +30,7 @@ public function close(): void;
/**
* Returns true if the channel is closed.
*
* @mutation-free
* @psalm-mutation-free
*/
public function isClosed(): bool;

Expand All @@ -39,7 +39,7 @@ public function isClosed(): bool;
*
* @return int<0, max>
*
* @mutation-free
* @psalm-mutation-free
*/
public function count(): int;

Expand All @@ -48,14 +48,14 @@ public function count(): int;
*
* Unbounded channels are never full.
*
* @mutation-free
* @psalm-mutation-free
*/
public function isFull(): bool;

/**
* Returns true if the channel is empty.
*
* @mutation-free
* @psalm-mutation-free
*/
public function isEmpty(): bool;
}
29 changes: 21 additions & 8 deletions src/Psl/Channel/Internal/BoundedChannelState.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,24 @@ final class BoundedChannelState implements ChannelInterface
*/
private array $messages = [];

/**
* @var int<0, max>
*/
private int $size = 0;

public bool $closed = false;

/**
* @var positive-int
*/
private readonly int $capacity;

/**
* @param positive-int $capacity
*/
public function __construct(
private int $capacity
) {
public function __construct(int $capacity)
{
$this->capacity = $capacity;
}

public function __destruct()
Expand All @@ -71,7 +79,9 @@ public function waitForMessage(Suspension $suspension): void
}

/**
* {@inheritDoc}
* @return positive-int
*
* @psalm-mutation-free
*/
public function getCapacity(): int
{
Expand Down Expand Up @@ -99,31 +109,33 @@ public function close(): void
}

/**
* {@inheritDoc}
* @psalm-mutation-free
*/
public function isClosed(): bool
{
return $this->closed;
}

/**
* {@inheritDoc}
* @return int<0, max>
*
* @psalm-mutation-free
*/
public function count(): int
{
return $this->size;
}

/**
* {@inheritDoc}
* @psalm-mutation-free
*/
public function isFull(): bool
{
return $this->capacity === $this->size;
}

/**
* {@inheritDoc}
* @psalm-mutation-free
*/
public function isEmpty(): bool
{
Expand Down Expand Up @@ -171,6 +183,7 @@ public function receive(): mixed
}

$item = array_shift($this->messages);
/** @psalm-suppress InvalidPropertyAssignmentValue - The size is always in sync with messages */
$this->size--;

if ($suspension = array_shift($this->waitingForSpace)) {
Expand Down
17 changes: 16 additions & 1 deletion src/Psl/Channel/Internal/ChannelSideTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ trait ChannelSideTrait
protected UnboundedChannelState|BoundedChannelState $state;

/**
* @return int<1, max>|null
* Returns the channel capacity if it’s bounded.
*
* @return null|positive-int
*
* @psalm-mutation-free
*/
public function getCapacity(): ?int
{
Expand All @@ -28,25 +32,36 @@ public function close(): void
$this->state->close();
}

/**
* @psalm-mutation-free
*/
public function isClosed(): bool
{
return $this->state->isClosed();
}

/**
* @return int<0, max>
*
* @psalm-mutation-free
*/
public function count(): int
{
/** @var int<0, max> */
return $this->state->count();
}

/**
* @psalm-mutation-free
*/
public function isFull(): bool
{
return $this->state->isFull();
}

/**
* @psalm-mutation-free
*/
public function isEmpty(): bool
{
return $this->state->isEmpty();
Expand Down
16 changes: 10 additions & 6 deletions src/Psl/Channel/Internal/UnboundedChannelState.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ public function waitForMessage(Suspension $suspension): void
}

/**
* {@inheritDoc}
* @return null
*
* @psalm-mutation-free
*/
public function getCapacity(): ?int
public function getCapacity(): null
{
return null;
}
Expand All @@ -66,31 +68,33 @@ public function close(): void
}

/**
* {@inheritDoc}
* @psalm-mutation-free
*/
public function isClosed(): bool
{
return $this->closed;
}

/**
* {@inheritDoc}
* @return int<0, max>
*
* @psalm-mutation-free
*/
public function count(): int
{
return count($this->messages);
}

/**
* {@inheritDoc}
* @psalm-mutation-free
*/
public function isFull(): bool
{
return false;
}

/**
* {@inheritDoc}
* @psalm-mutation-free
*/
public function isEmpty(): bool
{
Expand Down
18 changes: 18 additions & 0 deletions src/Psl/Class/is_readonly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Psl\Class;

use ReflectionClass;

/**
* Checks if class is read only.
*
* @param class-string $class_name
*/
function is_readonly(string $class_name): bool
{
/** @psalm-suppress MissingThrowsDocblock */
return (new ReflectionClass($class_name))->isReadOnly();
azjezz marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 3 additions & 0 deletions src/Psl/Collection/Exception/OutOfBoundsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

final class OutOfBoundsException extends Exception\OutOfBoundsException implements ExceptionInterface
{
/**
* @psalm-mutation-free
*/
public static function for(string|int $offset): OutOfBoundsException
{
return new self(Str\format('Key (%s) was out-of-bounds.', $offset));
Expand Down
Loading