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 | * TCP connection counter. |
||
55 | * @var ConnectionCounter |
||
56 | */ |
||
57 | private $counter; |
||
58 | |||
59 | /** |
||
60 | * Constructor. |
||
61 | * Initialize cURL multi handle. |
||
62 | * @param CoOption $options |
||
63 | */ |
||
64 | 26 | public function __construct(CoOption $options) |
|
72 | |||
73 | /** |
||
74 | * Call curl_multi_add_handle() or push into queue. |
||
75 | * @param resource $ch |
||
76 | * @param Deferred $deferred |
||
77 | */ |
||
78 | 13 | public function addOrEnqueue($ch, Deferred $deferred = null) |
|
87 | |||
88 | /** |
||
89 | * Call curl_multi_add_handle(). |
||
90 | * @param resource $ch |
||
91 | * @param Deferred $deferred |
||
92 | */ |
||
93 | 13 | private function addCurl($ch, Deferred $deferred = null) |
|
107 | |||
108 | /** |
||
109 | * Push into queue. |
||
110 | * @param resource $ch |
||
111 | * @param Deferred $deferred |
||
112 | */ |
||
113 | 3 | private function enqueueCurl($ch, Deferred $deferred = null) |
|
118 | |||
119 | /** |
||
120 | * Add delay. |
||
121 | * @param int $time |
||
122 | * @param Deferred $deferred |
||
123 | */ |
||
124 | 6 | public function addDelay($time, Deferred $deferred) |
|
139 | |||
140 | /** |
||
141 | * Run curl_multi_exec() loop. |
||
142 | */ |
||
143 | 18 | public function wait() |
|
160 | |||
161 | /** |
||
162 | * Used for halting loop. |
||
163 | */ |
||
164 | 4 | public function reserveHaltException($e) |
|
168 | |||
169 | /** |
||
170 | * Sleep at least required. |
||
171 | */ |
||
172 | 9 | private function sleepUntilNearestTime() |
|
190 | |||
191 | /** |
||
192 | * Consume completed cURL handles and delays. |
||
193 | */ |
||
194 | 18 | private function consumeCurlsAndUntils() |
|
200 | |||
201 | /** |
||
202 | * Poll completed cURL entries and consume cURL queue. |
||
203 | * @return array |
||
204 | */ |
||
205 | 18 | private function consumeCurls() |
|
220 | |||
221 | /** |
||
222 | * Consume delay queue. |
||
223 | */ |
||
224 | 18 | private function consumeUntils() |
|
236 | |||
237 | /** |
||
238 | * Resolve polled cURLs. |
||
239 | * @param array $entries Polled cURL entries. |
||
240 | */ |
||
241 | 18 | private function resolveCurls($entries) |
|
254 | } |
||
255 |