Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Connection 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 Connection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | abstract class Connection extends IOStream |
||
18 | { |
||
19 | /** |
||
20 | * @var string Path |
||
21 | */ |
||
22 | protected $path; |
||
23 | |||
24 | /** |
||
25 | * @var string Hostname |
||
26 | */ |
||
27 | protected $host; |
||
28 | |||
29 | /** |
||
30 | * @var string Real host |
||
31 | */ |
||
32 | protected $hostReal; |
||
33 | |||
34 | /** |
||
35 | * @var integer Port number |
||
36 | */ |
||
37 | protected $port; |
||
38 | |||
39 | /** |
||
40 | * @var string User name |
||
41 | */ |
||
42 | protected $user; |
||
43 | |||
44 | /** |
||
45 | * @var string Password |
||
46 | */ |
||
47 | protected $password; |
||
48 | |||
49 | /** |
||
50 | * @var string Address |
||
51 | */ |
||
52 | protected $addr; |
||
53 | |||
54 | /** |
||
55 | * @var object Stack of callbacks called when connection is established |
||
56 | */ |
||
57 | protected $onConnected = null; |
||
58 | |||
59 | /** |
||
60 | * @var boolean Connected? |
||
61 | */ |
||
62 | protected $connected = false; |
||
63 | |||
64 | /** |
||
65 | * @var boolean Failed? |
||
66 | */ |
||
67 | protected $failed = false; |
||
68 | |||
69 | /** |
||
70 | * @var integer Timeout |
||
71 | */ |
||
72 | protected $timeout = 120; |
||
73 | |||
74 | /** |
||
75 | * @var string Local address |
||
76 | */ |
||
77 | protected $locAddr; |
||
78 | |||
79 | /** |
||
80 | * @var integer Local port |
||
81 | */ |
||
82 | protected $locPort; |
||
83 | |||
84 | /** |
||
85 | * @var boolean Keepalive? |
||
86 | */ |
||
87 | protected $keepalive = false; |
||
88 | |||
89 | /** |
||
90 | * @var string Type |
||
91 | */ |
||
92 | protected $type; |
||
93 | |||
94 | /** |
||
95 | * @var Generic Parent socket |
||
96 | */ |
||
97 | protected $parentSocket; |
||
98 | |||
99 | /** |
||
100 | * @var boolean Dgram connection? |
||
101 | */ |
||
102 | protected $dgram = false; |
||
103 | |||
104 | /** |
||
105 | * @var boolean Enable bevConnect? |
||
106 | */ |
||
107 | protected $bevConnectEnabled = true; |
||
108 | |||
109 | /** |
||
110 | * @var array URI information |
||
111 | */ |
||
112 | protected $uri; |
||
113 | |||
114 | /** |
||
115 | * @var string Scheme |
||
116 | */ |
||
117 | protected $scheme; |
||
118 | |||
119 | /** |
||
120 | * @var string Private key file |
||
121 | */ |
||
122 | protected $pkfile; |
||
123 | |||
124 | /** |
||
125 | * @var string Certificate file |
||
126 | */ |
||
127 | protected $certfile; |
||
128 | |||
129 | /** |
||
130 | * @var string Passphrase |
||
131 | */ |
||
132 | protected $passphrase; |
||
133 | |||
134 | /** |
||
135 | * @var boolean Verify peer? |
||
136 | */ |
||
137 | protected $verifypeer = false; |
||
138 | |||
139 | /** |
||
140 | * @var boolean Allow self-signed? |
||
141 | */ |
||
142 | protected $allowselfsigned = true; |
||
143 | |||
144 | /** |
||
145 | * @var CappedStorage Context cache |
||
146 | */ |
||
147 | protected static $contextCache; |
||
148 | |||
149 | /** |
||
150 | * @var number Context cache size |
||
151 | */ |
||
152 | protected static $contextCacheSize = 64; |
||
153 | |||
154 | /** |
||
155 | * Connected? |
||
156 | * @return boolean |
||
157 | */ |
||
158 | public function isConnected() |
||
162 | |||
163 | /** |
||
164 | * Sets DGRAM mode |
||
165 | * @param boolean $bool DGRAM Mode |
||
166 | * @return void |
||
167 | */ |
||
168 | public function setDgram($bool) |
||
172 | |||
173 | /** |
||
174 | * Sets peer name |
||
175 | * @param string $host Hostname |
||
176 | * @param integer $port Port |
||
177 | * @return void |
||
178 | */ |
||
179 | public function setPeername($host, $port) |
||
192 | |||
193 | /** |
||
194 | * Getter |
||
195 | * @param string $name Name |
||
196 | * @return mixed |
||
197 | */ |
||
198 | public function __get($name) |
||
209 | |||
210 | /** |
||
211 | * Get socket name |
||
212 | * @param string &$addr Addr |
||
213 | * @param srting &$port Port |
||
214 | * @return void |
||
215 | */ |
||
216 | public function getSocketName(&$addr, &$port) |
||
224 | |||
225 | /** |
||
226 | * Sets parent socket |
||
227 | * @param \PHPDaemon\BoundSocket\Generic $sock |
||
228 | * @return void |
||
229 | */ |
||
230 | public function setParentSocket(Generic $sock) |
||
234 | |||
235 | /** |
||
236 | * Called when new UDP packet received |
||
237 | * @param object $pct Packet |
||
238 | * @return void |
||
239 | */ |
||
240 | public function onUdpPacket($pct) |
||
243 | |||
244 | /** |
||
245 | * Called when the connection is handshaked (at low-level), and peer is ready to recv. data |
||
246 | * @return void |
||
247 | */ |
||
248 | public function onReady() |
||
256 | |||
257 | /** |
||
258 | * Called if we inherit connection from request |
||
259 | * @param Request $req Parent Request |
||
260 | * @return void |
||
261 | */ |
||
262 | public function onInheritanceFromRequest($req) |
||
265 | |||
266 | /** |
||
267 | * Called when the connection failed to be established |
||
268 | * @return void |
||
269 | */ |
||
270 | public function onFailure() |
||
277 | |||
278 | /** |
||
279 | * Called when the connection failed |
||
280 | * @param EventBufferEvent $bev |
||
|
|||
281 | * @return void |
||
282 | */ |
||
283 | public function onFailureEv($bev = null) |
||
295 | |||
296 | /** |
||
297 | * Destructor |
||
298 | * @return void |
||
299 | */ |
||
300 | public function __destruct() |
||
306 | |||
307 | /** |
||
308 | * Send data to the connection. Note that it just writes to buffer that flushes at every baseloop |
||
309 | * @param string $data Data to send |
||
310 | * @return boolean Success |
||
311 | */ |
||
312 | public function write($data) |
||
319 | |||
320 | /** |
||
321 | * Executes the given callback when/if the connection is handshaked |
||
322 | * @param callable $cb Callback |
||
323 | * @return void |
||
324 | */ |
||
325 | public function onConnected($cb) |
||
336 | |||
337 | protected function importParams() |
||
371 | |||
372 | /** |
||
373 | * Initialize SSL context |
||
374 | * @return object|false Context |
||
375 | */ |
||
376 | protected function initSSLContext() |
||
406 | |||
407 | /** |
||
408 | * Get URL |
||
409 | * @return string |
||
410 | */ |
||
411 | public function getUrl() |
||
415 | |||
416 | /** |
||
417 | * Get host |
||
418 | * @return string |
||
419 | */ |
||
420 | public function getHost() |
||
424 | |||
425 | /** |
||
426 | * Get port |
||
427 | * @return integer |
||
428 | */ |
||
429 | public function getPort() |
||
433 | |||
434 | /** |
||
435 | * Connects to URL |
||
436 | * @param string $url URL |
||
437 | * @param callable $cb Callback |
||
438 | * @param \Closure $beforeConnect Callback |
||
439 | * @return boolean Success |
||
440 | */ |
||
441 | public function connect($url, $cb = null, \Closure $beforeConnect = null) |
||
503 | |||
504 | /** |
||
505 | * Establish UNIX socket connection |
||
506 | * @param string $path Path |
||
507 | * @return boolean Success |
||
508 | */ |
||
509 | public function connectUnix($path) |
||
528 | |||
529 | /** |
||
530 | * Establish raw socket connection |
||
531 | * @param string $host Hostname |
||
532 | * @return boolean Success |
||
533 | */ |
||
534 | public function connectRaw($host) |
||
573 | |||
574 | /** |
||
575 | * Establish UDP connection |
||
576 | * @param string $host Hostname |
||
577 | * @param integer $port Port |
||
578 | * @return boolean Success |
||
579 | */ |
||
580 | public function connectUdp($host, $port) |
||
630 | |||
631 | /** |
||
632 | * Establish TCP connection |
||
633 | * @param string $host Hostname |
||
634 | * @param integer $port Port |
||
635 | * @return boolean Success |
||
636 | */ |
||
637 | public function connectTcp($host, $port) |
||
706 | |||
707 | /** |
||
708 | * Set keepalive |
||
709 | * @param boolean $bool |
||
710 | * @return void |
||
711 | */ |
||
712 | public function setKeepalive($bool) |
||
717 | |||
718 | /** |
||
719 | * Close the connection |
||
720 | * @return void |
||
721 | */ |
||
722 | public function close() |
||
729 | |||
730 | /** |
||
731 | * Set timeouts |
||
732 | * @param integer $read Read timeout in seconds |
||
733 | * @param integer $write Write timeout in seconds |
||
734 | * @return void |
||
735 | */ |
||
736 | public function setTimeouts($read, $write) |
||
752 | |||
753 | /** |
||
754 | * Set socket option |
||
755 | * @param integer $level Level |
||
756 | * @param integer $optname Option |
||
757 | * @param mixed $val Value |
||
758 | * @return void |
||
759 | */ |
||
760 | public function setOption($level, $optname, $val) |
||
768 | |||
769 | /** |
||
770 | * Called when connection finishes |
||
771 | * @return void |
||
772 | */ |
||
773 | public function onFinish() |
||
783 | } |
||
784 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.