Skip to content

Commit

Permalink
chore: apply mago fmt
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Dec 29, 2024
1 parent 8cff0d2 commit 0c4f3a3
Show file tree
Hide file tree
Showing 655 changed files with 5,163 additions and 5,039 deletions.
28 changes: 14 additions & 14 deletions examples/async/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@

require __DIR__ . '/../../vendor/autoload.php';

Async\main(static function(): int {
$watcher = Async\Scheduler::onSignal(SIGINT, static function (): never {
IO\write_line('SIGINT received, stopping...');
exit(0);
});
Async\main(static function (): int {
$watcher = Async\Scheduler::onSignal(SIGINT, static function (): never {
IO\write_line('SIGINT received, stopping...');
exit(0);
});

Async\Scheduler::unreference($watcher);
Async\Scheduler::unreference($watcher);

IO\write_error_line('Press Ctrl+C to stop');
IO\write_error_line('Press Ctrl+C to stop');

Async\concurrently([
static fn(): string => Shell\execute('sleep', ['3']),
static fn(): string => Shell\execute('echo', ['Hello World!']),
static fn(): string => Shell\execute('echo', ['Hello World!']),
]);
Async\concurrently([
static fn(): string => Shell\execute('sleep', ['3']),
static fn(): string => Shell\execute('echo', ['Hello World!']),
static fn(): string => Shell\execute('echo', ['Hello World!']),
]);

IO\write_error_line('Done!');
IO\write_error_line('Done!');

return 0;
return 0;
});
2 changes: 1 addition & 1 deletion examples/async/usleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

$duration = DateTime\Timestamp::monotonic()->since($start);

IO\write_error_line("duration : %s.", $duration->toString(max_decimals: 5));
IO\write_error_line('duration : %s.', $duration->toString(max_decimals: 5));

return 0;
});
5 changes: 2 additions & 3 deletions examples/channel/bounded.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
$receiver->receive();
}
} catch (Channel\Exception\ClosedChannelException) {
IO\write_error_line("[ receiver ]: completed.");
IO\write_error_line('[ receiver ]: completed.');
}
});

Expand All @@ -37,9 +37,8 @@
$file->close();
}

IO\write_error_line("[ sender ]: completed.");
IO\write_error_line('[ sender ]: completed.');
$sender->close();

return 0;
});

6 changes: 3 additions & 3 deletions examples/io/benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
$args = getopt('i:o:t:');
$input_file = $args['i'] ?? '/dev/zero';
$output_file = $args['o'] ?? '/dev/null';
$seconds = DateTime\Duration::seconds((int)($args['t'] ?? 5));
$seconds = DateTime\Duration::seconds((int) ($args['t'] ?? 5));

// passing file descriptors requires mapping paths (https://bugs.php.net/bug.php?id=53465)
$input_file = Regex\replace($input_file, '(^/dev/fd/)', 'php://fd/');
Expand All @@ -55,10 +55,10 @@

$duration = DateTime\Timestamp::monotonic()->since($start);
$bytes = $i * 65536;
$bytes_formatted = Math\round($bytes / 1024 / 1024 / $duration->getTotalSeconds(), 1);
$bytes_formatted = Math\round((($bytes / 1024) / 1024) / $duration->getTotalSeconds(), 1);

IO\write_error_line('read %d byte(s) in %s => %dMiB/s', $bytes, $duration->toString(), $bytes_formatted);
IO\write_error_line('peak memory usage of %dMiB', Math\round(memory_get_peak_usage(true) / 1024 / 1024, 1));
IO\write_error_line('peak memory usage of %dMiB', Math\round((memory_get_peak_usage(true) / 1024) / 1024, 1));

return 0;
});
12 changes: 6 additions & 6 deletions examples/io/pipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@

require __DIR__ . '/../../vendor/autoload.php';

