Complex classes like FtpClient 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 FtpClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class FtpClient extends AbstractFtpClient { |
||
24 | |||
25 | /** |
||
26 | * Constructor. |
||
27 | * |
||
28 | * @param Authenticator $authenticator The authenticator. |
||
29 | */ |
||
30 | public function __construct(Authenticator $authenticator) { |
||
38 | |||
39 | /** |
||
40 | * Changes to the parent directory. |
||
41 | * |
||
42 | * @return FtpClient Returns this FTP client. |
||
43 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
44 | */ |
||
45 | public function cdup(): FtpClient { |
||
53 | |||
54 | /** |
||
55 | * Changes the current directory on a FTP server. |
||
56 | * |
||
57 | * @param string $directory The directory. |
||
58 | * @return FtpClient Returns this FTP client. |
||
59 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
60 | */ |
||
61 | public function chdir(string $directory): FtpClient { |
||
69 | |||
70 | /** |
||
71 | * Set permissions on a file via FTP. |
||
72 | * |
||
73 | * @param int $mode The mode. |
||
74 | * @param string $filename The filename. |
||
75 | * @return FtpClient Returns this FTP client. |
||
76 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
77 | */ |
||
78 | public function chmod(int $mode, string $filename): FtpClient { |
||
86 | |||
87 | /** |
||
88 | * Closes an FTP connection. |
||
89 | * |
||
90 | * @return FtpClient Returns this FTP client. |
||
91 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
92 | */ |
||
93 | public function close(): FtpClient { |
||
101 | |||
102 | /** |
||
103 | * Opens an FTP connection. |
||
104 | * |
||
105 | * @param int $timeout The timeout. |
||
106 | * @return FtpClient Returns this FTP client. |
||
107 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
108 | */ |
||
109 | public function connect(int $timeout = 90): FtpClient { |
||
121 | |||
122 | /** |
||
123 | * Deletes a file on the FTP server. |
||
124 | * |
||
125 | * @param string $path The path. |
||
126 | * @return FtpClient Returns this FTP client. |
||
127 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
128 | */ |
||
129 | public function delete(string $path): FtpClient { |
||
137 | |||
138 | /** |
||
139 | * Downloads a file from the FTP server and saves to an open file. |
||
140 | * |
||
141 | * @param resource $localStream The local stream. |
||
142 | * @param string $remoteFile The remote file. |
||
143 | * @param int $mode The mode. |
||
144 | * @param int $resumePos The resume position. |
||
145 | * @return FtpClient Returns this FTP client. |
||
146 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
147 | */ |
||
148 | public function fget($localStream, string $remoteFile, int $mode = FTP_BINARY, int $resumePos = 0): FtpClient { |
||
156 | |||
157 | /** |
||
158 | * Uploads from an open file to the FTP server. |
||
159 | * |
||
160 | * @param string $remoteFile The remote file. |
||
161 | * @param resource $localStream The local stream. |
||
162 | * @param int $mode The mode. |
||
163 | * @param int $startPos The start position. |
||
164 | * @return FtpClient Returns this FTP client. |
||
165 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
166 | */ |
||
167 | public function fput(string $remoteFile, $localStream, int $mode = FTP_BINARY, int $startPos = 0): FtpClient { |
||
175 | |||
176 | /** |
||
177 | * Downloads a file from the FTP server. |
||
178 | * |
||
179 | * @param string $localFile The local file. |
||
180 | * @param string $remoteFile The remote file. |
||
181 | * @param int $mode The mode. |
||
182 | * @param int $resumePos The resume position. |
||
183 | * @return FtpClient Returns this FTP client. |
||
184 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
185 | */ |
||
186 | public function get(string $localFile, string $remoteFile, int $mode = FTP_BINARY, int $resumePos = 0): FtpClient { |
||
194 | |||
195 | /** |
||
196 | * Logs in to an FTP connection. |
||
197 | * |
||
198 | * @return FtpClient Returns this FTP client. |
||
199 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
200 | */ |
||
201 | public function login(): FtpClient { |
||
212 | |||
213 | /** |
||
214 | * Returns the last modified time of the given file. |
||
215 | * |
||
216 | * @param string $remoteFile The remote file. |
||
217 | * @return int Returns the last modified time of the given file. |
||
218 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
219 | */ |
||
220 | public function mdtm(string $remoteFile): int { |
||
229 | |||
230 | /** |
||
231 | * Creates a directory. |
||
232 | * |
||
233 | * @param string $directory The directory. |
||
234 | * @return FtpClient Returns this FTP client. |
||
235 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
236 | */ |
||
237 | public function mkdir(string $directory): FtpClient { |
||
245 | |||
246 | /** |
||
247 | * Retrieves a file from the FTP server and writes it to an open file (non-blocking). |
||
248 | * |
||
249 | * @param resource $localStream The local stream. |
||
250 | * @param string $remoteFile The remote file. |
||
251 | * @param int $mode The mode. |
||
252 | * @param int $resumePos The resume position. |
||
253 | * @return int |
||
254 | */ |
||
255 | public function nbFget($localStream, string $remoteFile, int $mode = FTP_BINARY, int $resumePos = 0): int { |
||
258 | |||
259 | /** |
||
260 | * Stores a file from an open file to the FTP server (non-blocking). |
||
261 | * |
||
262 | * @param string $remoteFile The remote file. |
||
263 | * @param resource $localStream The local stream. |
||
264 | * @param int $mode The mode. |
||
265 | * @param int $startPos The start position. |
||
266 | * @return int |
||
267 | */ |
||
268 | public function nbFput(string $remoteFile, $localStream, int $mode = FTP_BINARY, $startPos = 0): int { |
||
271 | |||
272 | /** |
||
273 | * Retrieves a file from the FTP server and writes it to a local file (non-blocking). |
||
274 | * |
||
275 | * @param string $localFile The local file. |
||
276 | * @param string $remoteFile The remote file. |
||
277 | * @param int $mode The mode. |
||
278 | * @param int $resumePos The resume position. |
||
279 | * @return int |
||
280 | */ |
||
281 | public function nbGet(string $localFile, string $remoteFile, int $mode = FTP_BINARY, int $resumePos = 0): int { |
||
284 | |||
285 | /** |
||
286 | * Stores a file from an open file to the FTP server (non-blocking). |
||
287 | * |
||
288 | * @param string $remoteFile The remote file. |
||
289 | * @param string $localFile The local file. |
||
290 | * @param int $mode The mode. |
||
291 | * @param int $startPos The start position. |
||
292 | * @return int |
||
293 | */ |
||
294 | public function nbPut(string $remoteFile, string $localFile, int $mode = FTP_BINARY, int $startPos = 0): int { |
||
297 | |||
298 | /** |
||
299 | * Returns a list of files in the given directory. |
||
300 | * |
||
301 | * @param string $directory The directory. |
||
302 | * @return string[] Returns a list of files in the given directory. |
||
303 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
304 | */ |
||
305 | public function nlist(string $directory): array { |
||
314 | |||
315 | /** |
||
316 | * Turns passive mode on or off. |
||
317 | * |
||
318 | * @param bool $pasv The passive mode. |
||
319 | * @return FtpClient Returns this FTP client. |
||
320 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
321 | */ |
||
322 | public function pasv(bool $pasv): FtpClient { |
||
330 | |||
331 | /** |
||
332 | * Uploads a file to the FTP server. |
||
333 | * |
||
334 | * @param string $localFile The local file. |
||
335 | * @param string $remoteFile The remote file. |
||
336 | * @param int $mode The mode. |
||
337 | * @param int $startPos The start position. |
||
338 | * @return FtpClient Returns this FTP client. |
||
339 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
340 | */ |
||
341 | public function put(string $localFile, string $remoteFile, int $mode = FTP_BINARY, int $startPos = 0): FtpClient { |
||
349 | |||
350 | /** |
||
351 | * Returns the current directory name. |
||
352 | * |
||
353 | * @return FtpClient Returns this FTP client. |
||
354 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
355 | */ |
||
356 | public function pwd(): FtpClient { |
||
364 | |||
365 | /** |
||
366 | * Returns a detailed list of files in the given directory. |
||
367 | * |
||
368 | * @param string $directory The directory. |
||
369 | * @param bool $recursive Recursive ? |
||
370 | * @return array Returns a detailed list of files in the given directory. |
||
371 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
372 | */ |
||
373 | public function rawList(string $directory, bool $recursive = false): array { |
||
382 | |||
383 | /** |
||
384 | * Renames a file or a directory on the FTP server. |
||
385 | * |
||
386 | * @param string $oldName The old name. |
||
387 | * @param string $newName The new name. |
||
388 | * @return FtpClient Returns this FTP client. |
||
389 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
390 | */ |
||
391 | public function rename(string $oldName, string $newName): FtpClient { |
||
399 | |||
400 | /** |
||
401 | * Removes a directory. |
||
402 | * |
||
403 | * @param string $directory The directory. |
||
404 | * @return FtpClient Returns this FTP client. |
||
405 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
406 | */ |
||
407 | public function rmdir(string $directory): FtpClient { |
||
415 | |||
416 | /** |
||
417 | * Returns the size of the given file. |
||
418 | * |
||
419 | * @param string $remoteFile The remote file. |
||
420 | * @return int Returns the size of the given file. |
||
421 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
422 | */ |
||
423 | public function size(string $remoteFile): int { |
||
432 | |||
433 | /** |
||
434 | * Returns the system type identifier of the remote FTP server. |
||
435 | * |
||
436 | * @return string Returns the system type identifier of the remote FTP server. |
||
437 | * @throws FtpException Throws a FTP exception if an error occurs. |
||
438 | */ |
||
439 | public function systype(): string { |
||
448 | } |
||
449 |