Skip to content

Commit 95b4fd0

Browse files
authored
PHPStan level 9
1 parent 83748d2 commit 95b4fd0

File tree

12 files changed

+281
-148
lines changed

12 files changed

+281
-148
lines changed

src/vennv/AggregateError.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ final class AggregateError extends Error
88
{
99

1010
public function __construct(
11-
protected $message,
12-
protected $code = 0
11+
protected string $errorMessage,
12+
protected int $errorCode = 0
1313
)
1414
{
1515
parent::__construct(
16-
$message,
17-
$code
16+
$this->errorMessage,
17+
$this->errorCode
1818
);
1919
}
2020

2121
public function __toString() : string
2222
{
23-
return __CLASS__ . ": [$this->code]: $this->message\n";
23+
return __CLASS__ . ": [$this->errorCode]: $this->errorMessage\n";
2424
}
2525

2626
}

src/vennv/Async.php

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,28 @@ public function __construct(callable $callable)
1919
$this->id = EventQueue::addQueue(new Fiber($callable));
2020

2121
$queue = EventQueue::getQueue($this->id);
22-
$fiber = $queue->getFiber();
2322

24-
if (!$fiber->isStarted())
23+
if (!is_null($queue))
2524
{
26-
try
25+
$fiber = $queue->getFiber();
26+
if (!$fiber->isStarted())
2727
{
28-
$fiber->start();
29-
}
30-
catch (Exception | Throwable $error)
31-
{
32-
EventQueue::rejectQueue($this->id, $error->getMessage());
28+
try
29+
{
30+
$fiber->start();
31+
}
32+
catch (Exception | Throwable $error)
33+
{
34+
EventQueue::rejectQueue($this->id, $error->getMessage());
35+
}
3336
}
3437
}
3538
}
3639

3740
/**
3841
* @throws Throwable
3942
*/
40-
public static function await(mixed $callable) : mixed
43+
public static function await(callable|Promise|Async $callable) : mixed
4144
{
4245
$result = $callable;
4346

@@ -54,34 +57,42 @@ public static function await(mixed $callable) : mixed
5457
$result = $fiber->getReturn();
5558
}
5659

57-
if (
58-
$callable instanceof Promise ||
59-
$callable instanceof Async ||
60-
$result instanceof Promise ||
61-
$result instanceof Async
62-
)
60+
if ($callable instanceof Promise || $callable instanceof Async)
6361
{
64-
$queue = EventQueue::getQueue($callable->getId());
62+
self::awaitPromise($callable, $result);
63+
}
64+
65+
if ($result instanceof Promise || $result instanceof Async)
66+
{
67+
self::awaitPromise($result, $result);
68+
}
69+
70+
return $result;
71+
}
72+
73+
/**
74+
* @throws Throwable
75+
*/
76+
private static function awaitPromise(Async|Promise $promise, mixed &$result) : void
77+
{
78+
$queue = EventQueue::getQueue($promise->getId());
6579

66-
if (!is_null($queue))
80+
if (!is_null($queue))
81+
{
82+
while ($queue->getStatus() == StatusQueue::PENDING)
6783
{
68-
while ($queue->getStatus() === StatusQueue::PENDING)
84+
if (
85+
$queue->getStatus() == StatusQueue::REJECTED ||
86+
$queue->getStatus() == StatusQueue::FULFILLED
87+
)
6988
{
70-
if (
71-
$queue->getStatus() === StatusQueue::REJECTED ||
72-
$queue->getStatus() === StatusQueue::FULFILLED
73-
)
74-
{
75-
break;
76-
}
77-
self::wait();
89+
break;
7890
}
79-
80-
$result = $queue->getReturn();
91+
self::wait();
8192
}
82-
}
8393

84-
return $result;
94+
$result = $queue->getReturn();
95+
}
8596
}
8697

8798
/**

src/vennv/AsyncError.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ final class AsyncError extends TypeError
88
{
99

1010
public function __construct(
11-
protected $message,
12-
protected $code = 0
11+
protected string $errorMessage,
12+
protected int $errorCode = 0
1313
)
1414
{
1515
parent::__construct(
16-
$message,
17-
$code
16+
$this->errorMessage,
17+
$this->errorCode
1818
);
1919
}
2020

2121
public function __toString() : string
2222
{
23-
return __CLASS__ . ": [$this->code]: $this->message\n";
23+
return __CLASS__ . ": [$this->errorCode]: $this->errorMessage\n";
2424
}
2525

2626
}

src/vennv/EventQueue.php

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ class EventQueue implements InterfaceEventQueue
1313
private const TIME_OUT = 10;
1414

1515
private static int $nextId = 0;
16+
17+
/**
18+
* @var Queue[]
19+
*/
1620
private static array $queues = [];
21+
22+
/**
23+
* @var Queue[]
24+
*/
1725
private static array $returns = [];
1826

1927
private static function generateId() : int
@@ -54,7 +62,7 @@ public static function getQueue(int $id) : ?Queue
5462
return self::$queues[$id] ?? null;
5563
}
5664

57-
public static function getReturn(int $id) : mixed
65+
public static function getReturn(int $id) : ?Queue
5866
{
5967
return self::$returns[$id] ?? null;
6068
}
@@ -98,7 +106,7 @@ private static function doResult(int $id) : void
98106
break;
99107
case StatusQueue::PENDING:
100108
throw new EventQueueError(
101-
str_replace("%id%", $id, Error::QUEUE_STILL_PENDING)
109+
str_replace("%id%", "$id", Error::QUEUE_STILL_PENDING)
102110
);
103111
}
104112

