| @@ -1,12 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Promise; |
| 4 | 5 | |
| 5 | 6 | /** |
| 6 | 7 | * Promises/A+ implementation that avoids recursion when possible. |
| 7 | 8 | * |
| 8 | - * @link https://promisesaplus.com/ | |
| 9 | + * @see https://promisesaplus.com/ | |
| 10 | + * | |
| 11 | + * @final | |
| 9 | 12 | */ |
| 10 | 13 | class Promise implements PromiseInterface |
| 11 | 14 | { |
| 12 | 15 | private $state = self::PENDING; |
| @@ -18,14 +21,14 @@ | ||
| 18 | 21 | /** |
| 19 | 22 | * @param callable $waitFn Fn that when invoked resolves the promise. |
| 20 | 23 | * @param callable $cancelFn Fn that when invoked cancels the promise. |
| 21 | 24 | */ |
| 22 | - public function __construct(callable $waitFn = null, callable $cancelFn = null) | |
| 25 | + public function __construct(?callable $waitFn = null, ?callable $cancelFn = null) | |
| 23 | 26 | { |
| 24 | 27 | $this->waitFn = $waitFn; |
| 25 | 28 | $this->cancelFn = $cancelFn; |
| 26 | 29 | } |
| 27 | - public function then(callable $onFulfilled = null, callable $onRejected = null) | |
| 30 | + public function then(?callable $onFulfilled = null, ?callable $onRejected = null) : PromiseInterface | |
| 28 | 31 | { |
| 29 | 32 | if ($this->state === self::PENDING) { |
| 30 | 33 | $p = new Promise(null, [$this, 'cancel']); |
| 31 | 34 | $this->handlers[] = [$p, $onFulfilled, $onRejected]; |
| @@ -42,13 +45,13 @@ | ||
| 42 | 45 | // and immediately invoke any callbacks. |
| 43 | 46 | $rejection = Create::rejectionFor($this->result); |
| 44 | 47 | return $onRejected ? $rejection->then(null, $onRejected) : $rejection; |
| 45 | 48 | } |
| 46 | - public function otherwise(callable $onRejected) | |
| 49 | + public function otherwise(callable $onRejected) : PromiseInterface | |
| 47 | 50 | { |
| 48 | 51 | return $this->then(null, $onRejected); |
| 49 | 52 | } |
| 50 | - public function wait($unwrap = \true) | |
| 53 | + public function wait(bool $unwrap = \true) | |
| 51 | 54 | { |
| 52 | 55 | $this->waitIfPending(); |
| 53 | 56 | if ($this->result instanceof PromiseInterface) { |
| 54 | 57 | return $this->result->wait($unwrap); |
| @@ -60,13 +63,13 @@ | ||
| 60 | 63 | // It's rejected so "unwrap" and throw an exception. |
| 61 | 64 | throw Create::exceptionFor($this->result); |
| 62 | 65 | } |
| 63 | 66 | } |
| 64 | - public function getState() | |
| 67 | + public function getState() : string | |
| 65 | 68 | { |
| 66 | 69 | return $this->state; |
| 67 | 70 | } |
| 68 | - public function cancel() | |
| 71 | + public function cancel() : void | |
| 69 | 72 | { |
| 70 | 73 | if ($this->state !== self::PENDING) { |
| 71 | 74 | return; |
| 72 | 75 | } |
| @@ -77,10 +80,8 @@ | ||
| 77 | 80 | try { |
| 78 | 81 | $fn(); |
| 79 | 82 | } catch (\Throwable $e) { |
| 80 | 83 | $this->reject($e); |
| 81 | - } catch (\Exception $e) { | |
| 82 | - $this->reject($e); | |
| 83 | 84 | } |
| 84 | 85 | } |
| 85 | 86 | // Reject the promise only if it wasn't rejected in a then callback. |
| 86 | 87 | /** @psalm-suppress RedundantCondition */ |
| @@ -87,17 +88,17 @@ | ||
| 87 | 88 | if ($this->state === self::PENDING) { |
| 88 | 89 | $this->reject(new CancellationException('Promise has been cancelled')); |
| 89 | 90 | } |
| 90 | 91 | } |
| 91 | - public function resolve($value) | |
| 92 | + public function resolve($value) : void | |
| 92 | 93 | { |
| 93 | 94 | $this->settle(self::FULFILLED, $value); |
| 94 | 95 | } |
| 95 | - public function reject($reason) | |
| 96 | + public function reject($reason) : void | |
| 96 | 97 | { |
| 97 | 98 | $this->settle(self::REJECTED, $reason); |
| 98 | 99 | } |
| 99 | - private function settle($state, $value) | |
| 100 | + private function settle(string $state, $value) : void | |
| 100 | 101 | { |
| 101 | 102 | if ($this->state !== self::PENDING) { |
| 102 | 103 | // Ignore calls with the same resolution. |
| 103 | 104 | if ($state === $this->state && $value === $this->result) { |
| @@ -122,9 +123,9 @@ | ||
| 122 | 123 | // it in the task queue using the correct ID. |
| 123 | 124 | if (!\is_object($value) || !\method_exists($value, 'then')) { |
| 124 | 125 | $id = $state === self::FULFILLED ? 1 : 2; |
| 125 | 126 | // It's a success, so resolve the handlers in the queue. |
| 126 | - Utils::queue()->add(static function () use($id, $value, $handlers) { | |
| 127 | + Utils::queue()->add(static function () use($id, $value, $handlers) : void { | |
| 127 | 128 | foreach ($handlers as $handler) { |
| 128 | 129 | self::callHandler($id, $value, $handler); |
| 129 | 130 | } |
| 130 | 131 | }); |
| @@ -132,13 +133,13 @@ | ||
| 132 | 133 | // We can just merge our handlers onto the next promise. |
| 133 | 134 | $value->handlers = \array_merge($value->handlers, $handlers); |
| 134 | 135 | } else { |
| 135 | 136 | // Resolve the handlers when the forwarded promise is resolved. |
| 136 | - $value->then(static function ($value) use($handlers) { | |
| 137 | + $value->then(static function ($value) use($handlers) : void { | |
| 137 | 138 | foreach ($handlers as $handler) { |
| 138 | 139 | self::callHandler(1, $value, $handler); |
| 139 | 140 | } |
| 140 | - }, static function ($reason) use($handlers) { | |
| 141 | + }, static function ($reason) use($handlers) : void { | |
| 141 | 142 | foreach ($handlers as $handler) { |
| 142 | 143 | self::callHandler(2, $reason, $handler); |
| 143 | 144 | } |
| 144 | 145 | }); |
| @@ -150,9 +151,9 @@ | ||
| 150 | 151 | * @param int $index 1 (resolve) or 2 (reject). |
| 151 | 152 | * @param mixed $value Value to pass to the callback. |
| 152 | 153 | * @param array $handler Array of handler data (promise and callbacks). |
| 153 | 154 | */ |
| 154 | - private static function callHandler($index, $value, array $handler) | |
| 155 | + private static function callHandler(int $index, $value, array $handler) : void | |
| 155 | 156 | { |
| 156 | 157 | /** @var PromiseInterface $promise */ |
| 157 | 158 | $promise = $handler[0]; |
| 158 | 159 | // The promise may have been cancelled or resolved before placing |
| @@ -179,13 +180,11 @@ | ||
| 179 | 180 | $promise->reject($value); |
| 180 | 181 | } |
| 181 | 182 | } catch (\Throwable $reason) { |
| 182 | 183 | $promise->reject($reason); |
| 183 | - } catch (\Exception $reason) { | |
| 184 | - $promise->reject($reason); | |
| 185 | 184 | } |
| 186 | 185 | } |
| 187 | - private function waitIfPending() | |
| 186 | + private function waitIfPending() : void | |
| 188 | 187 | { |
| 189 | 188 | if ($this->state !== self::PENDING) { |
| 190 | 189 | return; |
| 191 | 190 | } elseif ($this->waitFn) { |
| @@ -201,15 +200,15 @@ | ||
| 201 | 200 | if ($this->state === self::PENDING) { |
| 202 | 201 | $this->reject('Invoking the wait callback did not resolve the promise'); |
| 203 | 202 | } |
| 204 | 203 | } |
| 205 | - private function invokeWaitFn() | |
| 204 | + private function invokeWaitFn() : void | |
| 206 | 205 | { |
| 207 | 206 | try { |
| 208 | 207 | $wfn = $this->waitFn; |
| 209 | 208 | $this->waitFn = null; |
| 210 | 209 | $wfn(\true); |
| 211 | - } catch (\Exception $reason) { | |
| 210 | + } catch (\Throwable $reason) { | |
| 212 | 211 | if ($this->state === self::PENDING) { |
| 213 | 212 | // The promise has not been resolved yet, so reject the promise |
| 214 | 213 | // with the exception. |
| 215 | 214 | $this->reject($reason); |
| @@ -219,9 +218,9 @@ | ||
| 219 | 218 | throw $reason; |
| 220 | 219 | } |
| 221 | 220 | } |
| 222 | 221 | } |
| 223 | - private function invokeWaitList() | |
| 222 | + private function invokeWaitList() : void | |
| 224 | 223 | { |
| 225 | 224 | $waitList = $this->waitList; |
| 226 | 225 | $this->waitList = null; |
| 227 | 226 | foreach ($waitList as $result) { |