Complex classes like SftpAdapter 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 SftpAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class SftpAdapter extends AbstractFtpAdapter |
||
18 | { |
||
19 | use StreamedCopyTrait; |
||
20 | |||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | protected $port = 22; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $hostFingerprint; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $privatekey; |
||
35 | |||
36 | /** |
||
37 | * @var bool |
||
38 | */ |
||
39 | protected $useAgent = false; |
||
40 | |||
41 | /** |
||
42 | * @var Agent |
||
43 | */ |
||
44 | private $agent; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $configurable = ['host', 'hostFingerprint', 'port', 'username', 'password', 'useAgent', 'agent', 'timeout', 'root', 'privateKey', 'permPrivate', 'permPublic', 'directoryPerm', 'NetSftpConnection']; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $statMap = ['mtime' => 'timestamp', 'size' => 'size']; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $directoryPerm = 0744; |
||
60 | |||
61 | /** |
||
62 | * Prefix a path. |
||
63 | * |
||
64 | * @param string $path |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | 6 | protected function prefix($path) |
|
72 | |||
73 | /** |
||
74 | * Set the username (string). |
||
75 | * |
||
76 | * @param string $val |
||
77 | * |
||
78 | * @return $this |
||
79 | */ |
||
80 | 9 | public function setUsername($val) { |
|
85 | |||
86 | /** |
||
87 | * Set the password (string). |
||
88 | * |
||
89 | * @param string $val |
||
90 | * |
||
91 | * @return $this |
||
92 | */ |
||
93 | 9 | public function setPassword($val) { |
|
98 | |||
99 | /** |
||
100 | * Set the finger print of the public key of the host you are connecting to. |
||
101 | * |
||
102 | * If the key does not match the server identification, the connection will |
||
103 | * be aborted. |
||
104 | * |
||
105 | * @param string $fingerprint Example: '88:76:75:96:c1:26:7c:dd:9f:87:50:db:ac:c4:a8:7c'. |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | 6 | public function setHostFingerprint($fingerprint) |
|
115 | |||
116 | /** |
||
117 | * Set the private key (string or path to local file). |
||
118 | * |
||
119 | * @param string $key |
||
120 | * |
||
121 | * @return $this |
||
122 | */ |
||
123 | 9 | public function setPrivateKey($key) |
|
129 | |||
130 | /** |
||
131 | * @param boolean $useAgent |
||
132 | * |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function setUseAgent($useAgent) |
||
141 | |||
142 | /** |
||
143 | * @param Agent $agent |
||
144 | * |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function setAgent(Agent $agent) |
||
153 | |||
154 | /** |
||
155 | * Set permissions for new directory |
||
156 | * |
||
157 | * @param int $directoryPerm |
||
158 | * |
||
159 | * @return $this |
||
160 | */ |
||
161 | 6 | public function setDirectoryPerm($directoryPerm) |
|
167 | |||
168 | /** |
||
169 | * Get permissions for new directory |
||
170 | * |
||
171 | * @return int |
||
172 | */ |
||
173 | 3 | public function getDirectoryPerm() |
|
177 | |||
178 | /** |
||
179 | * Inject the SFTP instance. |
||
180 | * |
||
181 | * @param SFTP $connection |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | 30 | public function setNetSftpConnection(SFTP $connection) |
|
191 | |||
192 | /** |
||
193 | * Connect. |
||
194 | */ |
||
195 | 21 | public function connect() |
|
201 | |||
202 | /** |
||
203 | * Login. |
||
204 | * |
||
205 | * @throws LogicException |
||
206 | */ |
||
207 | 21 | protected function login() |
|
227 | |||
228 | /** |
||
229 | * Convert the SSH RSA public key into a hex formatted fingerprint. |
||
230 | * |
||
231 | * @param string $publickey |
||
232 | * @return string Hex formatted fingerprint, e.g. '88:76:75:96:c1:26:7c:dd:9f:87:50:db:ac:c4:a8:7c'. |
||
233 | */ |
||
234 | 6 | private function getHexFingerprintFromSshPublicKey ($publickey) |
|
239 | |||
240 | /** |
||
241 | * Set the connection root. |
||
242 | */ |
||
243 | 15 | protected function setConnectionRoot() |
|
256 | |||
257 | /** |
||
258 | * Get the password, either the private key or a plain text password. |
||
259 | * |
||
260 | * @return Agent|RSA|string |
||
261 | */ |
||
262 | 21 | public function getAuthentication() |
|
274 | |||
275 | /** |
||
276 | * Get the private get with the password or private key contents. |
||
277 | * |
||
278 | * @return RSA |
||
279 | */ |
||
280 | 9 | public function getPrivateKey() |
|
296 | |||
297 | /** |
||
298 | * @return Agent|bool |
||
299 | */ |
||
300 | public function getAgent() |
||
308 | |||
309 | /** |
||
310 | * List the contents of a directory. |
||
311 | * |
||
312 | * @param string $directory |
||
313 | * @param bool $recursive |
||
314 | * |
||
315 | * @return array |
||
316 | */ |
||
317 | 6 | protected function listDirectoryContents($directory, $recursive = true) |
|
343 | |||
344 | /** |
||
345 | * Normalize a listing response. |
||
346 | * |
||
347 | * @param string $path |
||
348 | * @param array $object |
||
349 | * |
||
350 | * @return array |
||
351 | */ |
||
352 | 6 | protected function normalizeListingObject($path, array $object) |
|
368 | |||
369 | /** |
||
370 | * Disconnect. |
||
371 | */ |
||
372 | 15 | public function disconnect() |
|
376 | |||
377 | /** |
||
378 | * @inheritdoc |
||
379 | */ |
||
380 | 6 | public function write($path, $contents, Config $config) |
|
388 | |||
389 | /** |
||
390 | * @inheritdoc |
||
391 | */ |
||
392 | 6 | public function writeStream($path, $resource, Config $config) |
|
400 | |||
401 | /** |
||
402 | * Upload a file. |
||
403 | * |
||
404 | * @param string $path |
||
405 | * @param string|resource $contents |
||
406 | * @param Config $config |
||
407 | * @return bool |
||
408 | */ |
||
409 | 12 | public function upload($path, $contents, Config $config) |
|
425 | |||
426 | /** |
||
427 | * @inheritdoc |
||
428 | */ |
||
429 | 6 | public function read($path) |
|
439 | |||
440 | /** |
||
441 | * @inheritdoc |
||
442 | */ |
||
443 | 3 | public function readStream($path) |
|
457 | |||
458 | /** |
||
459 | * @inheritdoc |
||
460 | */ |
||
461 | 3 | public function update($path, $contents, Config $config) |
|
465 | |||
466 | /** |
||
467 | * @inheritdoc |
||
468 | */ |
||
469 | 3 | public function updateStream($path, $contents, Config $config) |
|
473 | |||
474 | /** |
||
475 | * @inheritdoc |
||
476 | */ |
||
477 | 3 | public function delete($path) |
|
483 | |||
484 | /** |
||
485 | * @inheritdoc |
||
486 | */ |
||
487 | 3 | public function rename($path, $newpath) |
|
493 | |||
494 | /** |
||
495 | * @inheritdoc |
||
496 | */ |
||
497 | 3 | public function deleteDir($dirname) |
|
503 | |||
504 | /** |
||
505 | * @inheritdoc |
||
506 | */ |
||
507 | 39 | public function has($path) |
|
511 | |||
512 | /** |
||
513 | * @inheritdoc |
||
514 | */ |
||
515 | 49 | public function getMetadata($path) |
|
530 | |||
531 | /** |
||
532 | * @inheritdoc |
||
533 | */ |
||
534 | 6 | public function getTimestamp($path) |
|
538 | |||
539 | /** |
||
540 | * @inheritdoc |
||
541 | */ |
||
542 | 3 | public function getMimetype($path) |
|
552 | |||
553 | /** |
||
554 | * @inheritdoc |
||
555 | */ |
||
556 | 3 | public function createDir($dirname, Config $config) |
|
566 | |||
567 | /** |
||
568 | * @inheritdoc |
||
569 | */ |
||
570 | 6 | public function getVisibility($path) |
|
574 | |||
575 | /** |
||
576 | * @inheritdoc |
||
577 | */ |
||
578 | 12 | public function setVisibility($path, $visibility) |
|
590 | |||
591 | /** |
||
592 | * @inheritdoc |
||
593 | */ |
||
594 | 6 | public function isConnected() |
|
602 | } |
||
603 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: