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 | 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() |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function shutdown() |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function close() |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function isClosed() |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | /** |
||
145 | * Read a record from the connection. |
||
146 | * |
||
147 | * @return array|null The record that has been read |
||
148 | */ |
||
149 | private function readRecord() |
||
177 | |||
178 | /** |
||
179 | * Process a record. |
||
180 | * |
||
181 | * @param array $record The record that has been read |
||
182 | * |
||
183 | * @return int Number of dispatched requests |
||
184 | * |
||
185 | * @throws ProtocolException If the client sends an unexpected record. |
||
186 | */ |
||
187 | private function processRecord(array $record) |
||
216 | |||
217 | /** |
||
218 | * Process a FCGI_BEGIN_REQUEST record. |
||
219 | * |
||
220 | * @param int $requestId The request id sending the record |
||
221 | * @param string $contentData The content of the record |
||
222 | * |
||
223 | * @throws ProtocolException If the FCGI_BEGIN_REQUEST record is unexpected |
||
224 | */ |
||
225 | private function processBeginRequestRecord($requestId, $contentData) |
||
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 string $buffer The buffer to read the pair from (pass by reference) |
||
261 | */ |
||
262 | private function readNameValuePair($requestId, &$buffer) |
||
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(&$buffer) |
||
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($requestId, $appStatus = 0, $protocolStatus = DaemonInterface::FCGI_REQUEST_COMPLETE) |
||
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($requestId, $type, $content = null) |
||
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 StreamInterface $stream The stream to write |
||
353 | */ |
||
354 | private function writeResponse($requestId, $headerData, StreamInterface $stream) |
||
379 | |||
380 | /** |
||
381 | * Dispatches a request to the kernel. |
||
382 | * |
||
383 | * @param int $requestId The request id that is ready to dispatch |
||
384 | * |
||
385 | * @throws \LogicException If the kernel doesn't return a valid response |
||
386 | */ |
||
387 | private function dispatchRequest($requestId) |
||
409 | |||
410 | /** |
||
411 | * Sends the response to the client. |
||
412 | * |
||
413 | * @param int $requestId The request id to respond to |
||
414 | * @param ResponseInterface $response The PSR-7 HTTP response message |
||
415 | */ |
||
416 | private function sendResponse($requestId, ResponseInterface $response) |
||
431 | |||
432 | /** |
||
433 | * Send a HttpFoundation response to the client. |
||
434 | * |
||
435 | * @param int $requestId The request id to respond to |
||
436 | * @param HttpFoundationResponse $response The HTTP foundation response message |
||
437 | */ |
||
438 | private function sendHttpFoundationResponse($requestId, HttpFoundationResponse $response) |
||
451 | } |
||
452 |