Async\main(static function(): int {
Async\main(static function (): int {
[$read, $write] = IO\pipe();

Async\concurrently([
static function() use($read): void {
IO\write_error_line("< sleeping.");
static function () use ($read): void {
IO\write_error_line('< sleeping.');

Async\sleep(Duration::milliseconds(10));

IO\write_error_line("< waiting for content.");
IO\write_error_line('< waiting for content.');

$content = $read->readAll();

IO\write_error_line('< received "%s".', $content);
IO\write_error_line("< closing.");
IO\write_error_line('< closing.');

$read->close();
},
static function() use($write): void {
static function () use ($write): void {
IO\write_error_line('> sleeping.');

Async\sleep(Duration::milliseconds(100));
Expand Down
4 changes: 2 additions & 2 deletions examples/io/queued.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

require __DIR__ . '/../../vendor/autoload.php';

Async\main(static function(): int {
Async\main(static function (): int {
[$read, $write] = IO\pipe();

$he = Async\run(static fn(): string => $read->readFixedSize(2));

Async\sleep(Psl\DateTime\Duration::milliseconds(200));

$write->write("hello");
$write->write('hello');

$llo = $read->readFixedSize(3);

Expand Down
3 changes: 1 addition & 2 deletions examples/run.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


declare(strict_types=1);

namespace Psl\Example\IO;
Expand Down Expand Up @@ -34,7 +33,7 @@

IO\write_error_line('- %s/%s -> started', $component, $script);

$awaitables[] = Async\run(static function() use($component, $script, $file): array {
$awaitables[] = Async\run(static function () use ($component, $script, $file): array {
$start = DateTime\Timestamp::monotonic();
Shell\execute(PHP_BINARY, [$file]);
$duration = DateTime\Timestamp::monotonic()->since($start);
Expand Down
9 changes: 5 additions & 4 deletions examples/tcp/basic-http-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@
IO\write_error_line('Click Ctrl+C to stop the server.');

Iter\apply($server->incoming(), static function (Network\StreamSocketInterface $connection): void {
Async\run(static function() use($connection): void {
Async\run(static function () use ($connection): void {
$request = $connection->read();

$connection->writeAll("HTTP/1.1 200 OK\nConnection: close\nContent-Type: text/html; charset=utf-8\n\n");
$connection->writeAll(Str\format(RESPONSE_FORMAT, Html\encode_special_characters($request)));
$connection->close();
})->catch(
static fn(IO\Exception\ExceptionInterface $e) => IO\write_error_line('Error: %s.', $e->getMessage())
)->ignore();
})->catch(static fn(IO\Exception\ExceptionInterface $e) => IO\write_error_line(
'Error: %s.',
$e->getMessage(),
))->ignore();
});

IO\write_error_line('');
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/concurrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
return 0;
}

$file = Filesystem\create_temporary_file(prefix: 'psl-examples') . ".sock";
$file = Filesystem\create_temporary_file(prefix: 'psl-examples') . '.sock';

Async\concurrently([
'server' => static function () use ($file): void {
Expand Down
2 changes: 1 addition & 1 deletion mago.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[source]
paths = ["src", "tests"]
paths = ["src", "tests", "examples"]
includes = ["vendor"]
excludes = []

Expand Down
8 changes: 4 additions & 4 deletions src/Psl/Async/Awaitable.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function then(Closure $success, Closure $failure): Awaitable
* @param null|Throwable $error
* @param null|T $value
*/
static function (?Throwable $error, mixed $value) use ($state, $success, $failure): void {
static function (null|Throwable $error, mixed $value) use ($state, $success, $failure): void {
if ($error) {
try {
$state->complete($failure($error));
Expand Down Expand Up @@ -181,7 +181,7 @@ static function (?Throwable $error, mixed $value) use ($state, $success, $failur
*/
public function map(Closure $success): Awaitable
{
return $this->then($success, static fn (Throwable $throwable) => throw $throwable);
return $this->then($success, static fn(Throwable $throwable) => throw $throwable);
}

/**
Expand Down Expand Up @@ -220,7 +220,7 @@ public function always(Closure $always): Awaitable
/** @var State<T> $state */
$state = new State();

$this->state->subscribe(static function (?Throwable $error, mixed $value) use ($state, $always): void {
$this->state->subscribe(static function (null|Throwable $error, mixed $value) use ($state, $always): void {
try {
$always();

Expand Down Expand Up @@ -256,7 +256,7 @@ public function await(): mixed
* @param null|Throwable $error
* @param null|T $value
*/
static function (?Throwable $error, mixed $value) use ($suspension): void {
static function (null|Throwable $error, mixed $value) use ($suspension): void {
if ($error) {
$suspension->throw($error);
} else {
Expand Down
8 changes: 6 additions & 2 deletions src/Psl/Async/Exception/CompositeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class CompositeException extends Exception implements ExceptionInterface
* @param non-empty-array<array-key, Throwable> $reasons Array of exceptions.
* @param string|null $message Exception message, defaults to message generated from passed exceptions.
*/
public function __construct(array $reasons, ?string $message = null)
public function __construct(array $reasons, null|string $message = null)
{
parent::__construct($message ?? $this->generateMessage($reasons));

Expand All @@ -43,7 +43,11 @@ public function getReasons(): array
*/
private function generateMessage(array $reasons): string
{
$message = Str\format('"Multiple errors encountered (%d); use "%s::getReasons()" to retrieve the array of exceptions thrown:', count($reasons), self::class);
$message = Str\format(
'"Multiple errors encountered (%d); use "%s::getReasons()" to retrieve the array of exceptions thrown:',
count($reasons),
self::class,
);

foreach ($reasons as $reason) {
$message .= PHP_EOL . PHP_EOL . $reason::class;
Expand Down
7 changes: 5 additions & 2 deletions src/Psl/Async/Exception/UnhandledAwaitableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ final class UnhandledAwaitableException extends RuntimeException implements Exce
public static function forThrowable(Throwable $throwable): UnhandledAwaitableException
{
return new self(
Str\format('Unhandled awaitable error "%s", make sure to call `Awaitable::await()` before the awaitable is destroyed, or call `Awaitable::ignore()` to ignore exceptions.', $throwable::class),
Str\format(
'Unhandled awaitable error "%s", make sure to call `Awaitable::await()` before the awaitable is destroyed, or call `Awaitable::ignore()` to ignore exceptions.',
$throwable::class,
),
(int) $throwable->getCode(),
$throwable
$throwable,
);
}
}
16 changes: 4 additions & 12 deletions src/Psl/Async/Internal/AwaitableIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class AwaitableIterator
/**
* @var null|Awaitable<void>|Awaitable<null>|Awaitable<array{Tk, Awaitable<Tv>}>
*/
private ?Awaitable $complete = null;
private null|Awaitable $complete = null;

public function __construct()
{
Expand All @@ -63,15 +63,7 @@ public function enqueue(State $state, mixed $key, Awaitable $awaitable): void
/**
* @param Tv|null $_result
*/
static function (
?Throwable $_error,
mixed $_result,
string $id
) use (
$key,
$awaitable,
$queue
): void {
static function (null|Throwable $_error, mixed $_result, string $id) use ($key, $awaitable, $queue): void {
unset($queue->pending[$id]);

if ($queue->suspension) {
Expand All @@ -81,7 +73,7 @@ static function (
}

$queue->items[] = [$key, $awaitable];
}
},
);

$queue->pending[$id] = $state;
Expand Down Expand Up @@ -126,7 +118,7 @@ public function error(Throwable $exception): void
*
* @return null|array{0: Tk, 1: Awaitable<Tv>}
*/
public function consume(): ?array
public function consume(): null|array
{
if (null !== $this->queue->suspension) {
Psl\invariant_violation('Concurrent consume() operations are not supported');
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Async/Internal/AwaitableIteratorQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ final class AwaitableIteratorQueue
*/
public array $pending = [];

public ?Suspension $suspension = null;
public null|Suspension $suspension = null;
}
2 changes: 1 addition & 1 deletion src/Psl/Async/Internal/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class State
*/
private mixed $result = null;

private ?Throwable $throwable = null;
private null|Throwable $throwable = null;

/**
* @throws Exception\UnhandledAwaitableException
Expand Down
6 changes: 3 additions & 3 deletions src/Psl/Async/OptionalIncrementalTimeout.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class OptionalIncrementalTimeout
/**
* @var ?Timestamp The end time.
*/
private ?Timestamp $end;
private null|Timestamp $end;

/**
* @var (Closure(): ?Duration) The handler to be called upon timeout.
Expand All @@ -35,7 +35,7 @@ final class OptionalIncrementalTimeout
* @param null|Duration $timeout The timeout duration. Null to disable timeout.
* @param (Closure(): ?Duration) $handler The handler to be executed if the timeout is reached.
*/
public function __construct(?Duration $timeout, Closure $handler)
public function __construct(null|Duration $timeout, Closure $handler)
{
$this->handler = $handler;

Expand All @@ -59,7 +59,7 @@ public function __construct(?Duration $timeout, Closure $handler)
*
* @return Duration|null The remaining time duration, null if no timeout is set, or the handler's return value if the timeout is exceeded.
*/
public function getRemaining(): ?Duration
public function getRemaining(): null|Duration
{
if ($this->end === null) {
return null;
Expand Down
1 change: 0 additions & 1 deletion src/Psl/Async/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ public static function repeat(DateTime\Duration $interval, Closure $callback): s
return EventLoop::repeat($interval->getTotalSeconds(), $callback);
}


/**
* Enable a callback to be active starting in the next tick.
*
Expand Down
5 changes: 4 additions & 1 deletion src/Psl/Async/all.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ function all(iterable $awaitables): array
throw $exception;
}

throw new Exception\CompositeException([$exception, ...$errors], 'Multiple exceptions thrown while waiting.');
throw new Exception\CompositeException(
[$exception, ...$errors],
'Multiple exceptions thrown while waiting.',
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Async/later.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function later(): void
{
$suspension = EventLoop::getSuspension();

EventLoop::defer(static fn () => $suspension->resume());
EventLoop::defer(static fn() => $suspension->resume());

$suspension->suspend();
}
5 changes: 1 addition & 4 deletions src/Psl/Async/sleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
function sleep(DateTime\Duration $duration): void
{
$suspension = EventLoop::getSuspension();
$watcher = EventLoop::delay(
$duration->getTotalSeconds(),
static fn () => $suspension->resume(),
);
$watcher = EventLoop::delay($duration->getTotalSeconds(), static fn() => $suspension->resume());

try {
$suspension->suspend();
Expand Down
Loading

0 comments on commit 0c4f3a3

Please sign in to comment.