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 |
||
16 | class SftpAdapter extends AbstractFtpAdapter |
||
17 | { |
||
18 | use StreamedCopyTrait; |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | protected $port = 22; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $privatekey; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $configurable = ['host', 'port', 'username', 'password', 'timeout', 'root', 'privateKey', 'permPrivate', 'permPublic', 'directoryPerm', 'NetSftpConnection']; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $statMap = ['mtime' => 'timestamp', 'size' => 'size']; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $directoryPerm = 0744; |
||
44 | |||
45 | /** |
||
46 | * Prefix a path. |
||
47 | * |
||
48 | * @param string $path |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | 6 | protected function prefix($path) |
|
56 | |||
57 | /** |
||
58 | * Set the private key (string or path to local file). |
||
59 | * |
||
60 | * @param string $key |
||
61 | * |
||
62 | * @return $this |
||
63 | */ |
||
64 | 9 | public function setPrivateKey($key) |
|
70 | |||
71 | /** |
||
72 | * Set permissions for new directory |
||
73 | * |
||
74 | * @param int $directoryPerm |
||
75 | * |
||
76 | * @return $this |
||
77 | */ |
||
78 | 6 | public function setDirectoryPerm($directoryPerm) |
|
84 | |||
85 | /** |
||
86 | * Get permissions for new directory |
||
87 | * |
||
88 | * @return int |
||
89 | */ |
||
90 | 3 | public function getDirectoryPerm() |
|
94 | |||
95 | /** |
||
96 | * Inject the SFTP instance. |
||
97 | * |
||
98 | * @param SFTP $connection |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | 21 | public function setNetSftpConnection(SFTP $connection) |
|
108 | |||
109 | /** |
||
110 | * Connect. |
||
111 | */ |
||
112 | 12 | public function connect() |
|
118 | |||
119 | /** |
||
120 | * Login. |
||
121 | * |
||
122 | * @throws LogicException |
||
123 | */ |
||
124 | 12 | protected function login() |
|
130 | |||
131 | /** |
||
132 | * Set the connection root. |
||
133 | */ |
||
134 | 9 | protected function setConnectionRoot() |
|
146 | |||
147 | /** |
||
148 | * Get the password, either the private key or a plain text password. |
||
149 | * |
||
150 | * @return RSA|string |
||
151 | */ |
||
152 | 15 | public function getPassword() |
|
160 | |||
161 | /** |
||
162 | * Get the private get with the password or private key contents. |
||
163 | * |
||
164 | * @return RSA |
||
165 | */ |
||
166 | 9 | public function getPrivateKey() |
|
182 | |||
183 | /** |
||
184 | * List the contents of a directory. |
||
185 | * |
||
186 | * @param string $directory |
||
187 | * @param bool $recursive |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | 6 | protected function listDirectoryContents($directory, $recursive = true) |
|
217 | |||
218 | /** |
||
219 | * Normalize a listing response. |
||
220 | * |
||
221 | * @param string $path |
||
222 | * @param array $object |
||
223 | * |
||
224 | * @return array |
||
225 | */ |
||
226 | 6 | protected function normalizeListingObject($path, array $object) |
|
241 | |||
242 | /** |
||
243 | * Disconnect. |
||
244 | */ |
||
245 | 6 | public function disconnect() |
|
249 | |||
250 | /** |
||
251 | * @inheritdoc |
||
252 | */ |
||
253 | 6 | public function write($path, $contents, Config $config) |
|
261 | |||
262 | /** |
||
263 | * @inheritdoc |
||
264 | */ |
||
265 | 6 | public function writeStream($path, $resource, Config $config) |
|
273 | |||
274 | /** |
||
275 | * Upload a file. |
||
276 | * |
||
277 | * @param string $path |
||
278 | * @param string|resource $contents |
||
279 | * @param Config $config |
||
280 | * @return bool |
||
281 | */ |
||
282 | 12 | public function upload($path, $contents, Config $config) |
|
303 | |||
304 | /** |
||
305 | * @inheritdoc |
||
306 | */ |
||
307 | 6 | public function read($path) |
|
317 | |||
318 | /** |
||
319 | * @inheritdoc |
||
320 | */ |
||
321 | 3 | public function readStream($path) |
|
335 | |||
336 | /** |
||
337 | * @inheritdoc |
||
338 | */ |
||
339 | 3 | public function update($path, $contents, Config $config) |
|
343 | |||
344 | /** |
||
345 | * @inheritdoc |
||
346 | */ |
||
347 | 3 | public function updateStream($path, $contents, Config $config) |
|
351 | |||
352 | /** |
||
353 | * @inheritdoc |
||
354 | */ |
||
355 | 3 | public function delete($path) |
|
361 | |||
362 | /** |
||
363 | * @inheritdoc |
||
364 | */ |
||
365 | 3 | public function rename($path, $newpath) |
|
371 | |||
372 | /** |
||
373 | * @inheritdoc |
||
374 | */ |
||
375 | 3 | public function deleteDir($dirname) |
|
381 | |||
382 | /** |
||
383 | * @inheritdoc |
||
384 | */ |
||
385 | 39 | public function has($path) |
|
389 | |||
390 | /** |
||
391 | * @inheritdoc |
||
392 | */ |
||
393 | 48 | public function getMetadata($path) |
|
408 | |||
409 | /** |
||
410 | * @inheritdoc |
||
411 | */ |
||
412 | 6 | public function getTimestamp($path) |
|
416 | |||
417 | /** |
||
418 | * @inheritdoc |
||
419 | */ |
||
420 | 3 | public function getMimetype($path) |
|
430 | |||
431 | /** |
||
432 | * @inheritdoc |
||
433 | */ |
||
434 | 3 | public function createDir($dirname, Config $config) |
|
444 | |||
445 | /** |
||
446 | * @inheritdoc |
||
447 | */ |
||
448 | 6 | public function getVisibility($path) |
|
452 | |||
453 | /** |
||
454 | * @inheritdoc |
||
455 | */ |
||
456 | 12 | public function setVisibility($path, $visibility) |
|
468 | |||
469 | /** |
||
470 | * @inheritdoc |
||
471 | */ |
||
472 | 6 | public function isConnected() |
|
480 | } |
||
481 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: