| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Promise; |
| 4 | 5 | |
| 5 | 6 | final class Utils |
| 6 | 7 | { |
| @@ -16,13 +17,11 @@ | ||
| 16 | 17 | * GuzzleHttp\Promise\Utils::queue()->run(); |
| 17 | 18 | * } |
| 18 | 19 | * </code> |
| 19 | 20 | * |
| 20 | - * @param TaskQueueInterface $assign Optionally specify a new queue instance. | |
| 21 | - * | |
| 22 | - * @return TaskQueueInterface | |
| 21 | + * @param TaskQueueInterface|null $assign Optionally specify a new queue instance. | |
| 23 | 22 | */ |
| 24 | - public static function queue(TaskQueueInterface $assign = null) | |
| 23 | + public static function queue(?TaskQueueInterface $assign = null) : TaskQueueInterface | |
| 25 | 24 | { |
| 26 | 25 | static $queue; |
| 27 | 26 | if ($assign) { |
| 28 | 27 | $queue = $assign; |
| @@ -35,16 +34,14 @@ | ||
| 35 | 34 | * Adds a function to run in the task queue when it is next `run()` and |
| 36 | 35 | * returns a promise that is fulfilled or rejected with the result. |
| 37 | 36 | * |
| 38 | 37 | * @param callable $task Task function to run. |
| 39 | - * | |
| 40 | - * @return PromiseInterface | |
| 41 | 38 | */ |
| 42 | - public static function task(callable $task) | |
| 39 | + public static function task(callable $task) : PromiseInterface | |
| 43 | 40 | { |
| 44 | 41 | $queue = self::queue(); |
| 45 | 42 | $promise = new Promise([$queue, 'run']); |
| 46 | - $queue->add(function () use($task, $promise) { | |
| 43 | + $queue->add(function () use($task, $promise) : void { | |
| 47 | 44 | try { |
| 48 | 45 | if (Is::pending($promise)) { |
| 49 | 46 | $promise->resolve($task()); |
| 50 | 47 | } |
| @@ -49,10 +46,8 @@ | ||
| 49 | 46 | $promise->resolve($task()); |
| 50 | 47 | } |
| 51 | 48 | } catch (\Throwable $e) { |
| 52 | 49 | $promise->reject($e); |
| 53 | - } catch (\Exception $e) { | |
| 54 | - $promise->reject($e); | |
| 55 | 50 | } |
| 56 | 51 | }); |
| 57 | 52 | return $promise; |
| 58 | 53 | } |
| @@ -66,12 +61,10 @@ | ||
| 66 | 61 | * promise. If the promise is rejected, the array will contain a "reason" |
| 67 | 62 | * key mapping to the rejection reason of the promise. |
| 68 | 63 | * |
| 69 | 64 | * @param PromiseInterface $promise Promise or value. |
| 70 | - * | |
| 71 | - * @return array | |
| 72 | 65 | */ |
| 73 | - public static function inspect(PromiseInterface $promise) | |
| 66 | + public static function inspect(PromiseInterface $promise) : array | |
| 74 | 67 | { |
| 75 | 68 | try { |
| 76 | 69 | return ['state' => PromiseInterface::FULFILLED, 'value' => $promise->wait()]; |
| 77 | 70 | } catch (RejectionException $e) { |
| @@ -77,10 +70,8 @@ | ||
| 77 | 70 | } catch (RejectionException $e) { |
| 78 | 71 | return ['state' => PromiseInterface::REJECTED, 'reason' => $e->getReason()]; |
| 79 | 72 | } catch (\Throwable $e) { |
| 80 | 73 | return ['state' => PromiseInterface::REJECTED, 'reason' => $e]; |
| 81 | - } catch (\Exception $e) { | |
| 82 | - return ['state' => PromiseInterface::REJECTED, 'reason' => $e]; | |
| 83 | 74 | } |
| 84 | 75 | } |
| 85 | 76 | /** |
| 86 | 77 | * Waits on all of the provided promises, but does not unwrap rejected |
| @@ -90,16 +81,14 @@ | ||
| 90 | 81 | * |
| 91 | 82 | * @see inspect for the inspection state array format. |
| 92 | 83 | * |
| 93 | 84 | * @param PromiseInterface[] $promises Traversable of promises to wait upon. |
| 94 | - * | |
| 95 | - * @return array | |
| 96 | 85 | */ |
| 97 | - public static function inspectAll($promises) | |
| 86 | + public static function inspectAll($promises) : array | |
| 98 | 87 | { |
| 99 | 88 | $results = []; |
| 100 | 89 | foreach ($promises as $key => $promise) { |
| 101 | - $results[$key] = inspect($promise); | |
| 90 | + $results[$key] = self::inspect($promise); | |
| 102 | 91 | } |
| 103 | 92 | return $results; |
| 104 | 93 | } |
| 105 | 94 | /** |
| @@ -110,14 +99,11 @@ | ||
| 110 | 99 | * promises are rejected. |
| 111 | 100 | * |
| 112 | 101 | * @param iterable<PromiseInterface> $promises Iterable of PromiseInterface objects to wait on. |
| 113 | 102 | * |
| 114 | - * @return array | |
| 115 | - * | |
| 116 | - * @throws \Exception on error | |
| 117 | - * @throws \Throwable on error in PHP >=7 | |
| 103 | + * @throws \Throwable on error | |
| 118 | 104 | */ |
| 119 | - public static function unwrap($promises) | |
| 105 | + public static function unwrap($promises) : array | |
| 120 | 106 | { |
| 121 | 107 | $results = []; |
| 122 | 108 | foreach ($promises as $key => $promise) { |
| 123 | 109 | $results[$key] = $promise->wait(); |
| @@ -133,18 +119,18 @@ | ||
| 133 | 119 | * rejects, the returned promise is rejected with the rejection reason. |
| 134 | 120 | * |
| 135 | 121 | * @param mixed $promises Promises or values. |
| 136 | 122 | * @param bool $recursive If true, resolves new promises that might have been added to the stack during its own resolution. |
| 137 | - * | |
| 138 | - * @return PromiseInterface | |
| 139 | 123 | */ |
| 140 | - public static function all($promises, $recursive = \false) | |
| 124 | + public static function all($promises, bool $recursive = \false) : PromiseInterface | |
| 141 | 125 | { |
| 142 | 126 | $results = []; |
| 143 | - $promise = Each::of($promises, function ($value, $idx) use(&$results) { | |
| 127 | + $promise = Each::of($promises, function ($value, $idx) use(&$results) : void { | |
| 144 | 128 | $results[$idx] = $value; |
| 145 | - }, function ($reason, $idx, Promise $aggregate) { | |
| 146 | - $aggregate->reject($reason); | |
| 129 | + }, function ($reason, $idx, Promise $aggregate) : void { | |
| 130 | + if (Is::pending($aggregate)) { | |
| 131 | + $aggregate->reject($reason); | |
| 132 | + } | |
| 147 | 133 | })->then(function () use(&$results) { |
| 148 | 134 | \ksort($results); |
| 149 | 135 | return $results; |
| 150 | 136 | }); |
| @@ -172,16 +158,14 @@ | ||
| 172 | 158 | * of fulfilled promises is less than the desired $count. |
| 173 | 159 | * |
| 174 | 160 | * @param int $count Total number of promises. |
| 175 | 161 | * @param mixed $promises Promises or values. |
| 176 | - * | |
| 177 | - * @return PromiseInterface | |
| 178 | 162 | */ |
| 179 | - public static function some($count, $promises) | |
| 163 | + public static function some(int $count, $promises) : PromiseInterface | |
| 180 | 164 | { |
| 181 | 165 | $results = []; |
| 182 | 166 | $rejections = []; |
| 183 | - return Each::of($promises, function ($value, $idx, PromiseInterface $p) use(&$results, $count) { | |
| 167 | + return Each::of($promises, function ($value, $idx, PromiseInterface $p) use(&$results, $count) : void { | |
| 184 | 168 | if (Is::settled($p)) { |
| 185 | 169 | return; |
| 186 | 170 | } |
| 187 | 171 | $results[$idx] = $value; |
| @@ -187,9 +171,9 @@ | ||
| 187 | 171 | $results[$idx] = $value; |
| 188 | 172 | if (\count($results) >= $count) { |
| 189 | 173 | $p->resolve(null); |
| 190 | 174 | } |
| 191 | - }, function ($reason) use(&$rejections) { | |
| 175 | + }, function ($reason) use(&$rejections) : void { | |
| 192 | 176 | $rejections[] = $reason; |
| 193 | 177 | })->then(function () use(&$results, &$rejections, $count) { |
| 194 | 178 | if (\count($results) !== $count) { |
| 195 | 179 | throw new AggregateException('Not enough promises to fulfill count', $rejections); |
| @@ -202,12 +186,10 @@ | ||
| 202 | 186 | * Like some(), with 1 as count. However, if the promise fulfills, the |
| 203 | 187 | * fulfillment value is not an array of 1 but the value directly. |
| 204 | 188 | * |
| 205 | 189 | * @param mixed $promises Promises or values. |
| 206 | - * | |
| 207 | - * @return PromiseInterface | |
| 208 | 190 | */ |
| 209 | - public static function any($promises) | |
| 191 | + public static function any($promises) : PromiseInterface | |
| 210 | 192 | { |
| 211 | 193 | return self::some(1, $promises)->then(function ($values) { |
| 212 | 194 | return $values[0]; |
| 213 | 195 | }); |
| @@ -220,17 +202,15 @@ | ||
| 220 | 202 | * |
| 221 | 203 | * @see inspect for the inspection state array format. |
| 222 | 204 | * |
| 223 | 205 | * @param mixed $promises Promises or values. |
| 224 | - * | |
| 225 | - * @return PromiseInterface | |
| 226 | 206 | */ |
| 227 | - public static function settle($promises) | |
| 207 | + public static function settle($promises) : PromiseInterface | |
| 228 | 208 | { |
| 229 | 209 | $results = []; |
| 230 | - return Each::of($promises, function ($value, $idx) use(&$results) { | |
| 210 | + return Each::of($promises, function ($value, $idx) use(&$results) : void { | |
| 231 | 211 | $results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value]; |
| 232 | - }, function ($reason, $idx) use(&$results) { | |
| 212 | + }, function ($reason, $idx) use(&$results) : void { | |
| 233 | 213 | $results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason]; |
| 234 | 214 | })->then(function () use(&$results) { |
| 235 | 215 | \ksort($results); |
| 236 | 216 | return $results; |