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) |
|
| 78 | 1 | ||
| 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 | public function addSocketOperation(StreamResourceInterface $streamResource, $operation) |
||
| 88 | { |
||
| 89 | 66 | $this->streamResources[$operation][spl_object_hash($streamResource)] = $streamResource; |
|
| 90 | } |
||
| 91 | 66 | ||
| 92 | 66 | /** |
|
| 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 | public function addSocketOperationArray(array $streamResources, $operation = null) |
||
| 105 | { |
||
| 106 | 8 | foreach ($streamResources as $streamResource) { |
|
| 107 | if ($operation !== null) { |
||
| 108 | 8 | $this->addSocketOperation($streamResource, $operation); |
|
| 109 | 8 | } else { |
|
| 110 | 2 | if (!is_array($streamResource) || count($streamResource) !== 2) { |
|
| 111 | 2 | throw new \InvalidArgumentException( |
|
| 112 | 6 | 'First parameter must contain pair (SocketInterface, operation)' |
|
| 113 | 2 | ); |
|
| 114 | } |
||
| 115 | 2 | ||
| 116 | $this->addSocketOperation(reset($streamResource), end($streamResource)); |
||
| 117 | } |
||
| 118 | 4 | } |
|
| 119 | } |
||
| 120 | 6 | ||
| 121 | 6 | /** |
|
| 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 | public function removeSocketOperation(StreamResourceInterface $streamResource, $operation) |
||
| 130 | { |
||
| 131 | 59 | $hash = spl_object_hash($streamResource); |
|
| 132 | if (isset($this->streamResources[$operation], $this->streamResources[$operation][$hash])) { |
||
| 133 | 59 | unset($this->streamResources[$operation][$hash]); |
|
| 134 | 59 | if (!$this->streamResources[$operation]) { |
|
| 135 | 59 | unset($this->streamResources[$operation]); |
|
| 136 | 59 | } |
|
| 137 | 59 | } |
|
| 138 | 59 | } |
|
| 139 | 59 | ||
| 140 | 59 | /** |
|
| 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 | public function changeSocketOperation(StreamResourceInterface $streamResource, $operation) |
||
| 149 | { |
||
| 150 | 2 | $this->removeAllSocketOperations($streamResource); |
|
| 151 | |||
| 152 | 2 | $this->addSocketOperation($streamResource, $operation); |
|
| 153 | } |
||
| 154 | 2 | ||
| 155 | 2 | /** |
|
| 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 | private function getSocketsForOperation($operation) |
||
| 163 | { |
||
| 164 | 62 | if (!isset($this->streamResources[$operation])) { |
|
| 165 | return null; |
||
| 166 | 62 | } |
|
| 167 | 62 | ||
| 168 | $result = []; |
||
| 169 | foreach ($this->streamResources[$operation] as $socket) { |
||
| 170 | 62 | /** @var StreamResourceInterface $socket */ |
|
| 171 | 62 | $result[] = $socket->getStreamResource(); |
|
| 172 | } |
||
| 173 | 62 | ||
| 174 | 62 | return $result ?: null; |
|
| 175 | } |
||
| 176 | 62 | ||
| 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 | private function popSocketsByResources(array $resources, $operation, $isOutOfBand) |
||
| 206 | 52 | ||
| 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 | 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 | 35 | * @param string $operation One of OperationInterface::OPERATION_* consts |
|
| 237 | * @param bool $isOutOfBand Is it OOB operation |
||
| 238 | 35 | * |
|
| 239 | 35 | * @return bool |
|
| 240 | */ |
||
| 241 | 35 | 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 | public function removeAllSocketOperations(StreamResourceInterface $streamResource) |
||
| 290 | |||
| 291 | 62 | /** |
|
| 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 | 52 | * |
|
| 302 | * @return int Amount of sockets ready for I/O |
||
| 303 | 52 | */ |
|
| 304 | 52 | 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 | 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 | private function isSocketServer($resource) |
||
| 356 | } |
||
| 357 |