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 |
||
| 6 | class FilesystemEntity { |
||
| 7 | protected $server; |
||
| 8 | protected $path; |
||
| 9 | protected $executor; |
||
| 10 | protected $identity = null; |
||
| 11 | |||
| 12 | function __construct($path, $executor) { |
||
| 22 | |||
| 23 | function isLocal() { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Execute a command on the relevant server |
||
| 38 | * @param string $command Shell command, either a fully escaped string or an array |
||
| 39 | */ |
||
| 40 | function exec($command, $options = array()) { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Create a process for later exection |
||
| 46 | * @param string $command Shell command, either a fully escaped string or an array |
||
| 47 | * @return Process |
||
| 48 | */ |
||
| 49 | function createProcess($command, $options = array()) { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Upload a file to the given destination on the server |
||
| 62 | * @param string $file The file to upload |
||
| 63 | * @param string $dest The remote filename/dir to upload to |
||
| 64 | */ |
||
| 65 | View Code Duplication | function upload($source, $dest) { |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Create a file with the given content at the given destination on the server |
||
| 75 | * @param string $content The content of the file |
||
| 76 | * @param string $dest The remote filename/dir to upload to |
||
| 77 | */ |
||
| 78 | function uploadContent($content, $dest) { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Download a file from the given source on the server to the given file |
||
| 84 | * @param string $source The remote filename to download |
||
| 85 | * @param string $dest The local filename/dir to download to |
||
| 86 | */ |
||
| 87 | View Code Duplication | function download($source, $dest) { |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Returns true if the given file or directory exists |
||
| 97 | * @param string $file The file/dir to look for |
||
| 98 | * @return boolean |
||
| 99 | */ |
||
| 100 | function exists($file = null) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Create the given file with the given content |
||
| 115 | */ |
||
| 116 | function writeFile($file, $content) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Remove a file or folder from the webroot's server |
||
| 127 | * |
||
| 128 | * @param string $file The file to remove |
||
| 129 | */ |
||
| 130 | function unlink($file) { |
||
| 135 | } |
||
| 136 | |||
| 137 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.