@@ -108,7 +116,7 @@ private static function doResult(int $id) : void
108116
else
109117
{
110118
throw new EventQueueError(
111-
str_replace("%id%", $id, Error::QUEUE_NOT_FOUND)
119+
str_replace("%id%", "$id", Error::QUEUE_NOT_FOUND)
112120
);
113121
}
114122
}
@@ -136,20 +144,6 @@ public static function runQueue(int $id) : void
136144
}
137145
}
138146

139-
/**
140-
* @throws Throwable
141-
*/
142-
private static function queueFulfilled(int $id) : bool
143-
{
144-
$queue = self::getQueue($id);
145-
if ($queue !== null)
146-
{
147-
$status = $queue->getStatus();
148-
return $status === StatusQueue::FULFILLED || $status === StatusQueue::REJECTED;
149-
}
150-
return false;
151-
}
152-
153147
/**
154148
* @throws Throwable
155149
*/
@@ -275,7 +269,7 @@ private static function checkStatus(int $id) : void
275269
else
276270
{
277271
throw new EventQueueError(
278-
str_replace("%id%", $id, Error::QUEUE_NOT_FOUND)
272+
str_replace("%id%", "$id", Error::QUEUE_NOT_FOUND)
279273
);
280274
}
281275
}
@@ -298,24 +292,21 @@ private static function run() : void
298292
{
299293
foreach (self::$queues as $id => $queue)
300294
{
301-
if ($queue instanceof Queue)
295+
if (self::shouldCheckStatus($queue))
302296
{
303-
304-
if (self::shouldCheckStatus($queue))
305-
{
306-
self::checkStatus($id);
307-
}
297+
self::checkStatus($id);
298+
}
308299

309-
// If the queue is still pending after 10 seconds of timeout, reject it.
310-
// If you encounter this problem, your current promise trick is too bad.
311-
if (self::shouldCheckStatus($queue, self::TIME_OUT))
312-
{
313-
self::rejectQueue(
314-
$id, str_replace("%id%", $id, Error::QUEUE_IS_TIMEOUT)
315-
);
316-
}
300+
// If the queue is still pending after 10 seconds of timeout, reject it.
301+
// If you encounter this problem, your current promise trick is too bad.
302+
if (self::shouldCheckStatus($queue, self::TIME_OUT))
303+
{
304+
self::rejectQueue(
305+
$id, str_replace("%id%", "$id", Error::QUEUE_IS_TIMEOUT)
306+
);
317307
}
318308
}
309+
319310
foreach (self::$returns as $id => $queue)
320311
{
321312
$canDrop = $queue->canDrop();

src/vennv/EventQueueError.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ final class EventQueueError extends TypeError
88
{
99

1010
public function __construct(
11-
protected $message,
12-
protected $code = 0
11+
protected string $errorMessage,
12+
protected int $errorCode = 0
1313
)
1414
{
1515
parent::__construct(
16-
$message,
17-
$code
16+
$this->errorMessage,
17+
$this->errorCode
1818
);
1919
}
2020

2121
public function __toString() : string
2222
{
23-
return __CLASS__ . ": [$this->code]: $this->message\n";
23+
return __CLASS__ . ": [$this->errorCode]: $this->errorMessage\n";
2424
}
2525

2626
}

src/vennv/InterfaceAsync.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface InterfaceAsync
1010
/**
1111
* This method is used to await a promise.
1212
*/
13-
public static function await(mixed $callable) : mixed;
13+
public static function await(callable|Promise|Async $callable) : mixed;
1414

1515
/**
1616
* @throws Throwable

src/vennv/InterfacePromise.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ interface InterfacePromise
1111
* This method is used to add a callback to the queue of callbacks
1212
* that will be executed when the promise is resolved.
1313
*/
14-
public function then(callable $callable) : Queue;
14+
public function then(callable $callable) : ?Queue;
1515

1616
/**
1717
* This method is used to add a callback to the queue of callbacks
1818
* that will be executed when the promise is rejected.
1919
*/
20-
public function catch(callable $callable) : Queue;
20+
public function catch(callable $callable) : ?Queue;
2121

2222
/**
2323
* This method is used to add a callback to the queue of callbacks
@@ -33,13 +33,15 @@ public static function reject(int $id, mixed $result) : void;
3333

3434
/**
3535
* @throws Throwable
36+
* @param array<callable|Promise|Async> $promises
3637
*
3738
* Fulfills when all the promises fulfill, rejects when any of the promises rejects.
3839
*/
3940
public static function all(array $promises) : Promise;
4041

4142
/**
4243
* @throws Throwable
44+
* @param array<callable|Promise|Async> $promises
4345
*
4446
* Settles when any of the promises settles.
4547
* In other words, fulfills when any of the promises fulfills, rejects when any of the promises rejects.
@@ -48,13 +50,15 @@ public static function race(array $promises) : Promise;
4850

4951
/**
5052
* @throws Throwable
53+
* @param array<callable|Promise|Async> $promises
5154
*
5255
* Fulfills when any of the promises fulfills, rejects when all the promises reject.
5356
*/
5457
public static function any(array $promises) : Promise;
5558

5659
/**
5760
* @throws Throwable
61+
* @param array<callable|Promise|Async> $promises
5862
*
5963
* Fulfills when all promises settle.
6064
*/

src/vennv/InterfaceSystem.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ interface InterfaceSystem {
1414
public static function setTimeout(callable $callable, int $timeout) : void;
1515

1616
/**
17+
* @param array<int, mixed> $options
18+
*
1719
* This method is used to fetch data from a url.
1820
*/
1921
public static function fetch(string $url, array $options = [CURLOPT_RETURNTRANSFER => true]) : Promise;

0 commit comments

Comments
 (0)