Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 10 | trait RunsSSHCommands |
||
| 11 | { |
||
| 12 | use ChecksSSHConnection; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Runs scp. |
||
| 16 | * |
||
| 17 | * @param $file |
||
| 18 | * @param null $server |
||
| 19 | * @param bool $verbose |
||
| 20 | */ |
||
| 21 | protected function runScp($file, $destination_path, $server = null, $verbose = false) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Runs ssh command on server. |
||
| 39 | * |
||
| 40 | * @param $command |
||
| 41 | * @param $server |
||
| 42 | */ |
||
| 43 | View Code Duplication | protected function runSSH($command, $server = null) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Exec ssh command on server. |
||
| 54 | * |
||
| 55 | * @param $command |
||
| 56 | * @param $server |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | View Code Duplication | protected function execSSH($command, $server = null) |
|
| 68 | } |
||
| 69 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: