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 | 67 | public function select($seconds, $usec = null) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Add socket into selector list |
||
| 87 | * |
||
| 88 | * @param StreamResourceInterface $streamResource Resource object |
||
| 89 | 66 | * @param string $operation One of OperationInterface::OPERATION_* consts |
|
| 90 | * |
||
| 91 | 66 | * @return void |
|
| 92 | 66 | */ |
|
| 93 | public function addSocketOperation(StreamResourceInterface $streamResource, $operation) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Add array of socket with specified operation |
||
| 100 | * |
||
| 101 | * @param StreamResourceInterface[] $streamResources List of resources. Value depends on second argument. |
||
| 102 | * If string is provided, then it must be array of StreamResourceInterface. |
||
| 103 | * If $operation parameter is omitted then this argument must contain |
||
| 104 | * pairs [StreamResourceInterface, operation] for each element |
||
| 105 | * @param string $operation Operation, one of OperationInterface::OPERATION_* consts |
||
| 106 | 8 | * |
|
| 107 | * @return void |
||
| 108 | 8 | * @throws \InvalidArgumentException |
|
| 109 | 8 | */ |
|
| 110 | 2 | public function addSocketOperationArray(array $streamResources, $operation = null) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Remove given socket from select list |
||
| 129 | * |
||
| 130 | * @param StreamResourceInterface $streamResource Stream resource object |
||
| 131 | 59 | * @param string $operation One of OperationInterface::OPERATION_* consts |
|
| 132 | * |
||
| 133 | 59 | * @return void |
|
| 134 | 59 | */ |
|
| 135 | 59 | public function removeSocketOperation(StreamResourceInterface $streamResource, $operation) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Remove all previously defined operations on this socket and adds socket into list of given operation |
||
| 148 | * |
||
| 149 | * @param StreamResourceInterface $streamResource Stream resource object |
||
| 150 | 2 | * @param string $operation One of OperationInterface::OPERATION_* consts |
|
| 151 | * |
||
| 152 | 2 | * @return void |
|
| 153 | */ |
||
| 154 | 2 | public function changeSocketOperation(StreamResourceInterface $streamResource, $operation) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Return socket objects for operations |
||
| 163 | * |
||
| 164 | 62 | * @param string $operation One of OperationInterface::OPERATION_* consts |
|
| 165 | * |
||
| 166 | 62 | * @return resource[]|null List of socket resource |
|
| 167 | 62 | */ |
|
| 168 | private function getSocketsForOperation($operation) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get socket objects by resources and remove them from work list |
||
| 185 | * |
||
| 186 | * @param resource[] $resources Stream resources |
||
| 187 | 52 | * @param string $operation One of OperationInterface::OPERATION_* consts |
|
| 188 | * @param bool $isOutOfBand Is it OOB operation |
||
| 189 | 52 | * |
|
| 190 | 52 | * @return StreamResourceInterface[] |
|
| 191 | */ |
||
| 192 | private function popSocketsByResources(array $resources, $operation, $isOutOfBand) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Checks whether given socket can process I/O operation after stream_select return |
||
| 216 | * |
||
| 217 | 52 | * @param resource $stream Socket resource |
|
| 218 | * @param string $operation One of OperationInterface::OPERATION_* consts |
||
| 219 | 52 | * @param bool $isOutOfBand Is it OOB operation |
|
| 220 | 51 | * |
|
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | 27 | private function isActuallyReadyForIo($stream, $operation, $isOutOfBand) |
|
| 237 | |||
| 238 | 35 | /** |
|
| 239 | 35 | * Remove given socket from all operations |
|
| 240 | * |
||
| 241 | 35 | * @param StreamResourceInterface $streamResource Resource object |
|
| 242 | 35 | * |
|
| 243 | 35 | * @return void |
|
| 244 | 35 | */ |
|
| 245 | public function removeAllSocketOperations(StreamResourceInterface $streamResource) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Make stream_select call |
||
| 257 | * |
||
| 258 | * @param int $seconds Amount of seconds to wait |
||
| 259 | 62 | * @param int $usec Amount of microseconds to add to $seconds |
|
| 260 | * @param resource[] &$read List of sockets to check for read. After function return it will be filled with |
||
| 261 | 62 | * sockets, which are ready to read |
|
| 262 | 62 | * @param resource[] &$write List of sockets to check for write. After function return it will be filled with |
|
| 263 | 62 | * sockets, which are ready to write |
|
| 264 | 2 | * @param resource[] &$oob After call it will be filled with sockets having OOB data, input value is ignored |
|
| 265 | * |
||
| 266 | * @return int Amount of sockets ready for I/O |
||
| 267 | 60 | */ |
|
| 268 | 60 | private function doStreamSelect( |
|
| 288 | 21 | ||
| 289 | 21 | /** |
|
| 290 | * Calculate amount of attempts for select operation |
||
| 291 | 62 | * |
|
| 292 | * @param int|null $seconds Amount of seconds |
||
| 293 | * @param int|null $usec Amount of microseconds |
||
| 294 | * |
||
| 295 | * @return int |
||
| 296 | */ |
||
| 297 | private function calculateAttemptsCount($seconds, $usec) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Check whether given resource is server socket |
||
| 310 | * |
||
| 311 | * @param resource $resource Resource to test |
||
| 312 | * |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | private function isSocketServer($resource) |
||
| 320 | } |
||
| 321 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.