Complex classes like AbstractFtpAdapter 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 AbstractFtpAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | abstract class AbstractFtpAdapter extends AbstractAdapter |
||
12 | { |
||
13 | /** |
||
14 | * @var mixed |
||
15 | */ |
||
16 | protected $connection; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $host; |
||
22 | |||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | protected $port = 21; |
||
27 | |||
28 | /** |
||
29 | * @var string|null |
||
30 | */ |
||
31 | protected $username; |
||
32 | |||
33 | /** |
||
34 | * @var string|null |
||
35 | */ |
||
36 | protected $password; |
||
37 | |||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $ssl = false; |
||
42 | |||
43 | /** |
||
44 | * @var int |
||
45 | */ |
||
46 | protected $timeout = 90; |
||
47 | |||
48 | /** |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $passive = true; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $separator = '/'; |
||
57 | |||
58 | /** |
||
59 | * @var string|null |
||
60 | */ |
||
61 | protected $root; |
||
62 | |||
63 | /** |
||
64 | * @var int |
||
65 | */ |
||
66 | protected $permPublic = 0744; |
||
67 | |||
68 | /** |
||
69 | * @var int |
||
70 | */ |
||
71 | protected $permPrivate = 0700; |
||
72 | |||
73 | /** |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $configurable = array(); |
||
77 | |||
78 | /** |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $systemType; |
||
82 | |||
83 | /** |
||
84 | * @var bool |
||
85 | */ |
||
86 | protected $alternativeRecursion = false; |
||
87 | |||
88 | /** |
||
89 | * Constructor. |
||
90 | * |
||
91 | * @param array $config |
||
92 | */ |
||
93 | 96 | public function __construct(array $config) |
|
97 | |||
98 | /** |
||
99 | * Set the config. |
||
100 | * |
||
101 | * @param array $config |
||
102 | * |
||
103 | * @return $this |
||
104 | */ |
||
105 | 96 | public function setConfig(array $config) |
|
121 | |||
122 | /** |
||
123 | * Returns the host. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | 90 | public function getHost() |
|
131 | |||
132 | /** |
||
133 | * Set the host. |
||
134 | * |
||
135 | * @param string $host |
||
136 | * |
||
137 | * @return $this |
||
138 | */ |
||
139 | 96 | public function setHost($host) |
|
145 | |||
146 | /** |
||
147 | * Set the public permission value. |
||
148 | * |
||
149 | * @param int $permPublic |
||
150 | * |
||
151 | * @return $this |
||
152 | */ |
||
153 | 78 | public function setPermPublic($permPublic) |
|
159 | |||
160 | /** |
||
161 | * Set the private permission value. |
||
162 | * |
||
163 | * @param int $permPrivate |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | 78 | public function setPermPrivate($permPrivate) |
|
173 | |||
174 | /** |
||
175 | * Returns the ftp port. |
||
176 | * |
||
177 | * @return int |
||
178 | */ |
||
179 | 90 | public function getPort() |
|
183 | |||
184 | /** |
||
185 | * Returns the root folder to work from. |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | 78 | public function getRoot() |
|
193 | |||
194 | /** |
||
195 | * Set the ftp port. |
||
196 | * |
||
197 | * @param int|string $port |
||
198 | * |
||
199 | * @return $this |
||
200 | */ |
||
201 | 78 | public function setPort($port) |
|
207 | |||
208 | /** |
||
209 | * Set the root folder to work from. |
||
210 | * |
||
211 | * @param string $root |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | 87 | public function setRoot($root) |
|
221 | |||
222 | /** |
||
223 | * Returns the ftp username. |
||
224 | * |
||
225 | * @return string username |
||
226 | */ |
||
227 | 84 | public function getUsername() |
|
231 | |||
232 | /** |
||
233 | * Set ftp username. |
||
234 | * |
||
235 | * @param string $username |
||
236 | * |
||
237 | * @return $this |
||
238 | */ |
||
239 | 78 | public function setUsername($username) |
|
245 | |||
246 | /** |
||
247 | * Returns the password. |
||
248 | * |
||
249 | * @return string password |
||
250 | */ |
||
251 | 84 | public function getPassword() |
|
255 | |||
256 | /** |
||
257 | * Set the ftp password. |
||
258 | * |
||
259 | * @param string $password |
||
260 | * |
||
261 | * @return $this |
||
262 | */ |
||
263 | 78 | public function setPassword($password) |
|
269 | |||
270 | /** |
||
271 | * Returns the amount of seconds before the connection will timeout. |
||
272 | * |
||
273 | * @return int |
||
274 | */ |
||
275 | 90 | public function getTimeout() |
|
279 | |||
280 | /** |
||
281 | * Set the amount of seconds before the connection should timeout. |
||
282 | * |
||
283 | * @param int $timeout |
||
284 | * |
||
285 | * @return $this |
||
286 | */ |
||
287 | 78 | public function setTimeout($timeout) |
|
293 | |||
294 | /** |
||
295 | * Return the FTP system type. |
||
296 | * |
||
297 | * @return string |
||
298 | */ |
||
299 | 3 | public function getSystemType() |
|
303 | |||
304 | /** |
||
305 | * Set the FTP system type (windows or unix). |
||
306 | * |
||
307 | * @param string $systemType |
||
308 | * |
||
309 | * @return $this |
||
310 | */ |
||
311 | 15 | public function setSystemType($systemType) |
|
317 | |||
318 | /** |
||
319 | * @inheritdoc |
||
320 | */ |
||
321 | 27 | public function listContents($directory = '', $recursive = false) |
|
325 | |||
326 | abstract protected function listDirectoryContents($directory, $recursive = false); |
||
327 | |||
328 | /** |
||
329 | * Normalize a directory listing. |
||
330 | * |
||
331 | * @param array $listing |
||
332 | * @param string $prefix |
||
333 | * |
||
334 | * @return array directory listing |
||
335 | */ |
||
336 | 21 | protected function normalizeListing(array $listing, $prefix = '') |
|
353 | |||
354 | /** |
||
355 | * Sort a directory listing. |
||
356 | * |
||
357 | * @param array $result |
||
358 | * |
||
359 | * @return array sorted listing |
||
360 | */ |
||
361 | 18 | protected function sortListing(array $result) |
|
371 | |||
372 | /** |
||
373 | * Normalize a file entry. |
||
374 | * |
||
375 | * @param string $item |
||
376 | * @param string $base |
||
377 | * |
||
378 | * @return array normalized file array |
||
379 | * |
||
380 | * @throws NotSupportedException |
||
381 | */ |
||
382 | 42 | protected function normalizeObject($item, $base) |
|
394 | |||
395 | /** |
||
396 | * Normalize a Unix file entry. |
||
397 | * |
||
398 | * @param string $item |
||
399 | * @param string $base |
||
400 | * |
||
401 | * @return array normalized file array |
||
402 | */ |
||
403 | 33 | protected function normalizeUnixObject($item, $base) |
|
425 | |||
426 | /** |
||
427 | * Normalize a Windows/DOS file entry. |
||
428 | * |
||
429 | * @param string $item |
||
430 | * @param string $base |
||
431 | * |
||
432 | * @return array normalized file array |
||
433 | */ |
||
434 | 6 | protected function normalizeWindowsObject($item, $base) |
|
462 | |||
463 | /** |
||
464 | * Get the system type from a listing item. |
||
465 | * |
||
466 | * @param string $item |
||
467 | * |
||
468 | * @return string the system type |
||
469 | */ |
||
470 | 33 | protected function detectSystemType($item) |
|
474 | |||
475 | /** |
||
476 | * Get the file type from the permissions. |
||
477 | * |
||
478 | * @param string $permissions |
||
479 | * |
||
480 | * @return string file type |
||
481 | */ |
||
482 | 30 | protected function detectType($permissions) |
|
486 | |||
487 | /** |
||
488 | * Normalize a permissions string. |
||
489 | * |
||
490 | * @param string $permissions |
||
491 | * |
||
492 | * @return int |
||
493 | */ |
||
494 | 30 | protected function normalizePermissions($permissions) |
|
514 | |||
515 | /** |
||
516 | * Filter out dot-directories. |
||
517 | * |
||
518 | * @param array $list |
||
519 | * |
||
520 | * @return array |
||
521 | */ |
||
522 | public function removeDotDirectories(array $list) |
||
534 | |||
535 | /** |
||
536 | * @inheritdoc |
||
537 | */ |
||
538 | 9 | public function has($path) |
|
542 | |||
543 | /** |
||
544 | * @inheritdoc |
||
545 | */ |
||
546 | 6 | public function getSize($path) |
|
550 | |||
551 | /** |
||
552 | * @inheritdoc |
||
553 | */ |
||
554 | 6 | public function getVisibility($path) |
|
558 | |||
559 | /** |
||
560 | * Ensure a directory exists. |
||
561 | * |
||
562 | * @param string $dirname |
||
563 | */ |
||
564 | 9 | public function ensureDirectory($dirname) |
|
570 | |||
571 | /** |
||
572 | * @return mixed |
||
573 | */ |
||
574 | 63 | public function getConnection() |
|
583 | |||
584 | /** |
||
585 | * Get the public permission value. |
||
586 | * |
||
587 | * @return int |
||
588 | */ |
||
589 | 6 | public function getPermPublic() |
|
593 | |||
594 | /** |
||
595 | * Get the private permission value. |
||
596 | * |
||
597 | * @return int |
||
598 | */ |
||
599 | 6 | public function getPermPrivate() |
|
603 | |||
604 | /** |
||
605 | * Disconnect on destruction. |
||
606 | */ |
||
607 | 96 | public function __destruct() |
|
611 | |||
612 | /** |
||
613 | * Establish a connection. |
||
614 | */ |
||
615 | abstract public function connect(); |
||
616 | |||
617 | /** |
||
618 | * Close the connection. |
||
619 | */ |
||
620 | abstract public function disconnect(); |
||
621 | |||
622 | /** |
||
623 | * Check if a connection is active. |
||
624 | * |
||
625 | * @return bool |
||
626 | */ |
||
627 | abstract public function isConnected(); |
||
628 | } |
||
629 |