Complex classes like AsyncSelector 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 AsyncSelector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class AsyncSelector |
||
21 | { |
||
22 | /** |
||
23 | * Delay in microseconds between select attempts, if previous stream_select returned incorrect result |
||
24 | * @link https://bugs.php.net/bug.php?id=65137 |
||
25 | */ |
||
26 | const ATTEMPT_DELAY = 250000; |
||
27 | |||
28 | /** |
||
29 | * Attempt count to use when time out is not set |
||
30 | */ |
||
31 | const ATTEMPT_COUNT_FOR_INFINITE_TIMEOUT = 10; |
||
32 | |||
33 | /** |
||
34 | * Array of resources indexed by operation |
||
35 | * |
||
36 | * @var StreamResourceInterface[][] |
||
37 | */ |
||
38 | private $streamResources = []; |
||
39 | |||
40 | /** |
||
41 | * Wait socket resources for network operation |
||
42 | * |
||
43 | * @param int $seconds Number of seconds to wait |
||
44 | * @param int $usec Number of microseconds to add |
||
45 | * |
||
46 | * @return SelectContext |
||
47 | * @throws TimeoutException If operation was interrupted during timeout |
||
48 | * @throws SocketException If network operation failed |
||
49 | * @throws \InvalidArgumentException If there is no socket in the list |
||
50 | */ |
||
51 | 71 | public function select($seconds, $usec = null) |
|
78 | |||
79 | /** |
||
80 | * Add socket into selector list |
||
81 | * |
||
82 | * @param StreamResourceInterface $streamResource Resource object |
||
83 | * @param string $operation One of OperationInterface::OPERATION_* consts |
||
84 | * |
||
85 | * @return void |
||
86 | */ |
||
87 | 70 | public function addSocketOperation(StreamResourceInterface $streamResource, $operation) |
|
91 | |||
92 | /** |
||
93 | * Add array of socket with specified operation |
||
94 | * |
||
95 | * @param StreamResourceInterface[] $streamResources List of resources. Value depends on second argument. |
||
96 | * If string is provided, then it must be array of StreamResourceInterface. |
||
97 | * If $operation parameter is omitted then this argument must contain |
||
98 | * pairs [StreamResourceInterface, operation] for each element |
||
99 | * @param string $operation Operation, one of OperationInterface::OPERATION_* consts |
||
100 | * |
||
101 | * @return void |
||
102 | * @throws \InvalidArgumentException |
||
103 | */ |
||
104 | 8 | public function addSocketOperationArray(array $streamResources, $operation = null) |
|
120 | |||
121 | /** |
||
122 | * Remove given socket from select list |
||
123 | * |
||
124 | * @param StreamResourceInterface $streamResource Stream resource object |
||
125 | * @param string $operation One of OperationInterface::OPERATION_* consts |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | 39 | public function removeSocketOperation(StreamResourceInterface $streamResource, $operation) |
|
139 | |||
140 | /** |
||
141 | * Remove all previously defined operations on this socket and adds socket into list of given operation |
||
142 | * |
||
143 | * @param StreamResourceInterface $streamResource Stream resource object |
||
144 | * @param string $operation One of OperationInterface::OPERATION_* consts |
||
145 | * |
||
146 | * @return void |
||
147 | */ |
||
148 | 2 | public function changeSocketOperation(StreamResourceInterface $streamResource, $operation) |
|
154 | |||
155 | /** |
||
156 | * Return socket objects for operations |
||
157 | * |
||
158 | * @param string $operation One of OperationInterface::OPERATION_* consts |
||
159 | * |
||
160 | * @return resource[]|null List of socket resource |
||
161 | */ |
||
162 | 66 | private function getSocketsForOperation($operation) |
|
176 | |||
177 | /** |
||
178 | * Get socket objects by resources and remove them from work list |
||
179 | * |
||
180 | * @param resource[] $resources Stream resources |
||
181 | * @param string $operation One of OperationInterface::OPERATION_* consts |
||
182 | * @param bool $isOutOfBand Is it OOB operation |
||
183 | * |
||
184 | * @return StreamResourceInterface[] |
||
185 | */ |
||
186 | 56 | private function popSocketsByResources(array $resources, $operation, $isOutOfBand) |
|
206 | |||
207 | /** |
||
208 | * Extract context from given lists of resources |
||
209 | * |
||
210 | * @param resource[] $read Read-ready resources |
||
211 | * @param resource[] $write Write-ready resources |
||
212 | * @param resource[] $oob Oob-ready resources |
||
213 | * |
||
214 | * @return SelectContext|null |
||
215 | */ |
||
216 | 56 | private function extractContext(array $read, array $write, array $oob) |
|
231 | |||
232 | /** |
||
233 | * Checks whether given socket can process I/O operation after stream_select return |
||
234 | * |
||
235 | * @param resource $stream Socket resource |
||
236 | * @param string $operation One of OperationInterface::OPERATION_* consts |
||
237 | * @param bool $isOutOfBand Is it OOB operation |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | 56 | private function isActuallyReadyForIo($stream, $operation, $isOutOfBand) |
|
273 | |||
274 | /** |
||
275 | * Remove given socket from all operations |
||
276 | * |
||
277 | * @param StreamResourceInterface $streamResource Resource object |
||
278 | * |
||
279 | * @return void |
||
280 | */ |
||
281 | 37 | public function removeAllSocketOperations(StreamResourceInterface $streamResource) |
|
290 | |||
291 | /** |
||
292 | * Make stream_select call |
||
293 | * |
||
294 | * @param int $seconds Amount of seconds to wait |
||
295 | * @param int $usec Amount of microseconds to add to $seconds |
||
296 | * @param resource[] &$read List of sockets to check for read. After function return it will be filled with |
||
297 | * sockets, which are ready to read |
||
298 | * @param resource[] &$write List of sockets to check for write. After function return it will be filled with |
||
299 | * sockets, which are ready to write |
||
300 | * @param resource[] &$oob After call it will be filled with sockets having OOB data, input value is ignored |
||
301 | * |
||
302 | * @return int Amount of sockets ready for I/O |
||
303 | */ |
||
304 | 66 | private function doStreamSelect( |
|
324 | |||
325 | /** |
||
326 | * Calculate amount of attempts for select operation |
||
327 | * |
||
328 | * @param int|null $seconds Amount of seconds |
||
329 | * @param int|null $usec Amount of microseconds |
||
330 | * |
||
331 | * @return int |
||
332 | */ |
||
333 | 66 | private function calculateAttemptsCount($seconds, $usec) |
|
343 | |||
344 | /** |
||
345 | * Check whether given resource is server socket |
||
346 | * |
||
347 | * @param resource $resource Resource to test |
||
348 | * |
||
349 | * @return bool |
||
350 | */ |
||
351 | 56 | private function isSocketServer($resource) |
|
356 | } |
||
357 |