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 | * The number of whole running TCP connections. |
||
37 | * @var array |
||
38 | */ |
||
39 | private $connectionCount = 0; |
||
40 | |||
41 | /** |
||
42 | * Counts per destination. |
||
43 | * key => identifier |
||
44 | * value => count |
||
45 | * @var array |
||
46 | */ |
||
47 | private $destinations = []; |
||
48 | |||
49 | /** |
||
50 | * Delays to be ended at. |
||
51 | * @var array |
||
52 | */ |
||
53 | private $untils = []; |
||
54 | |||
55 | /** |
||
56 | * React Deferreds. |
||
57 | * @var Deferred |
||
58 | */ |
||
59 | private $deferreds = []; |
||
60 | |||
61 | /** |
||
62 | * Used for halting loop. |
||
63 | * @var \RuntimeException |
||
64 | */ |
||
65 | private $haltException; |
||
66 | |||
67 | /** |
||
68 | * Constructor. |
||
69 | * Initialize cURL multi handle. |
||
70 | * @param CoOption $options |
||
71 | */ |
||
72 | 22 | public function __construct(CoOption $options) |
|
79 | |||
80 | /** |
||
81 | * Call curl_multi_add_handle() or push into queue. |
||
82 | * @param resource $ch |
||
83 | * @param Deferred $deferred |
||
84 | */ |
||
85 | 12 | public function addOrEnqueue($ch, Deferred $deferred = null) |
|
97 | |||
98 | /** |
||
99 | * Call curl_multi_add_handle(). |
||
100 | * @param resource $ch |
||
101 | * @param Deferred $deferred |
||
102 | */ |
||
103 | 12 | private function addCurl($ch, Deferred $deferred = null) |
|
117 | |||
118 | /** |
||
119 | * Add destination info. |
||
120 | * @param resource $ch |
||
121 | */ |
||
122 | 12 | private function addDestination($ch) |
|
136 | |||
137 | /** |
||
138 | * Remove destination info. |
||
139 | * @param resource $ch |
||
140 | */ |
||
141 | 9 | private function removeDestination($ch) |
|
155 | |||
156 | /** |
||
157 | * Push into queue. |
||
158 | * @param resource $ch |
||
159 | * @param Deferred $deferred |
||
160 | */ |
||
161 | 2 | private function enqueueCurl($ch, Deferred $deferred = null) |
|
166 | |||
167 | /** |
||
168 | * Add delay. |
||
169 | * @param int $time |
||
170 | * @param Deferred $deferred |
||
171 | */ |
||
172 | 4 | public function addDelay($time, Deferred $deferred) |
|
184 | |||
185 | /** |
||
186 | * Run curl_multi_exec() loop. |
||
187 | */ |
||
188 | 16 | public function wait() |
|
205 | |||
206 | /** |
||
207 | * Used for halting loop. |
||
208 | */ |
||
209 | 3 | public function reserveHaltException($e) |
|
213 | |||
214 | /** |
||
215 | * Sleep at least required. |
||
216 | */ |
||
217 | 8 | private function sleepUntilNearestTime() |
|
235 | |||
236 | /** |
||
237 | * Consume completed cURL handles and delays. |
||
238 | */ |
||
239 | 16 | private function consumeCurlsAndUntils() |
|
245 | |||
246 | /** |
||
247 | * Poll completed cURL entries and consume cURL queue. |
||
248 | * @return array |
||
249 | */ |
||
250 | 16 | private function consumeCurls() |
|
265 | |||
266 | /** |
||
267 | * Consume delay queue. |
||
268 | */ |
||
269 | 16 | private function consumeUntils() |
|
281 | |||
282 | /** |
||
283 | * Resolve polled cURLs. |
||
284 | * @param array $entries Polled cURL entries. |
||
285 | */ |
||
286 | 16 | private function resolveCurls($entries) |
|
299 | } |
||
300 |