Complex classes like ftp_sockets 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 ftp_sockets, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class ftp_sockets extends ftp_base { |
||
29 | |||
30 | function __construct($verb=FALSE, $le=FALSE) { |
||
33 | |||
34 | // <!-- --------------------------------------------------------------------------------------- --> |
||
35 | // <!-- Private functions --> |
||
36 | // <!-- --------------------------------------------------------------------------------------- --> |
||
37 | |||
38 | function _settimeout($sock) { |
||
39 | if(!@socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) { |
||
40 | $this->PushError('_connect','socket set receive timeout',socket_strerror(socket_last_error($sock))); |
||
|
|||
41 | @socket_close($sock); |
||
42 | return FALSE; |
||
43 | } |
||
44 | if(!@socket_set_option($sock, SOL_SOCKET , SO_SNDTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) { |
||
45 | $this->PushError('_connect','socket set send timeout',socket_strerror(socket_last_error($sock))); |
||
46 | @socket_close($sock); |
||
47 | return FALSE; |
||
48 | } |
||
49 | return true; |
||
50 | } |
||
51 | |||
52 | function _connect($host, $port) { |
||
68 | |||
69 | function _readmsg($fnction="_readmsg"){ |
||
92 | |||
93 | function _exec($cmd, $fnction="_exec") { |
||
108 | |||
109 | function _data_prepare($mode=FTP_ASCII) { |
||
110 | if(!$this->_settype($mode)) return FALSE; |
||
111 | $this->SendMSG("Creating data socket"); |
||
112 | $this->_ftp_data_sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
||
113 | if ($this->_ftp_data_sock < 0) { |
||
114 | $this->PushError('_data_prepare','socket create failed',socket_strerror(socket_last_error($this->_ftp_data_sock))); |
||
115 | return FALSE; |
||
116 | } |
||
117 | if(!$this->_settimeout($this->_ftp_data_sock)) { |
||
118 | $this->_data_close(); |
||
119 | return FALSE; |
||
120 | } |
||
121 | if($this->_passive) { |
||
122 | if(!$this->_exec("PASV", "pasv")) { |
||
123 | $this->_data_close(); |
||
124 | return FALSE; |
||
125 | } |
||
126 | if(!$this->_checkCode()) { |
||
127 | $this->_data_close(); |
||
128 | return FALSE; |
||
129 | } |
||
130 | $ip_port = explode(",", preg_replace("/^.+ \\(?([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]+,[0-9]+)\\)?.*$/s", "\\1", $this->_message)); |
||
131 | $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3]; |
||
132 | $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]); |
||
133 | $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport); |
||
134 | if(!@socket_connect($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) { |
||
135 | $this->PushError("_data_prepare","socket_connect", socket_strerror(socket_last_error($this->_ftp_data_sock))); |
||
136 | $this->_data_close(); |
||
137 | return FALSE; |
||
138 | } |
||
139 | else $this->_ftp_temp_sock=$this->_ftp_data_sock; |
||
140 | } else { |
||
141 | if(!@socket_getsockname($this->_ftp_control_sock, $addr, $port)) { |
||
142 | $this->PushError("_data_prepare","can't get control socket information", socket_strerror(socket_last_error($this->_ftp_control_sock))); |
||
143 | $this->_data_close(); |
||
144 | return FALSE; |
||
145 | } |
||
146 | if(!@socket_bind($this->_ftp_data_sock,$addr)){ |
||
147 | $this->PushError("_data_prepare","can't bind data socket", socket_strerror(socket_last_error($this->_ftp_data_sock))); |
||
148 | $this->_data_close(); |
||
149 | return FALSE; |
||
150 | } |
||
151 | if(!@socket_listen($this->_ftp_data_sock)) { |
||
152 | $this->PushError("_data_prepare","can't listen data socket", socket_strerror(socket_last_error($this->_ftp_data_sock))); |
||
153 | $this->_data_close(); |
||
154 | return FALSE; |
||
155 | } |
||
156 | if(!@socket_getsockname($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) { |
||
157 | $this->PushError("_data_prepare","can't get data socket information", socket_strerror(socket_last_error($this->_ftp_data_sock))); |
||
158 | $this->_data_close(); |
||
159 | return FALSE; |
||
160 | } |
||
161 | if(!$this->_exec('PORT '.str_replace('.',',',$this->_datahost.'.'.($this->_dataport>>8).'.'.($this->_dataport&0x00FF)), "_port")) { |
||
162 | $this->_data_close(); |
||
163 | return FALSE; |
||
164 | } |
||
165 | if(!$this->_checkCode()) { |
||
166 | $this->_data_close(); |
||
167 | return FALSE; |
||
168 | } |
||
169 | } |
||
170 | return TRUE; |
||
171 | } |
||
172 | |||
173 | function _data_read($mode=FTP_ASCII, $fp=NULL) { |
||
174 | $NewLine=$this->_eol_code[$this->OS_local]; |
||
175 | if(is_resource($fp)) $out=0; |
||
176 | else $out=""; |
||
177 | if(!$this->_passive) { |
||
178 | $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport); |
||
179 | $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock); |
||
180 | if($this->_ftp_temp_sock===FALSE) { |
||
181 | $this->PushError("_data_read","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock))); |
||
182 | $this->_data_close(); |
||
183 | return FALSE; |
||
184 | } |
||
185 | } |
||
186 | |||
187 | while(($block=@socket_read($this->_ftp_temp_sock, $this->_ftp_buff_size, PHP_BINARY_READ))!==false) { |
||
188 | if($block==="") break; |
||
189 | if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block); |
||
190 | if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block)); |
||
191 | else $out.=$block; |
||
192 | } |
||
193 | return $out; |
||
194 | } |
||
195 | |||
196 | function _data_write($mode=FTP_ASCII, $fp=NULL) { |
||
197 | $NewLine=$this->_eol_code[$this->OS_local]; |
||
198 | if(is_resource($fp)) $out=0; |
||
199 | else $out=""; |
||
200 | if(!$this->_passive) { |
||
201 | $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport); |
||
202 | $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock); |
||
203 | if($this->_ftp_temp_sock===FALSE) { |
||
204 | $this->PushError("_data_write","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock))); |
||
205 | $this->_data_close(); |
||
206 | return false; |
||
207 | } |
||
208 | } |
||
209 | if(is_resource($fp)) { |
||
210 | while(!feof($fp)) { |
||
211 | $block=fread($fp, $this->_ftp_buff_size); |
||
212 | if(!$this->_data_write_block($mode, $block)) return false; |
||
213 | } |
||
214 | } elseif(!$this->_data_write_block($mode, $fp)) return false; |
||
215 | return true; |
||
216 | } |
||
217 | |||
218 | function _data_write_block($mode, $block) { |
||
219 | if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block); |
||
220 | do { |
||
221 | if(($t=@socket_write($this->_ftp_temp_sock, $block))===FALSE) { |
||
222 | $this->PushError("_data_write","socket_write", socket_strerror(socket_last_error($this->_ftp_temp_sock))); |
||
223 | $this->_data_close(); |
||
224 | return FALSE; |
||
225 | } |
||
226 | $block=substr($block, $t); |
||
227 | } while(!empty($block)); |
||
228 | return true; |
||
229 | } |
||
230 | |||
231 | function _data_close() { |
||
237 | |||
238 | function _quit() { |
||
245 | } |
||
246 | ?> |
||
247 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: