Complex classes like ConnectionHandler 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 ConnectionHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | final class ConnectionHandler implements ConnectionHandlerInterface |
||
| 19 | { |
||
| 20 | const READ_LENGTH = 4096; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | private $shutdown; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | private $closed; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var KernelInterface |
||
| 34 | */ |
||
| 35 | private $kernel; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var ConnectionInterface |
||
| 39 | */ |
||
| 40 | private $connection; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $requests; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $buffer; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var int |
||
| 54 | */ |
||
| 55 | private $bufferLength; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Constructor. |
||
| 59 | * |
||
| 60 | * @param KernelInterface $kernel The kernel to use to handle requests |
||
| 61 | * @param ConnectionInterface $connection The connection to handle |
||
| 62 | */ |
||
| 63 | public function __construct(KernelInterface $kernel, ConnectionInterface $connection) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | public function ready(): array |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function shutdown(): void |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | public function close(): void |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | public function isClosed(): bool |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | /** |
||
| 145 | * Read a record from the connection. |
||
| 146 | * |
||
| 147 | * @return array The record that has been read |
||
| 148 | */ |
||
| 149 | private function readRecord(): array |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Process a record. |
||
| 180 | * |
||
| 181 | * @param array $record The record that has been read |
||
| 182 | * |
||
| 183 | * @return null|int Number of dispatched requests |
||
| 184 | * |
||
| 185 | * @throws ProtocolException If the client sends an unexpected record. |
||
| 186 | */ |
||
| 187 | private function processRecord(array $record): ?int |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Process a FCGI_BEGIN_REQUEST record. |
||
| 219 | * |
||
| 220 | * @param int $requestId The request id sending the record |
||
| 221 | * @param null|string $contentData The content of the record |
||
| 222 | * |
||
| 223 | * @throws ProtocolException If the FCGI_BEGIN_REQUEST record is unexpected |
||
| 224 | */ |
||
| 225 | private function processBeginRequestRecord(int $requestId, ?string $contentData): void |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Read a FastCGI name-value pair from a buffer and add it to the request params. |
||
| 258 | * |
||
| 259 | * @param int $requestId The request id that sent the name-value pair |
||
| 260 | * @param null|string $buffer The buffer to read the pair from (pass by reference) |
||
| 261 | */ |
||
| 262 | private function readNameValuePair(int $requestId, ?string &$buffer): void |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Read the field length of a FastCGI name-value pair from a buffer. |
||
| 280 | * |
||
| 281 | * @param string $buffer The buffer to read the field length from (pass by reference) |
||
| 282 | * |
||
| 283 | * @return int The length of the field |
||
| 284 | */ |
||
| 285 | private function readFieldLength(string &$buffer): int |
||
| 302 | |||
| 303 | /** |
||
| 304 | * End the request by writing an FCGI_END_REQUEST record and then removing |
||
| 305 | * the request from memory and closing the connection if necessary. |
||
| 306 | * |
||
| 307 | * @param int $requestId The request id to end |
||
| 308 | * @param int $appStatus The application status to declare |
||
| 309 | * @param int $protocolStatus The protocol status to declare |
||
| 310 | */ |
||
| 311 | private function endRequest(int $requestId, int $appStatus = 0, int $protocolStatus = DaemonInterface::FCGI_REQUEST_COMPLETE): void |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Write a record to the connection. |
||
| 329 | * |
||
| 330 | * @param int $requestId The request id to write to |
||
| 331 | * @param int $type The type of record |
||
| 332 | * @param string $content The content of the record |
||
| 333 | */ |
||
| 334 | private function writeRecord(int $requestId, int $type, string $content = null): void |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Write a response to the connection as FCGI_STDOUT records. |
||
| 349 | * |
||
| 350 | * @param int $requestId The request id to write to |
||
| 351 | * @param string $headerData The header -data to write (including terminating CRLFCRLF) |
||
| 352 | * @param resource $stream The stream to write |
||
| 353 | */ |
||
| 354 | private function writeResponse(int $requestId, string $headerData, $stream): void |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Dispatches a request to the kernel. |
||
| 382 | * |
||
| 383 | * @param int $requestId The request id that is ready to dispatch |
||
| 384 | * |
||
| 385 | * @return int status code from the response. |
||
| 386 | * |
||
| 387 | * @throws \LogicException If the kernel doesn't return a valid response |
||
| 388 | */ |
||
| 389 | private function dispatchRequest(int $requestId): int |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Sends the response to the client. |
||
| 418 | * |
||
| 419 | * @param int $requestId The request id to respond to |
||
| 420 | * @param ResponseInterface $response The PSR-7 HTTP response message |
||
| 421 | */ |
||
| 422 | private function sendResponse(int $requestId, ResponseInterface $response): void |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Send a HttpFoundation response to the client. |
||
| 440 | * |
||
| 441 | * @param int $requestId The request id to respond to |
||
| 442 | * @param HttpFoundationResponse $response The HTTP foundation response message |
||
| 443 | */ |
||
| 444 | private function sendHttpFoundationResponse(int $requestId, HttpFoundationResponse $response): void |
||
| 456 | } |
||
| 457 |