Complex classes like SSH 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 SSH, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class SSH { |
||
29 | |||
30 | /** |
||
31 | * Host of server |
||
32 | * |
||
33 | * @var string |
||
34 | * @access private |
||
35 | */ |
||
36 | private $host; |
||
37 | |||
38 | /** |
||
39 | * Username of authenticated session |
||
40 | * |
||
41 | * @var string |
||
42 | * @access private |
||
43 | */ |
||
44 | private $username; |
||
45 | |||
46 | /** |
||
47 | * File path to the private key file |
||
48 | * |
||
49 | * @var string |
||
50 | * @access private |
||
51 | */ |
||
52 | private $privateKey; |
||
53 | |||
54 | /** |
||
55 | * SSH protocol being used |
||
56 | * |
||
57 | * @var int |
||
58 | * @access private |
||
59 | */ |
||
60 | private $protocol; |
||
61 | |||
62 | /** |
||
63 | * Whether or not the connection was successful. |
||
64 | * |
||
65 | * @var bool |
||
66 | * @access private |
||
67 | */ |
||
68 | public $connected = false; |
||
69 | |||
70 | /** |
||
71 | * The SSH connection object. |
||
72 | * |
||
73 | * @var Object |
||
74 | * @access private |
||
75 | */ |
||
76 | private $SSHObject; |
||
77 | |||
78 | /** |
||
79 | * The SFTP connection object. |
||
80 | * |
||
81 | * @var Object |
||
82 | * @access private |
||
83 | */ |
||
84 | private $SFTPObject; |
||
85 | |||
86 | /** |
||
87 | * Stores the commits of the PHPSecLib class |
||
88 | * |
||
89 | * @var array |
||
90 | * @access private |
||
91 | */ |
||
92 | protected $commits; |
||
93 | |||
94 | /** |
||
95 | * HTTP class |
||
96 | * |
||
97 | * @var Object |
||
98 | * @access private |
||
99 | */ |
||
100 | protected $http; |
||
101 | |||
102 | |||
103 | /** |
||
104 | * Construction method for the SSH class |
||
105 | * |
||
106 | * @FIXME: Codebase no longer includes SSH-related classes |
||
107 | * |
||
108 | * @access public |
||
109 | * @param string $pgHost Address of remote host.Default |
||
110 | * @param int $pgPort Default: 22 |
||
111 | * @param string|null $pgUsername |
||
112 | * @param string|null $pgPassphrase |
||
113 | * @param string|null $pgPrivateKey |
||
114 | * @param int $pgProtocol |
||
115 | * @param int $pgTimeout |
||
116 | * @param HTTP $http This must be a HTTP class object from HTTP::getDefaultInstance() |
||
117 | * |
||
118 | * @see HTTP::getDefaultInstance() |
||
119 | * @see WIKI::__construct() |
||
120 | */ |
||
121 | public function __construct( |
||
188 | |||
189 | /** |
||
190 | * Establishes a connection to the remote server. |
||
191 | * |
||
192 | * @FIXME: Codebase no longer includes SSH-related classes |
||
193 | * |
||
194 | * @access protected |
||
195 | * @param string $pgHost Host of server to connect to. |
||
196 | * @param int $pgPort Port of server. |
||
197 | * @param int $pgProtocol Which SSH protocol to use. |
||
198 | * @param int $pgTimeout How long before the connection times out. (Milliseconds) |
||
199 | * @return bool |
||
200 | */ |
||
201 | protected function connect( $pgHost, $pgPort = 22, $pgProtocol, $pgTimeout = 10 ) { |
||
217 | |||
218 | /** |
||
219 | * Authenticates to the remote server. |
||
220 | * |
||
221 | * @access protected |
||
222 | * @param string $pgUsername Username |
||
223 | * @param string $pgPassphrase Password or passphrase of key file |
||
224 | * @param string $pgPrivateKey File path of key file. |
||
225 | * @return bool |
||
226 | */ |
||
227 | protected function authenticate($pgUsername, $pgPassphrase, $pgPrivateKey) |
||
275 | |||
276 | /** |
||
277 | * Opens a shell, sends a command and returns output and closes the shell. |
||
278 | * NOTICE: Using a command that opens a new shell will cause hangups. |
||
279 | * |
||
280 | * @access public |
||
281 | * @param string $command Command to execute |
||
282 | * @param string $callback Function to call upon executing task. |
||
283 | * @param bool $displayError Should stderr be outputted as well. Only available with SSH2. |
||
284 | * @param bool $exitstatus Returns the exit status along with output. Output becomes array. Only available with SSH2. |
||
285 | * @returns bool|string|array |
||
286 | */ |
||
287 | public function exec( $command, $callback = null, $displayError = false, $exitstatus = false ) { |
||
300 | |||
301 | /** |
||
302 | * Opens an interactive shell if not done already and transmits commands and returns output. |
||
303 | * |
||
304 | * @access public |
||
305 | * @param string $command Command to execute |
||
306 | * @param string $expect String of output to expect and remove from output. |
||
307 | * @param bool $expectRegex Switches string expectation to regular expressions. |
||
308 | * @returns bool|string |
||
309 | * |
||
310 | * FIXME Contains undefined constants |
||
311 | */ |
||
312 | public function iExec($command, $expect = "", $expectRegex = false) { |
||
319 | |||
320 | /** |
||
321 | * Sets a timeout for exec and iexec |
||
322 | * |
||
323 | * @access public |
||
324 | * @param int $time |
||
325 | * @return void |
||
326 | */ |
||
327 | public function setTimeout( $time ) |
||
331 | |||
332 | /** |
||
333 | * Returns whether or not exec or iexec timed out. Only available for SSH2. |
||
334 | * |
||
335 | * @access public |
||
336 | * @returns bool|null |
||
337 | */ |
||
338 | public function didTimout() { |
||
343 | |||
344 | /** |
||
345 | * Write a file to a remote server |
||
346 | * |
||
347 | * @access public |
||
348 | * @param string $to location of file to be placed |
||
349 | * @param string $data data to write or file location of file to upload |
||
350 | * @param bool $resume resume an interrupted transfer |
||
351 | * @return bool |
||
352 | * |
||
353 | * FIXME Contains undefined constants |
||
354 | */ |
||
355 | public function file_put_contents( $to, $data, $resume = false ) { |
||
363 | |||
364 | /** |
||
365 | * Retrieve a file from a remote server |
||
366 | * |
||
367 | * @access public |
||
368 | * @param string $from Location on remote server to retrieve from. |
||
369 | * @param string|bool $to Location to write to. If left blank, file contents is returned. |
||
370 | * @param int $offset Where to start retrieving files from. |
||
371 | * @param int $length How much of the file to retrieve. |
||
372 | * @returns bool|string |
||
373 | */ |
||
374 | public function file_get_contents( $from, $to = false, $offset = 0, $length = -1 ) |
||
378 | |||
379 | /** |
||
380 | * Makes directory |
||
381 | * |
||
382 | * @param string $pathname The directory path. |
||
383 | * @param int $mode The mode is 0777 by default, which means the widest possible access. |
||
384 | * @param bool $recursive Allows the creation of nested directories specified in the pathname. |
||
385 | * @return bool |
||
386 | * @access public |
||
387 | */ |
||
388 | public function mkdir( $pathname, $mode = 0777, $recursive = false ) |
||
392 | |||
393 | /** |
||
394 | * Changes SFTP's current directory to directory. |
||
395 | * |
||
396 | * @param string $directory The new current directory |
||
397 | * @return bool |
||
398 | * @access public |
||
399 | */ |
||
400 | public function chdir( $directory ) |
||
404 | |||
405 | /** |
||
406 | * Displays the current directory |
||
407 | * |
||
408 | * @access public |
||
409 | * @return string |
||
410 | */ |
||
411 | public function pwd() |
||
415 | |||
416 | /** |
||
417 | * Deletes a directory and all of its contents |
||
418 | * |
||
419 | * @access public |
||
420 | * @param string $dirname Path to the directory. |
||
421 | * @return bool |
||
422 | */ |
||
423 | public function rmdir( $dirname ) |
||
427 | |||
428 | /** |
||
429 | * Retrieves the contents of the directory |
||
430 | * |
||
431 | * @param string $dir Directory to retrieve |
||
432 | * @param bool $detailed Return details of contents |
||
433 | * @access public |
||
434 | * @return bool|array |
||
435 | */ |
||
436 | public function directory_get_contents( $dir, $detailed = false ) |
||
440 | |||
441 | /** |
||
442 | * Changes file mode |
||
443 | * |
||
444 | * @access public |
||
445 | * @param string $path Path to the directory or file |
||
446 | * @param int $mode Mode to change to |
||
447 | * @param bool $recursive Apply it to files within directory and children directories. |
||
448 | * @return bool|int |
||
449 | */ |
||
450 | public function chmod( $path, $mode, $recursive = false ) |
||
454 | |||
455 | /** |
||
456 | * Sets access and modification time of file |
||
457 | * |
||
458 | * @access public |
||
459 | * @param string $filename The name of the file being touched. |
||
460 | * @param int $time The touch time. If time is not supplied, the current system time is used. |
||
461 | * @param int $atime If present, the access time of the given filename is set to the value of atime. Otherwise, it is set to the value passed to the time parameter. If neither are present, the current system time is used. |
||
462 | * @return bool |
||
463 | */ |
||
464 | public function touch( $filename, $time = null, $atime = null ) |
||
468 | |||
469 | /** |
||
470 | * Changes file or directory owner |
||
471 | * |
||
472 | * @param string $filename Path to the file or directory. |
||
473 | * @param int $user A user number. |
||
474 | * @param bool $recursive Apply to all files and directories within dirctory. |
||
475 | * @access public |
||
476 | * @return bool |
||
477 | */ |
||
478 | public function chown( $filename, $user, $recursive = false ) |
||
482 | |||
483 | /** |
||
484 | * Changes file or directory group |
||
485 | * |
||
486 | * @param string $filename Path to the file or directory. |
||
487 | * @param int $group A group number. |
||
488 | * @param bool $recursive Apply to all files and directories within dirctory. |
||
489 | * @access public |
||
490 | * @return bool |
||
491 | */ |
||
492 | public function chgrp( $filename, $group, $recursive = false ) |
||
496 | |||
497 | /** |
||
498 | * Truncates a file to a specified size |
||
499 | * |
||
500 | * @param string $filename Path to the file. |
||
501 | * @param int $size Size to truncate to. |
||
502 | * @access public |
||
503 | * @return bool |
||
504 | */ |
||
505 | public function truncate( $filename, $size ) |
||
509 | |||
510 | /** |
||
511 | * Gives information about a file |
||
512 | * |
||
513 | * @param string $filename Path to the file. |
||
514 | * @access public |
||
515 | * @return array|bool |
||
516 | */ |
||
517 | public function stat( $filename ) |
||
521 | |||
522 | /** |
||
523 | * Gives information about a file or symbolic link |
||
524 | * |
||
525 | * @param string $filename Path to a file or a symbolic link. |
||
526 | * @access public |
||
527 | * @return array|bool |
||
528 | */ |
||
529 | public function lstat( $filename ) |
||
533 | |||
534 | /** |
||
535 | * Gets file size. Files >4GB return as 4GB. |
||
536 | * |
||
537 | * @access public |
||
538 | * @param string $filename Path to the file. |
||
539 | * @return bool|int |
||
540 | */ |
||
541 | public function filesize( $filename ) |
||
545 | |||
546 | /** |
||
547 | * Deletes a file |
||
548 | * |
||
549 | * @access public |
||
550 | * @param string $filename Path to the file. |
||
551 | * @return bool |
||
552 | */ |
||
553 | public function unlink( $filename ) |
||
557 | |||
558 | /** |
||
559 | * Renames a file or directory |
||
560 | * |
||
561 | * @access public |
||
562 | * @param string $oldname The old name. |
||
563 | * @param string $newname The new name. |
||
564 | * @return bool |
||
565 | */ |
||
566 | public function rename( $oldname, $newname ) |
||
570 | |||
571 | /** |
||
572 | * Check for Update Function |
||
573 | * |
||
574 | * Checks the phpseclib/phpseclib library for updates. |
||
575 | * |
||
576 | * @return bool Returns true if no updates |
||
577 | * Returns false if updates needed |
||
578 | * |
||
579 | * FIXME The .json file may no longer contain ['commit']['sha'], but possibly ['tree']['sha'] |
||
580 | */ |
||
581 | protected function CheckForUpdate() { |
||
593 | |||
594 | protected function installPHPseclib() { |
||
621 | |||
622 | /** |
||
623 | * @param string $gitFolder |
||
624 | */ |
||
625 | private function copyOverGitFiles( $gitFolder ) { |
||
644 | |||
645 | /** |
||
646 | * recursively remove a directory |
||
647 | * @param string $dir |
||
648 | */ |
||
649 | private function rrmdir( $dir ) { |
||
661 | |||
662 | /** |
||
663 | * @param string $fullUpdatePath |
||
664 | * @return string |
||
665 | */ |
||
666 | private function getLocalPath( $fullUpdatePath ) { |
||
672 | |||
673 | /** |
||
674 | * Destruction class. Closes the SSH connection and kills everything. |
||
675 | * |
||
676 | * @access private |
||
677 | * @return void |
||
678 | */ |
||
679 | public function __destruct() |
||
688 | } |
||
689 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..