Skip to content

Commit b67a068

Browse files
authored
Merge pull request #16 from rcalicdan/more-type-safety
More type safety
2 parents 4c22cf3 + 44f3d16 commit b67a068

28 files changed

+2129
-1010
lines changed

phpstan.neon

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ parameters:
77
- src
88
treatPhpDocTypesAsCertain: false
99
excludePaths:
10-
- tests/
10+
- tests/
11+
ignoreErrors:
12+
- '#Variable method call on.*#'

src/Api/AsyncFile.php

Lines changed: 261 additions & 67 deletions
Large diffs are not rendered by default.

src/Api/AsyncHttp.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Rcalicdan\FiberAsync\Http\Request;
77
use Rcalicdan\FiberAsync\Http\Response;
88
use Rcalicdan\FiberAsync\Http\StreamingResponse;
9+
use Rcalicdan\FiberAsync\Promise\Interfaces\CancellablePromiseInterface;
910
use Rcalicdan\FiberAsync\Promise\Interfaces\PromiseInterface;
1011

1112
/**
@@ -16,13 +17,13 @@
1617
* underlying handler and event loop management for a more convenient API.
1718
*
1819
* @method static Request request() Creates a new fluent request builder.
19-
* @method static PromiseInterface<Response> get(string $url, array $query = []) Performs a GET request.
20-
* @method static PromiseInterface<Response> post(string $url, array $data = []) Performs a POST request.
21-
* @method static PromiseInterface<Response> put(string $url, array $data = []) Performs a PUT request.
20+
* @method static PromiseInterface<Response> get(string $url, array<string, mixed> $query = []) Performs a GET request.
21+
* @method static PromiseInterface<Response> post(string $url, array<string, mixed> $data = []) Performs a POST request.
22+
* @method static PromiseInterface<Response> put(string $url, array<string, mixed> $data = []) Performs a PUT request.
2223
* @method static PromiseInterface<Response> delete(string $url) Performs a DELETE request.
23-
* @method static PromiseInterface<Response> fetch(string $url, array $options = []) A flexible, fetch-like request method.
24-
* @method static PromiseInterface<StreamingResponse> stream(string $url, array $options = [], ?callable $onChunk = null) Streams a response body.
25-
* @method static PromiseInterface<array{file: string, status: int|null, headers: array}> download(string $url, string $destination, array $options = []) Downloads a file.
24+
* @method static PromiseInterface<Response> fetch(string $url, array<int|string, mixed> $options = []) A flexible, fetch-like request method.
25+
* @method static PromiseInterface<StreamingResponse> stream(string $url, array<int|string, mixed> $options = [], ?callable $onChunk = null) Streams a response body.
26+
* @method static CancellablePromiseInterface<array{file: string, status: int, headers: array<mixed>}> download(string $url, string $destination, array<int|string, mixed> $options = []) Downloads a file.
2627
*/
2728
class AsyncHttp
2829
{
@@ -55,7 +56,7 @@ public static function request(): Request
5556
* Performs a quick, asynchronous GET request.
5657
*
5758
* @param string $url The target URL.
58-
* @param array $query Optional query parameters.
59+
* @param array<string, mixed> $query Optional query parameters.
5960
* @return PromiseInterface<Response> A promise that resolves with a Response object.
6061
*/
6162
public static function get(string $url, array $query = []): PromiseInterface
@@ -67,7 +68,7 @@ public static function get(string $url, array $query = []): PromiseInterface
6768
* Performs a quick, asynchronous POST request with a JSON payload.
6869
*
6970
* @param string $url The target URL.
70-
* @param array $data Data to be JSON-encoded.
71+
* @param array<string, mixed> $data Data to be JSON-encoded.
7172
* @return PromiseInterface<Response> A promise that resolves with a Response object.
7273
*/
7374
public static function post(string $url, array $data = []): PromiseInterface
@@ -79,7 +80,7 @@ public static function post(string $url, array $data = []): PromiseInterface
7980
* Performs a quick, asynchronous PUT request.
8081
*
8182
* @param string $url The target URL.
82-
* @param array $data Data to be JSON-encoded.
83+
* @param array<string, mixed> $data Data to be JSON-encoded.
8384
* @return PromiseInterface<Response> A promise that resolves with a Response object.
8485
*/
8586
public static function put(string $url, array $data = []): PromiseInterface
@@ -102,7 +103,7 @@ public static function delete(string $url): PromiseInterface
102103
* A flexible, fetch-like method for making HTTP requests.
103104
*
104105
* @param string $url The target URL.
105-
* @param array $options Associative array of request options (method, headers, body, etc.).
106+
* @param array<int|string, mixed> $options Associative array of request options (method, headers, body, etc.).
106107
* @return PromiseInterface<Response> A promise that resolves with a Response object.
107108
*/
108109
public static function fetch(string $url, array $options = []): PromiseInterface
@@ -114,11 +115,11 @@ public static function fetch(string $url, array $options = []): PromiseInterface
114115
* Streams an HTTP response, processing it in chunks.
115116
*
116117
* @param string $url The URL to stream from.
117-
* @param array $options Advanced cURL or request options.
118+
* @param array<int|string, mixed> $options Advanced cURL or request options.
118119
* @param callable|null $onChunk Optional callback for each data chunk.
119-
* @return PromiseInterface<StreamingResponse> A promise resolving with a StreamingResponse object.
120+
* @return CancellablePromiseInterface<StreamingResponse> A promise resolving with a StreamingResponse object.
120121
*/
121-
public static function stream(string $url, array $options = [], ?callable $onChunk = null): PromiseInterface
122+
public static function stream(string $url, array $options = [], ?callable $onChunk = null): CancellablePromiseInterface
122123
{
123124
return self::getInstance()->stream($url, $options, $onChunk);
124125
}
@@ -128,10 +129,10 @@ public static function stream(string $url, array $options = [], ?callable $onChu
128129
*
129130
* @param string $url The URL of the file to download.
130131
* @param string $destination The local path to save the file.
131-
* @param array $options Advanced cURL or request options.
132-
* @return PromiseInterface<array{file: string, status: int|null, headers: array}> A promise resolving with download metadata.
132+
* @param array<int|string, mixed> $options Advanced cURL or request options.
133+
* @return CancellablePromiseInterface<array{file: string, status: int, headers: array<mixed>}> A promise resolving with download metadata.
133134
*/
134-
public static function download(string $url, string $destination, array $options = []): PromiseInterface
135+
public static function download(string $url, string $destination, array $options = []): CancellablePromiseInterface
135136
{
136137
return self::getInstance()->download($url, $destination, $options);
137138
}
@@ -158,11 +159,11 @@ public static function setInstance(HttpHandler $handler): void
158159
* Magic method to handle dynamic static calls and proxy them to the handler instance.
159160
*
160161
* @param string $method The method name.
161-
* @param array $arguments The arguments to pass to the method.
162+
* @param array<mixed> $arguments The arguments to pass to the method.
162163
* @return mixed The result of the proxied method call.
163164
*/
164165
public static function __callStatic(string $method, array $arguments)
165166
{
166167
return self::getInstance()->{$method}(...$arguments);
167168
}
168-
}
169+
}

