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 |
||
| 7 | class Promise implements PromiseInterface |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var integer |
||
| 11 | */ |
||
| 12 | private $state = self::STATE_PENDING; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var mixed |
||
| 16 | */ |
||
| 17 | private $current; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private $context = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \Closure |
||
| 26 | */ |
||
| 27 | private $waitCallback; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \Closure |
||
| 31 | */ |
||
| 32 | private $cancelCallback; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var boolean |
||
| 36 | */ |
||
| 37 | private $isStateTransformed; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Appends fulfillment and rejection handlers to the promise, and returns an immutable |
||
| 41 | * new promise resolving to the return value of the handler. |
||
| 42 | * |
||
| 43 | * @param \Closure $onFulfilled Callback to be called when promise state is fulfilled. |
||
| 44 | * @param \Closure $onRejected Callback to be called when promise state is rejected. |
||
| 45 | * @return Promise |
||
| 46 | */ |
||
| 47 | public function then($onFulfilled = null, $onRejected = null) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Set waiting callback. |
||
| 69 | * |
||
| 70 | * @param \Closure $callback Callback to be set. |
||
| 71 | * @return void |
||
| 72 | */ |
||
| 73 | public function setWaitCallback(\Closure $callback = null) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Set cancellation callback. |
||
| 86 | * |
||
| 87 | * @param \Closure $callback Callback to be set. |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | public function setCancelCallback(\Closure $callback = null) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Comparing current promise state to new state and current promise value |
||
| 103 | * to new value. |
||
| 104 | * |
||
| 105 | * @param mixed $value Value to be compare. |
||
| 106 | * @param integer $state New promise state to be compare. |
||
| 107 | * @return null|boolean |
||
| 108 | */ |
||
| 109 | private function validateState($value, $state) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | public function resolve($value) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | */ |
||
| 157 | public function reject($reason) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Synchronously forces promise to complete using wait method. |
||
| 169 | * |
||
| 170 | * @return mixed |
||
| 171 | */ |
||
| 172 | public function wait() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Cancel a promise that has not yet been fulfilled. |
||
| 191 | * |
||
| 192 | * @return mixed |
||
| 193 | */ |
||
| 194 | public function cancel() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | public function currentState() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Invoking promise handler based on current state and given value. |
||
| 234 | * |
||
| 235 | * @param mixed $value |
||
| 236 | * @return void |
||
| 237 | */ |
||
| 238 | private function trigger($value) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Invoking handler based on given context, index, and value. |
||
| 277 | * |
||
| 278 | * @param array $context |
||
| 279 | * @param integer $index |
||
| 280 | * @param mixed $value |
||
| 281 | * @return void |
||
| 282 | */ |
||
| 283 | private static function invokeContext($context, $index, $value) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Set promise state to given state. |
||
| 308 | * |
||
| 309 | * @param integer $state |
||
| 310 | * @return void |
||
| 311 | */ |
||
| 312 | private function setState($state) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Synchronously forces promise to wait when current promise state is pending. |
||
| 327 | * |
||
| 328 | * @return void |
||
| 329 | */ |
||
| 330 | private function waitInPendingState() |
||
| 356 | } |
||
| 357 |