Complex classes like Promise often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Promise, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Promise implements PromiseInterface |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var integer |
||
| 12 | */ |
||
| 13 | private $state = self::STATE_PENDING; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var mixed |
||
| 17 | */ |
||
| 18 | private $current; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | private $context = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var \Closure |
||
| 27 | */ |
||
| 28 | private $waitCallback; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var \Closure |
||
| 32 | */ |
||
| 33 | private $cancelCallback; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var boolean |
||
| 37 | */ |
||
| 38 | private $isStateTransformed; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Appends fulfillment and rejection handlers to the promise, and returns an immutable |
||
| 42 | * new promise resolving to the return value of the handler. |
||
| 43 | * |
||
| 44 | * @param \Closure $onFulfilled Callback to be called when promise state is fulfilled. |
||
| 45 | * @param \Closure $onRejected Callback to be called when promise state is rejected. |
||
| 46 | * @return Promise |
||
| 47 | */ |
||
| 48 | public function then($onFulfilled = null, $onRejected = null) |
||
| 49 | { |
||
| 50 | if ($this->state === self::STATE_PENDING) { |
||
| 51 | $q = new Promise(); |
||
| 52 | $this->context[] = [$q, $onFulfilled, $onRejected]; |
||
| 53 | |||
| 54 | return $q; |
||
| 55 | } |
||
| 56 | |||
| 57 | if ($this->state === self::STATE_FULFILLED) { |
||
| 58 | return $onFulfilled |
||
|
|
|||
| 59 | ? (new FulfilledPromise($this->current))->then($onFulfilled, null) |
||
| 60 | : new FulfilledPromise($this->current); |
||
| 61 | } |
||
| 62 | |||
| 63 | return $onRejected |
||
| 64 | ? (new RejectedPromise($this->current))->then(null, $onRejected) |
||
| 65 | : new RejectedPromise($this->current); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Set waiting callback. |
||
| 70 | * |
||
| 71 | * @param \Closure $callback Callback to be set. |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | public function setWaitCallback(\Closure $callback = null) |
||
| 75 | { |
||
| 76 | if (!($callback instanceof \Closure)) { |
||
| 77 | throw new \InvalidArgumentException( |
||
| 78 | sprintf("Parameter 1 of %s must be a valid callback.", __METHOD__) |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | $this->waitCallback = $callback; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Set cancellation callback. |
||
| 87 | * |
||
| 88 | * @param \Closure $callback Callback to be set. |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | public function setCancelCallback(\Closure $callback = null) |
||
| 92 | { |
||
| 93 | if (!($callback instanceof \Closure)) { |
||
| 94 | throw new \InvalidArgumentException( |
||
| 95 | sprintf("Parameter 1 of %s must be a valid callback.", __METHOD__) |
||
| 96 | ); |
||
| 97 | } |
||
| 98 | |||
| 99 | $this->cancelCallback = $callback; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Comparing current promise state to new state and current promise value |
||
| 104 | * to new value. |
||
| 105 | * |
||
| 106 | * @param mixed $value Value to be compare. |
||
| 107 | * @param integer $state New promise state to be compare. |
||
| 108 | * @return null|boolean |
||
| 109 | */ |
||
| 110 | private function validateState($value, $state) |
||
| 111 | { |
||
| 112 | if ($this->state !== self::STATE_PENDING) { |
||
| 113 | if ($value === $this->current && $state === $this->state) { |
||
| 114 | return null; |
||
| 115 | } |
||
| 116 | |||
| 117 | $prevStatus = $this->state; |
||
| 118 | $currentStatus = $state; |
||
| 119 | |||
| 120 | throw $this->state === $state |
||
| 121 | ? new \LogicException( |
||
| 122 | sprintf("State of the promise is already %s", $prevStatus == 4 ? 'fulfilled' : 'rejected') |
||
| 123 | ) |
||
| 124 | : new \LogicException( |
||
| 125 | sprintf( |
||
| 126 | "Unable to change %s promise to %s", |
||
| 127 | $prevStatus == 4 ? 'fulfilled' : 'rejected', |
||
| 128 | $currentStatus == 4 ? 'fulfilled' : 'rejected' |
||
| 129 | ) |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | |||
| 133 | if ($value === $this) { |
||
| 134 | throw new \LogicException( |
||
| 135 | "Unable to fulfill or reject a promise with itself." |
||
| 136 | ); |
||
| 137 | } |
||
| 138 | |||
| 139 | return true; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | public function resolve($value) |
||
| 146 | { |
||
| 147 | $this->isStateTransformed = $this->validateState($value, self::STATE_FULFILLED); |
||
| 148 | |||
| 149 | if ($this->isStateTransformed) { |
||
| 150 | $this->setState(self::STATE_FULFILLED); |
||
| 151 | $this->trigger($value); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | public function reject($reason) |
||
| 159 | { |
||
| 160 | $this->isStateTransformed = $this->validateState($reason, self::STATE_REJECTED); |
||
| 161 | |||
| 162 | if ($this->isStateTransformed) { |
||
| 163 | $this->setState(self::STATE_REJECTED); |
||
| 164 | $this->trigger($reason); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Synchronously forces promise to complete using wait method. |
||
| 170 | * |
||
| 171 | * @return mixed |
||
| 172 | */ |
||
| 173 | public function wait() |
||
| 174 | { |
||
| 175 | $this->waitInPendingState(); |
||
| 176 | |||
| 177 | $q = $this->current instanceof PromiseInterface |
||
| 178 | ? $this->current->wait() |
||
| 179 | : $this->current; |
||
| 180 | |||
| 181 | if ($this->current instanceof PromiseInterface || $this->currentState() === self::STATE_FULFILLED) { |
||
| 182 | return $q; |
||
| 183 | } else { |
||
| 184 | throw $q instanceof \Exception |
||
| 185 | ? $q |
||
| 186 | : new \Exception($q); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Cancel a promise that has not yet been fulfilled. |
||
| 192 | * |
||
| 193 | * @return mixed |
||
| 194 | */ |
||
| 195 | public function cancel() |
||
| 196 | { |
||
| 197 | if ($this->currentState() !== self::STATE_PENDING) { |
||
| 198 | return; |
||
| 199 | } |
||
| 200 | |||
| 201 | if ($this->cancelCallback) { |
||
| 202 | $cancelCallback = $this->cancelCallback; |
||
| 203 | $this->cancelCallback = null; |
||
| 204 | |||
| 205 | try { |
||
| 206 | $cancelCallback(); |
||
| 207 | } catch (\Exception $e) { |
||
| 208 | $this->reject($e); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | if ($this->currentState() === self::STATE_PENDING) { |
||
| 213 | $this->reject('Promise has been cancelled.'); |
||
| 214 | } |
||
| 215 | |||
| 216 | $e = $this->current instanceof PromiseInterface |
||
| 217 | ? $this->current->cancel() |
||
| 218 | : $this->current; |
||
| 219 | |||
| 220 | if ($this->current instanceof PromiseInterface || $this->currentState() === self::STATE_REJECTED) { |
||
| 221 | return $e; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | public function currentState() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Invoking promise handler based on current state and given value. |
||
| 235 | * |
||
| 236 | * @param mixed $value |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | private function trigger($value) |
||
| 240 | { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Invoking handler based on given context, index, and value. |
||
| 278 | * |
||
| 279 | * @param array $context |
||
| 280 | * @param integer $index |
||
| 281 | * @param mixed $value |
||
| 282 | * @return void |
||
| 283 | */ |
||
| 284 | private static function invokeContext($context, $index, $value) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Set promise state to given state. |
||
| 309 | * |
||
| 310 | * @param integer $state |
||
| 311 | * @return void |
||
| 312 | */ |
||
| 313 | private function setState($state) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Synchronously forces promise to wait when current promise state is pending. |
||
| 328 | * |
||
| 329 | * @return void |
||
| 330 | */ |
||
| 331 | private function waitInPendingState() |
||
| 357 | } |
||
| 358 |