| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace YoastSEO_Vendor\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(\YoastSEO_Vendor\GuzzleHttp\Promise\TaskQueueInterface $assign = null) | |
| 23 | + public static function queue(?\YoastSEO_Vendor\GuzzleHttp\Promise\TaskQueueInterface $assign = null) : \YoastSEO_Vendor\GuzzleHttp\Promise\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) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface | |
| 43 | 40 | { |
| 44 | 41 | $queue = self::queue(); |
| 45 | 42 | $promise = new \YoastSEO_Vendor\GuzzleHttp\Promise\Promise([$queue, 'run']); |
| 46 | - $queue->add(function () use($task, $promise) { | |
| 43 | + $queue->add(function () use($task, $promise) : void { | |
| 47 | 44 | try { |
| 48 | 45 | if (\YoastSEO_Vendor\GuzzleHttp\Promise\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,21 +61,21 @@ | ||
| 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(\YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface $promise) | |
| 66 | + public static function inspect(\YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface $promise) : array | |
| 74 | 67 | { |
| 75 | 68 | try { |
| 76 | 69 | return ['state' => \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface::FULFILLED, 'value' => $promise->wait()]; |
| 77 | - } catch (\YoastSEO_Vendor\GuzzleHttp\Promise\RejectionException $e) { | |
| 78 | - return ['state' => \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface::REJECTED, 'reason' => $e->getReason()]; | |
| 79 | 70 | } catch (\Throwable $e) { |
| 71 | + if ($e instanceof \YoastSEO_Vendor\GuzzleHttp\Promise\AggregateException) { | |
| 72 | + return ['state' => \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface::REJECTED, 'reason' => $e]; | |
| 73 | + } | |
| 74 | + if ($e instanceof \YoastSEO_Vendor\GuzzleHttp\Promise\RejectionException) { | |
| 75 | + return ['state' => \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface::REJECTED, 'reason' => $e->getReason()]; | |
| 76 | + } | |
| 80 | 77 | return ['state' => \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface::REJECTED, 'reason' => $e]; |
| 81 | - } catch (\Exception $e) { | |
| 82 | - return ['state' => \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface::REJECTED, 'reason' => $e]; | |
| 83 | 78 | } |
| 84 | 79 | } |
| 85 | 80 | /** |
| 86 | 81 | * Waits on all of the provided promises, but does not unwrap rejected |
| @@ -90,16 +85,15 @@ | ||
| 90 | 85 | * |
| 91 | 86 | * @see inspect for the inspection state array format. |
| 92 | 87 | * |
| 93 | 88 | * @param PromiseInterface[] $promises Traversable of promises to wait upon. |
| 94 | - * | |
| 95 | - * @return array | |
| 96 | 89 | */ |
| 97 | - public static function inspectAll($promises) | |
| 90 | + public static function inspectAll($promises) : array | |
| 98 | 91 | { |
| 92 | + $promises = self::prepareIterable($promises, __FUNCTION__); | |
| 99 | 93 | $results = []; |
| 100 | 94 | foreach ($promises as $key => $promise) { |
| 101 | - $results[$key] = inspect($promise); | |
| 95 | + $results[$key] = self::inspect($promise); | |
| 102 | 96 | } |
| 103 | 97 | return $results; |
| 104 | 98 | } |
| 105 | 99 | /** |
| @@ -110,15 +104,13 @@ | ||
| 110 | 104 | * promises are rejected. |
| 111 | 105 | * |
| 112 | 106 | * @param iterable<PromiseInterface> $promises Iterable of PromiseInterface objects to wait on. |
| 113 | 107 | * |
| 114 | - * @return array | |
| 115 | - * | |
| 116 | - * @throws \Exception on error | |
| 117 | - * @throws \Throwable on error in PHP >=7 | |
| 108 | + * @throws \Throwable on error | |
| 118 | 109 | */ |
| 119 | - public static function unwrap($promises) | |
| 110 | + public static function unwrap($promises) : array | |
| 120 | 111 | { |
| 112 | + $promises = self::prepareIterable($promises, __FUNCTION__); | |
| 121 | 113 | $results = []; |
| 122 | 114 | foreach ($promises as $key => $promise) { |
| 123 | 115 | $results[$key] = $promise->wait(); |
| 124 | 116 | } |
| @@ -133,18 +125,19 @@ | ||
| 133 | 125 | * rejects, the returned promise is rejected with the rejection reason. |
| 134 | 126 | * |
| 135 | 127 | * @param mixed $promises Promises or values. |
| 136 | 128 | * @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 | 129 | */ |
| 140 | - public static function all($promises, $recursive = \false) | |
| 130 | + public static function all($promises, bool $recursive = \false) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface | |
| 141 | 131 | { |
| 132 | + $promises = self::prepareIterable($promises, __FUNCTION__); | |
| 142 | 133 | $results = []; |
| 143 | - $promise = \YoastSEO_Vendor\GuzzleHttp\Promise\Each::of($promises, function ($value, $idx) use(&$results) { | |
| 134 | + $promise = \YoastSEO_Vendor\GuzzleHttp\Promise\Each::of($promises, function ($value, $idx) use(&$results) : void { | |
| 144 | 135 | $results[$idx] = $value; |
| 145 | - }, function ($reason, $idx, \YoastSEO_Vendor\GuzzleHttp\Promise\Promise $aggregate) { | |
| 146 | - $aggregate->reject($reason); | |
| 136 | + }, function ($reason, $idx, \YoastSEO_Vendor\GuzzleHttp\Promise\Promise $aggregate) : void { | |
| 137 | + if (\YoastSEO_Vendor\GuzzleHttp\Promise\Is::pending($aggregate)) { | |
| 138 | + $aggregate->reject($reason); | |
| 139 | + } | |
| 147 | 140 | })->then(function () use(&$results) { |
| 148 | 141 | \ksort($results); |
| 149 | 142 | return $results; |
| 150 | 143 | }); |
| @@ -149,8 +142,13 @@ | ||
| 149 | 142 | return $results; |
| 150 | 143 | }); |
| 151 | 144 | if (\true === $recursive) { |
| 152 | 145 | $promise = $promise->then(function ($results) use($recursive, &$promises) { |
| 146 | + // A consumed generator cannot be traversed again, so a | |
| 147 | + // recursive pass has nothing further to observe. | |
| 148 | + if ($promises instanceof \Generator) { | |
| 149 | + return $results; | |
| 150 | + } | |
| 153 | 151 | foreach ($promises as $promise) { |
| 154 | 152 | if (\YoastSEO_Vendor\GuzzleHttp\Promise\Is::pending($promise)) { |
| 155 | 153 | return self::all($promises, $recursive); |
| 156 | 154 | } |
| @@ -172,16 +170,15 @@ | ||
| 172 | 170 | * of fulfilled promises is less than the desired $count. |
| 173 | 171 | * |
| 174 | 172 | * @param int $count Total number of promises. |
| 175 | 173 | * @param mixed $promises Promises or values. |
| 176 | - * | |
| 177 | - * @return PromiseInterface | |
| 178 | 174 | */ |
| 179 | - public static function some($count, $promises) | |
| 175 | + public static function some(int $count, $promises) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface | |
| 180 | 176 | { |
| 177 | + $promises = self::prepareIterable($promises, __FUNCTION__); | |
| 181 | 178 | $results = []; |
| 182 | 179 | $rejections = []; |
| 183 | - return \YoastSEO_Vendor\GuzzleHttp\Promise\Each::of($promises, function ($value, $idx, \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface $p) use(&$results, $count) { | |
| 180 | + return \YoastSEO_Vendor\GuzzleHttp\Promise\Each::of($promises, function ($value, $idx, \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface $p) use(&$results, $count) : void { | |
| 184 | 181 | if (\YoastSEO_Vendor\GuzzleHttp\Promise\Is::settled($p)) { |
| 185 | 182 | return; |
| 186 | 183 | } |
| 187 | 184 | $results[$idx] = $value; |
| @@ -187,9 +184,9 @@ | ||
| 187 | 184 | $results[$idx] = $value; |
| 188 | 185 | if (\count($results) >= $count) { |
| 189 | 186 | $p->resolve(null); |
| 190 | 187 | } |
| 191 | - }, function ($reason) use(&$rejections) { | |
| 188 | + }, function ($reason) use(&$rejections) : void { | |
| 192 | 189 | $rejections[] = $reason; |
| 193 | 190 | })->then(function () use(&$results, &$rejections, $count) { |
| 194 | 191 | if (\count($results) !== $count) { |
| 195 | 192 | throw new \YoastSEO_Vendor\GuzzleHttp\Promise\AggregateException('Not enough promises to fulfill count', $rejections); |
| @@ -202,13 +199,12 @@ | ||
| 202 | 199 | * Like some(), with 1 as count. However, if the promise fulfills, the |
| 203 | 200 | * fulfillment value is not an array of 1 but the value directly. |
| 204 | 201 | * |
| 205 | 202 | * @param mixed $promises Promises or values. |
| 206 | - * | |
| 207 | - * @return PromiseInterface | |
| 208 | 203 | */ |
| 209 | - public static function any($promises) | |
| 204 | + public static function any($promises) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface | |
| 210 | 205 | { |
| 206 | + $promises = self::prepareIterable($promises, __FUNCTION__); | |
| 211 | 207 | return self::some(1, $promises)->then(function ($values) { |
| 212 | 208 | return $values[0]; |
| 213 | 209 | }); |
| 214 | 210 | } |
| @@ -220,20 +216,34 @@ | ||
| 220 | 216 | * |
| 221 | 217 | * @see inspect for the inspection state array format. |
| 222 | 218 | * |
| 223 | 219 | * @param mixed $promises Promises or values. |
| 224 | - * | |
| 225 | - * @return PromiseInterface | |
| 226 | 220 | */ |
| 227 | - public static function settle($promises) | |
| 221 | + public static function settle($promises) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface | |
| 228 | 222 | { |
| 223 | + $promises = self::prepareIterable($promises, __FUNCTION__); | |
| 229 | 224 | $results = []; |
| 230 | - return \YoastSEO_Vendor\GuzzleHttp\Promise\Each::of($promises, function ($value, $idx) use(&$results) { | |
| 225 | + return \YoastSEO_Vendor\GuzzleHttp\Promise\Each::of($promises, function ($value, $idx) use(&$results) : void { | |
| 231 | 226 | $results[$idx] = ['state' => \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface::FULFILLED, 'value' => $value]; |
| 232 | - }, function ($reason, $idx) use(&$results) { | |
| 227 | + }, function ($reason, $idx) use(&$results) : void { | |
| 233 | 228 | $results[$idx] = ['state' => \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface::REJECTED, 'reason' => $reason]; |
| 234 | 229 | })->then(function () use(&$results) { |
| 235 | 230 | \ksort($results); |
| 236 | 231 | return $results; |
| 237 | 232 | }); |
| 233 | + } | |
| 234 | + private static function prepareIterable($promises, string $method) : iterable | |
| 235 | + { | |
| 236 | + if (\is_iterable($promises)) { | |
| 237 | + return $promises; | |
| 238 | + } | |
| 239 | + self::triggerNonIterableDeprecation($promises, $method); | |
| 240 | + return [$promises]; | |
| 241 | + } | |
| 242 | + private static function triggerNonIterableDeprecation($promises, string $method) : void | |
| 243 | + { | |
| 244 | + if (\is_iterable($promises)) { | |
| 245 | + return; | |
| 246 | + } | |
| 247 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/promises', '2.5', 'Passing a non-iterable to %s::%s() is deprecated; guzzlehttp/promises 3.0 will require an iterable.', self::class, $method); | |
| 238 | 248 | } |
| 239 | 249 | } |