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) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Add socket into selector list |
||
| 83 | * |
||
| 84 | * @param StreamResourceInterface $streamResource Resource object |
||
| 85 | * @param string $operation One of OperationInterface::OPERATION_* consts |
||
| 86 | * |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | 66 | public function addSocketOperation(StreamResourceInterface $streamResource, $operation) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Add array of socket with specified operation |
||
| 96 | * |
||
| 97 | * @param StreamResourceInterface[] $streamResources List of resources. Value depends on second argument. |
||
| 98 | * If string is provided, then it must be array of StreamResourceInterface. |
||
| 99 | * If $operation parameter is omitted then this argument must contain |
||
| 100 | * pairs [StreamResourceInterface, operation] for each element |
||
| 101 | * @param string $operation Operation, one of OperationInterface::OPERATION_* consts |
||
| 102 | * |
||
| 103 | * @return void |
||
| 104 | * @throws \InvalidArgumentException |
||
| 105 | */ |
||
| 106 | 8 | public function addSocketOperationArray(array $streamResources, $operation = null) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Remove given socket from select list |
||
| 125 | * |
||
| 126 | * @param StreamResourceInterface $streamResource Stream resource object |
||
| 127 | * @param string $operation One of OperationInterface::OPERATION_* consts |
||
| 128 | * |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | 59 | public function removeSocketOperation(StreamResourceInterface $streamResource, $operation) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Remove all previously defined operations on this socket and adds socket into list of given operation |
||
| 144 | * |
||
| 145 | * @param StreamResourceInterface $streamResource Stream resource object |
||
| 146 | * @param string $operation One of OperationInterface::OPERATION_* consts |
||
| 147 | * |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | 2 | public function changeSocketOperation(StreamResourceInterface $streamResource, $operation) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Return socket objects for operations |
||
| 159 | * |
||
| 160 | * @param string $operation One of OperationInterface::OPERATION_* consts |
||
| 161 | * |
||
| 162 | * @return resource[]|null List of socket resource |
||
| 163 | */ |
||
| 164 | 62 | private function getSocketsForOperation($operation) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Get socket objects by resources and remove them from work list |
||
| 181 | * |
||
| 182 | * @param resource[] $resources Stream resources |
||
| 183 | * @param string $operation One of OperationInterface::OPERATION_* consts |
||
| 184 | * |
||
| 185 | * @return StreamResourceInterface[] |
||
| 186 | */ |
||
| 187 | 52 | private function popSocketsByResources(array $resources, $operation) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Checks whether given socket can process I/O operation after stream_select return |
||
| 211 | * |
||
| 212 | * @param resource $stream Socket resource |
||
| 213 | * @param string $operation One of OperationInterface::OPERATION_* consts |
||
| 214 | * |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | 52 | private function isActuallyReadyForIo($stream, $operation) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Remove given socket from all operations |
||
| 231 | * |
||
| 232 | * @param StreamResourceInterface $streamResource Resource object |
||
| 233 | * |
||
| 234 | * @return void |
||
| 235 | */ |
||
| 236 | 35 | public function removeAllSocketOperations(StreamResourceInterface $streamResource) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Make stream_select call |
||
| 248 | * |
||
| 249 | * @param int $seconds Amount of seconds to wait |
||
| 250 | * @param int $usec Amount of microseconds to add to $seconds |
||
| 251 | * @param resource[] $read List of sockets to check for read. After function return it will be filled with |
||
| 252 | * sockets, which are ready to read |
||
| 253 | * @param resource[] $write List of sockets to check for write. After function return it will be filled with |
||
| 254 | * sockets, which are ready to write |
||
| 255 | * |
||
| 256 | * @return int Amount of sockets ready for I/O |
||
| 257 | * @throws SocketException |
||
| 258 | */ |
||
| 259 | 62 | private function doStreamSelect($seconds, $usec = null, array &$read = null, array &$write = null) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Calculate amount of attempts for select operation |
||
| 277 | * |
||
| 278 | * @param int|null $seconds Amount of seconds |
||
| 279 | * @param int|null $usec Amount of microseconds |
||
| 280 | * |
||
| 281 | * @return int |
||
| 282 | */ |
||
| 283 | 62 | private function calculateAttemptsCount($seconds, $usec) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Check whether given resource is server socket |
||
| 296 | * |
||
| 297 | * @param resource $resource Resource to test |
||
| 298 | * |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | 52 | private function isSocketServer($resource) |
|
| 306 | } |
||
| 307 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.