← All changes
|
includes/sdk/s3/GuzzleHttp/Promise/EachPromise.php
+17
-21
1.2.10
→
1.4.2
View file →
| @@ -1,11 +1,14 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Promise; |
| 4 | 5 | |
| 5 | 6 | /** |
| 6 | 7 | * Represents a promise that iterates over many promises and invokes |
| 7 | 8 | * side-effect functions in the process. |
| 9 | + * | |
| 10 | + * @final | |
| 8 | 11 | */ |
| 9 | 12 | class EachPromise implements PromisorInterface |
| 10 | 13 | { |
| 11 | 14 | private $pending = []; |
| @@ -56,9 +59,9 @@ | ||
| 56 | 59 | $this->onRejected = $config['rejected']; |
| 57 | 60 | } |
| 58 | 61 | } |
| 59 | 62 | /** @psalm-suppress InvalidNullableReturnType */ |
| 60 | - public function promise() | |
| 63 | + public function promise() : PromiseInterface | |
| 61 | 64 | { |
| 62 | 65 | if ($this->aggregate) { |
| 63 | 66 | return $this->aggregate; |
| 64 | 67 | } |
| @@ -68,21 +71,18 @@ | ||
| 68 | 71 | $this->iterable->rewind(); |
| 69 | 72 | $this->refillPending(); |
| 70 | 73 | } catch (\Throwable $e) { |
| 71 | 74 | $this->aggregate->reject($e); |
| 72 | - } catch (\Exception $e) { | |
| 73 | - $this->aggregate->reject($e); | |
| 74 | 75 | } |
| 75 | 76 | /** |
| 76 | 77 | * @psalm-suppress NullableReturnStatement |
| 77 | - * @phpstan-ignore-next-line | |
| 78 | 78 | */ |
| 79 | 79 | return $this->aggregate; |
| 80 | 80 | } |
| 81 | - private function createPromise() | |
| 81 | + private function createPromise() : void | |
| 82 | 82 | { |
| 83 | 83 | $this->mutex = \false; |
| 84 | - $this->aggregate = new Promise(function () { | |
| 84 | + $this->aggregate = new Promise(function () : void { | |
| 85 | 85 | if ($this->checkIfFinished()) { |
| 86 | 86 | return; |
| 87 | 87 | } |
| 88 | 88 | \reset($this->pending); |
| @@ -96,9 +96,9 @@ | ||
| 96 | 96 | } |
| 97 | 97 | } |
| 98 | 98 | }); |
| 99 | 99 | // Clear the references when the promise is resolved. |
| 100 | - $clearFn = function () { | |
| 100 | + $clearFn = function () : void { | |
| 101 | 101 | $this->iterable = $this->concurrency = $this->pending = null; |
| 102 | 102 | $this->onFulfilled = $this->onRejected = null; |
| 103 | 103 | $this->nextPendingIndex = 0; |
| 104 | 104 | }; |
| @@ -103,9 +103,9 @@ | ||
| 103 | 103 | $this->nextPendingIndex = 0; |
| 104 | 104 | }; |
| 105 | 105 | $this->aggregate->then($clearFn, $clearFn); |
| 106 | 106 | } |
| 107 | - private function refillPending() | |
| 107 | + private function refillPending() : void | |
| 108 | 108 | { |
| 109 | 109 | if (!$this->concurrency) { |
| 110 | 110 | // Add all pending promises. |
| 111 | 111 | while ($this->addPending() && $this->advanceIterator()) { |
| @@ -112,9 +112,9 @@ | ||
| 112 | 112 | } |
| 113 | 113 | return; |
| 114 | 114 | } |
| 115 | 115 | // Add only up to N pending promises. |
| 116 | - $concurrency = \is_callable($this->concurrency) ? \call_user_func($this->concurrency, \count($this->pending)) : $this->concurrency; | |
| 116 | + $concurrency = \is_callable($this->concurrency) ? ($this->concurrency)(\count($this->pending)) : $this->concurrency; | |
| 117 | 117 | $concurrency = \max($concurrency - \count($this->pending), 0); |
| 118 | 118 | // Concurrency may be set to 0 to disallow new promises. |
| 119 | 119 | if (!$concurrency) { |
| 120 | 120 | return; |
| @@ -127,9 +127,9 @@ | ||
| 127 | 127 | // next value to yield until promise callbacks are called. |
| 128 | 128 | while (--$concurrency && $this->advanceIterator() && $this->addPending()) { |
| 129 | 129 | } |
| 130 | 130 | } |
| 131 | - private function addPending() | |
| 131 | + private function addPending() : bool | |
| 132 | 132 | { |
| 133 | 133 | if (!$this->iterable || !$this->iterable->valid()) { |
| 134 | 134 | return \false; |
| 135 | 135 | } |
| @@ -137,22 +137,22 @@ | ||
| 137 | 137 | $key = $this->iterable->key(); |
| 138 | 138 | // Iterable keys may not be unique, so we use a counter to |
| 139 | 139 | // guarantee uniqueness |
| 140 | 140 | $idx = $this->nextPendingIndex++; |
| 141 | - $this->pending[$idx] = $promise->then(function ($value) use($idx, $key) { | |
| 141 | + $this->pending[$idx] = $promise->then(function ($value) use($idx, $key) : void { | |
| 142 | 142 | if ($this->onFulfilled) { |
| 143 | - \call_user_func($this->onFulfilled, $value, $key, $this->aggregate); | |
| 143 | + ($this->onFulfilled)($value, $key, $this->aggregate); | |
| 144 | 144 | } |
| 145 | 145 | $this->step($idx); |
| 146 | - }, function ($reason) use($idx, $key) { | |
| 146 | + }, function ($reason) use($idx, $key) : void { | |
| 147 | 147 | if ($this->onRejected) { |
| 148 | - \call_user_func($this->onRejected, $reason, $key, $this->aggregate); | |
| 148 | + ($this->onRejected)($reason, $key, $this->aggregate); | |
| 149 | 149 | } |
| 150 | 150 | $this->step($idx); |
| 151 | 151 | }); |
| 152 | 152 | return \true; |
| 153 | 153 | } |
| 154 | - private function advanceIterator() | |
| 154 | + private function advanceIterator() : bool | |
| 155 | 155 | { |
| 156 | 156 | // Place a lock on the iterator so that we ensure to not recurse, |
| 157 | 157 | // preventing fatal generator errors. |
| 158 | 158 | if ($this->mutex) { |
| @@ -166,15 +166,11 @@ | ||
| 166 | 166 | } catch (\Throwable $e) { |
| 167 | 167 | $this->aggregate->reject($e); |
| 168 | 168 | $this->mutex = \false; |
| 169 | 169 | return \false; |
| 170 | - } catch (\Exception $e) { | |
| 171 | - $this->aggregate->reject($e); | |
| 172 | - $this->mutex = \false; | |
| 173 | - return \false; | |
| 174 | 170 | } |
| 175 | 171 | } |
| 176 | - private function step($idx) | |
| 172 | + private function step(int $idx) : void | |
| 177 | 173 | { |
| 178 | 174 | // If the promise was already resolved, then ignore this step. |
| 179 | 175 | if (Is::settled($this->aggregate)) { |
| 180 | 176 | return; |
| @@ -187,9 +183,9 @@ | ||
| 187 | 183 | // Add more pending promises if possible. |
| 188 | 184 | $this->refillPending(); |
| 189 | 185 | } |
| 190 | 186 | } |
| 191 | - private function checkIfFinished() | |
| 187 | + private function checkIfFinished() : bool | |
| 192 | 188 | { |
| 193 | 189 | if (!$this->pending && !$this->iterable->valid()) { |
| 194 | 190 | // Resolve the promise if there's nothing left to do. |
| 195 | 191 | $this->aggregate->resolve(null); |