Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Client extends AbstractClient implements ClientInterface |
||
27 | { |
||
28 | protected $socket; |
||
29 | protected $chunk_size; |
||
30 | protected $verify_peer_name; |
||
31 | |||
32 | public function __construct(array $config, ObjectSpec $objectSpec = null) |
||
33 | { |
||
34 | parent::__construct($config, $objectSpec); |
||
35 | |||
36 | if (!empty($config['chunk_size'])) { |
||
37 | $this->chunk_size = (int) $config['chunk_size']; |
||
38 | } else { |
||
39 | $this->chunk_size = 1024; |
||
40 | } |
||
41 | |||
42 | if (isset($config['verify_peer_name'])) { |
||
43 | $this->verify_peer_name = (bool) $config['verify_peer_name']; |
||
44 | } else { |
||
45 | $this->verify_peer_name = true; |
||
46 | } |
||
47 | |||
48 | if ($this->port === false) { |
||
49 | // if not set, default port is 700 |
||
50 | $this->port = 700; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | public function __destruct() |
||
58 | |||
59 | /** |
||
60 | * Setup context in case of ssl connection |
||
61 | * |
||
62 | * @return resource|null |
||
63 | */ |
||
64 | private function setupContext() |
||
97 | |||
98 | /** |
||
99 | * Setup connection socket |
||
100 | * |
||
101 | * @param resource|null $context SSL context or null in case of tcp connection |
||
102 | * |
||
103 | * @throws Exception |
||
104 | */ |
||
105 | private function setupSocket($context = null) |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | * |
||
133 | * @see \AfriCC\EPP\ClientInterface::connect() |
||
134 | */ |
||
135 | public function connect($newPassword = false) |
||
149 | |||
150 | /** |
||
151 | * Closes a previously opened EPP connection |
||
152 | */ |
||
153 | public function close() |
||
164 | |||
165 | protected function getFrame() |
||
181 | |||
182 | protected function sendFrame(FrameInterface $frame) |
||
189 | |||
190 | protected function log($message, $color = '0;32') |
||
197 | |||
198 | /** |
||
199 | * check if socket is still active |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | private function active() |
||
207 | |||
208 | /** |
||
209 | * receive socket data |
||
210 | * |
||
211 | * @param int $length |
||
212 | * |
||
213 | * @throws Exception |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | private function recv($length) |
||
256 | |||
257 | /** |
||
258 | * send data to socket |
||
259 | * |
||
260 | * @param string $buffer |
||
261 | */ |
||
262 | private function send($buffer) |
||
314 | } |
||
315 |