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 | 26 | 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 | 13 | public function addOrEnqueue($ch, Deferred $deferred = null) |
|
86 | 13 | { |
|
87 | 13 | if (isset($this->added[(string)$ch]) || isset($this->queue[(string)$ch])) { |
|
88 | 3 | throw new \UnexpectedValueException("The cURL handle is already enqueued: $ch"); |
|
89 | } |
||
90 | 13 | $id = $this->options['group'] ? (string)curl_getinfo($ch, CURLINFO_PRIVATE) : ''; |
|
91 | 13 | $this->options['concurrency'] > 0 |
|
92 | 13 | && $this->connectionCount >= $this->options['concurrency'] |
|
93 | 13 | && ($id === '' || empty($this->destinations[$id])) |
|
94 | 3 | ? $this->enqueueCurl($ch, $deferred) |
|
95 | 13 | : $this->addCurl($ch, $deferred); |
|
96 | 13 | } |
|
97 | |||
98 | /** |
||
99 | * Call curl_multi_add_handle(). |
||
100 | * @param resource $ch |
||
101 | * @param Deferred $deferred |
||
102 | */ |
||
103 | 13 | private function addCurl($ch, Deferred $deferred = null) |
|
117 | |||
118 | /** |
||
119 | * Add destination info. |
||
120 | * @param resource $ch |
||
121 | */ |
||
122 | 13 | private function addDestination($ch) |
|
123 | 13 | { |
|
124 | 13 | $id = $this->options['group'] ? (string)curl_getinfo($ch, CURLINFO_PRIVATE) : ''; |
|
125 | 13 | if ($id === '') { |
|
126 | 12 | ++$this->connectionCount; |
|
127 | 12 | return; |
|
128 | } |
||
129 | 1 | if (empty($this->destinations[$id])) { |
|
130 | 1 | $this->destinations[$id] = 1; |
|
131 | 1 | ++$this->connectionCount; |
|
132 | 1 | return; |
|
133 | } |
||
134 | 1 | ++$this->destinations[$id]; |
|
135 | 1 | } |
|
136 | |||
137 | /** |
||
138 | * Remove destination info. |
||
139 | * @param resource $ch |
||
140 | */ |
||
141 | 10 | private function removeDestination($ch) |
|
142 | 10 | { |
|
143 | 10 | $id = $this->options['group'] ? (string)curl_getinfo($ch, CURLINFO_PRIVATE) : ''; |
|
144 | 10 | if ($id === '') { |
|
145 | 9 | --$this->connectionCount; |
|
146 | 9 | return; |
|
147 | } |
||
148 | 1 | if (empty($this->destinations[$id]) || $this->destinations[$id] === 1) { |
|
149 | 1 | unset($this->destinations[$id]); |
|
150 | 1 | --$this->connectionCount; |
|
151 | 1 | return; |
|
152 | } |
||
153 | 1 | --$this->destinations[$id]; |
|
154 | 1 | } |
|
155 | |||
156 | /** |
||
157 | * Push into queue. |
||
158 | * @param resource $ch |
||
159 | * @param Deferred $deferred |
||
160 | */ |
||
161 | 3 | private function enqueueCurl($ch, Deferred $deferred = null) |
|
166 | |||
167 | /** |
||
168 | * Add delay. |
||
169 | * @param int $time |
||
170 | * @param Deferred $deferred |
||
171 | */ |
||
172 | 6 | public function addDelay($time, Deferred $deferred) |
|
187 | |||
188 | /** |
||
189 | * Run curl_multi_exec() loop. |
||
190 | */ |
||
191 | 18 | public function wait() |
|
208 | |||
209 | /** |
||
210 | * Used for halting loop. |
||
211 | */ |
||
212 | 4 | public function reserveHaltException($e) |
|
216 | |||
217 | /** |
||
218 | * Sleep at least required. |
||
219 | */ |
||
220 | 9 | private function sleepUntilNearestTime() |
|
238 | |||
239 | /** |
||
240 | * Consume completed cURL handles and delays. |
||
241 | */ |
||
242 | 18 | private function consumeCurlsAndUntils() |
|
248 | |||
249 | /** |
||
250 | * Poll completed cURL entries and consume cURL queue. |
||
251 | * @return array |
||
252 | */ |
||
253 | 18 | private function consumeCurls() |
|
268 | |||
269 | /** |
||
270 | * Consume delay queue. |
||
271 | */ |
||
272 | 18 | private function consumeUntils() |
|
284 | |||
285 | /** |
||
286 | * Resolve polled cURLs. |
||
287 | * @param array $entries Polled cURL entries. |
||
288 | */ |
||
289 | 18 | private function resolveCurls($entries) |
|
302 | } |
||
303 |