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 | 19 | 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 | 9 | 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) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Run curl_multi_exec() loop. |
||
| 122 | */ |
||
| 123 | 13 | public function wait() |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Used for halting loop. |
||
| 146 | */ |
||
| 147 | 3 | public function reserveHaltException($e) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Sleep at least required. |
||
| 154 | */ |
||
| 155 | 8 | private function sleepUntilNearestTime() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Consume completed cURL handles and delays. |
||
| 175 | */ |
||
| 176 | 13 | private function consumeCurlsAndUntils() |
|
| 214 | } |
||
| 215 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: