Complex classes like CURLPool 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 CURLPool, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class CURLPool |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Options. |
||
| 13 | * @var CoOption |
||
| 14 | */ |
||
| 15 | private $options; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * cURL multi handle. |
||
| 19 | * @var resource |
||
| 20 | */ |
||
| 21 | private $mh; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * cURL handles those have not been dispatched. |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private $queue = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * cURL handles those have been already dispatched. |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | private $added = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Delays to be ended at. |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private $untils = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * React Deferreds. |
||
| 43 | * @var Deferred |
||
| 44 | */ |
||
| 45 | private $deferreds = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Used for halting loop. |
||
| 49 | * @var \RuntimeException |
||
| 50 | */ |
||
| 51 | private $haltException; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Constructor. |
||
| 55 | * Initialize cURL multi handle. |
||
| 56 | * @param CoOption $options |
||
| 57 | */ |
||
| 58 | 20 | public function __construct(CoOption $options) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Call curl_multi_add_handle() or push into queue. |
||
| 68 | * @param resource $ch |
||
| 69 | * @param Deferred $deferred |
||
| 70 | */ |
||
| 71 | 10 | public function addOrEnqueue($ch, Deferred $deferred = null) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Add delay. |
||
| 95 | * @param int $time |
||
| 96 | * @param Deferred $deferred |
||
| 97 | */ |
||
| 98 | 4 | public function addDelay($time, Deferred $deferred) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Run curl_multi_exec() loop. |
||
| 113 | */ |
||
| 114 | 14 | public function wait() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Used for halting loop. |
||
| 134 | */ |
||
| 135 | 3 | public function reserveHaltException($e) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Sleep at least required. |
||
| 142 | */ |
||
| 143 | 8 | private function sleepUntilNearestTime() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Consume completed cURL handles and delays. |
||
| 162 | */ |
||
| 163 | 14 | private function consumeCurlsAndUntils() |
|
| 203 | } |
||
| 204 |