src/Api/AsyncLoop.php renamed to src/Api/Task.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414
* and stopping the event loop automatically, making it ideal for simple
1515
* async workflows and batch processing.
1616
*/
17-
final class AsyncLoop
17+
final class Task
1818
{
1919
/**
20-
* @var AsyncOperations|null Cached instance of core async operations handler
20+
* @var AsyncOperations|null Cached instance of core async operations handler.
2121
*/
2222
private static ?AsyncOperations $asyncOps = null;
2323

2424
/**
25-
* @var LoopOperations|null Cached instance of loop operations handler
25+
* @var LoopOperations|null Cached instance of loop operations handler.
2626
*/
2727
private static ?LoopOperations $loopOps = null;
2828

2929
/**
3030
* Get the singleton instance of AsyncOperations with lazy initialization.
3131
*
32-
* @return AsyncOperations The core async operations handler
32+
* @return AsyncOperations The core async operations handler.
3333
*/
3434
protected static function getAsyncOperations(): AsyncOperations
3535
{
@@ -43,7 +43,7 @@ protected static function getAsyncOperations(): AsyncOperations
4343
/**
4444
* Get the singleton instance of LoopOperations with lazy initialization.
4545
*
46-
* @return LoopOperations The loop operations handler with automatic lifecycle management
46+
* @return LoopOperations The loop operations handler with automatic lifecycle management.
4747
*/
4848
protected static function getLoopOperations(): LoopOperations
4949
{
@@ -56,6 +56,8 @@ protected static function getLoopOperations(): LoopOperations
5656

5757
/**
5858
* Reset all cached instances to their initial state.
59+
*
60+
* @return void
5961
*/
6062
public static function reset(): void
6163
{
@@ -66,8 +68,8 @@ public static function reset(): void
6668
/**
6769
* Run an async operation with automatic event loop management.
6870
*
69-
* @param callable|PromiseInterface $asyncOperation The operation to execute
70-
* @return mixed The result of the async operation
71+
* @param callable(): mixed|PromiseInterface<mixed> $asyncOperation The operation to execute.
72+
* @return mixed The result of the async operation.
7173
*/
7274
public static function run(callable|PromiseInterface $asyncOperation): mixed
7375
{
@@ -77,8 +79,8 @@ public static function run(callable|PromiseInterface $asyncOperation): mixed
7779
/**
7880
* Run multiple async operations concurrently with automatic loop management.
7981
*
80-
* @param array $asyncOperations Array of callables or promises to execute
81-
* @return array Results of all operations in the same order as input
82+
* @param array<int|string, callable(): mixed|PromiseInterface<mixed>> $asyncOperations Array of callables or promises to execute.
83+
* @return array<mixed> Results of all operations in the same order as input.
8284
*/
8385
public static function runAll(array $asyncOperations): array
8486
{
@@ -88,9 +90,9 @@ public static function runAll(array $asyncOperations): array
8890
/**
8991
* Run async operations with concurrency control and automatic loop management.
9092
*
91-
* @param array $asyncOperations Array of operations to execute
92-
* @param int $concurrency Maximum number of concurrent operations
93-
* @return array Results of all operations
93+
* @param array<int|string, callable(): mixed|PromiseInterface<mixed>> $asyncOperations Array of operations to execute.
94+
* @param int $concurrency Maximum number of concurrent operations.
95+
* @return array<mixed> Results of all operations.
9496
*/
9597
public static function runConcurrent(array $asyncOperations, int $concurrency = 10): array
9698
{
@@ -100,11 +102,11 @@ public static function runConcurrent(array $asyncOperations, int $concurrency =
100102
/**
101103
* Run an async operation with a timeout constraint and automatic loop management.
102104
*
103-
* @param callable|PromiseInterface|array $asyncOperation The operation to execute
104-
* @param float $timeout Maximum time to wait in seconds
105-
* @return mixed The result of the operation if completed within timeout
105+
* @param callable(): mixed|PromiseInterface<mixed>|array<int|string, callable(): mixed|PromiseInterface<mixed>> $asyncOperation The operation to execute.
106+
* @param float $timeout Maximum time to wait in seconds.
107+
* @return mixed The result of the operation if completed within timeout.
106108
*
107-
* @throws \Exception If the operation times out
109+
* @throws \Exception If the operation times out.
108110
*/
109111
public static function runWithTimeout(callable|PromiseInterface|array $asyncOperation, float $timeout): mixed
110112
{
@@ -114,13 +116,13 @@ public static function runWithTimeout(callable|PromiseInterface|array $asyncOper
114116
/**
115117
* Run async operations in batches with concurrency control and automatic loop management.
116118
*
117-
* @param array $asyncOperations Array of operations to execute
118-
* @param int $batch Number of operations to run in each batch
119-
* @param int|null $concurrency Maximum number of concurrent operations per batch
120-
* @return array Results of all operations
119+
* @param array<int|string, callable(): mixed|PromiseInterface<mixed>> $asyncOperations Array of operations to execute.
120+
* @param int $batch Number of operations to run in each batch.
121+
* @param int|null $concurrency Maximum number of concurrent operations per batch.
122+
* @return array<mixed> Results of all operations.
121123
*/
122124
public static function runBatch(array $asyncOperations, int $batch, ?int $concurrency = null): array
123125
{
124126
return self::getLoopOperations()->runBatch($asyncOperations, $batch, $concurrency);
125127
}
126-
}
128+
}

src/Api/Timer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Rcalicdan\FiberAsync\Api;
44

55
use Rcalicdan\FiberAsync\Async\AsyncOperations;
6-
use Rcalicdan\FiberAsync\Promise\Interfaces\PromiseInterface;
6+
use Rcalicdan\FiberAsync\Promise\Interfaces\CancellablePromiseInterface;
77

88
/**
99
* Static API for timer-based asynchronous operations.
@@ -55,9 +55,9 @@ public static function reset(): void
5555
* in async execution, implementing retry delays, or rate limiting operations.
5656
*
5757
* @param float $seconds Number of seconds to delay (supports fractional seconds)
58-
* @return PromiseInterface A promise that resolves with null after the delay
58+
* @return CancellablePromiseInterface A promise that resolves with null after the delay
5959
*/
60-
public static function delay(float $seconds): PromiseInterface
60+
public static function delay(float $seconds): CancellablePromiseInterface
6161
{
6262
return self::getAsyncOperations()->delay($seconds);
6363
}

src/Async/AsyncOperations.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Rcalicdan\FiberAsync\Async\Handlers\PromiseHandler;
1111
use Rcalicdan\FiberAsync\Async\Handlers\TimerHandler;
1212
use Rcalicdan\FiberAsync\Async\Interfaces\AsyncOperationsInterface;
13+
use Rcalicdan\FiberAsync\Promise\Interfaces\CancellablePromiseInterface;
1314
use Rcalicdan\FiberAsync\Promise\Interfaces\PromiseInterface;
1415

1516
/**
@@ -147,9 +148,9 @@ public function await(PromiseInterface $promise): mixed
147148
* Create a promise that resolves after a specified delay.
148149
*
149150
* @param float $seconds Number of seconds to delay
150-
* @return PromiseInterface<null> A promise that resolves after the delay
151+
* @return CancellablePromiseInterface<null> A promise that resolves after the delay
151152
*/
152-
public function delay(float $seconds): PromiseInterface
153+
public function delay(float $seconds): CancellablePromiseInterface
153154
{
154155
return $this->timerHandler->delay($seconds);
155156
}

src/Async/Interfaces/AsyncOperationsInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Rcalicdan\FiberAsync\Async\Interfaces;
44

5+
use Rcalicdan\FiberAsync\Promise\Interfaces\CancellablePromiseInterface;
56
use Rcalicdan\FiberAsync\Promise\Interfaces\PromiseInterface;
67
use Throwable;
78

@@ -65,9 +66,9 @@ public function await(PromiseInterface $promise): mixed;
6566
* Creates a promise that resolves with null after the specified delay.
6667
*
6768
* @param float $seconds The delay in seconds (supports fractions for milliseconds).
68-
* @return PromiseInterface<null> A promise that resolves with null after the delay.
69+
* @return CancellablePromiseInterface<null> A promise that resolves with null after the delay.
6970
*/
70-
public function delay(float $seconds): PromiseInterface;
71+
public function delay(float $seconds): CancellablePromiseInterface;
7172

7273
/**
7374
* Waits for all promises to resolve or any to reject.

0 commit comments

Comments
 (0)