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 |
||
19 | class RsyncTransporter extends AbstractTransporter implements SshCapableTransporterInterface |
||
20 | { |
||
21 | protected $cli; |
||
22 | protected $_exists = array(); |
||
23 | |||
24 | 11 | public function __construct(EventDispatcherInterface $dispatcher, Cli $cli) |
|
30 | |||
31 | /** |
||
32 | * Checks if a file or directory exists on the remote server |
||
33 | * |
||
34 | * @param string $path remote source path |
||
35 | * @return bool true when the resource exists, false otherwise |
||
36 | */ |
||
37 | 1 | View Code Duplication | public function exists($path) |
50 | |||
51 | /** |
||
52 | * Create a directory on the remote server |
||
53 | * |
||
54 | * @param string $dest remote path |
||
55 | * @param bool $recursive |
||
56 | * @throws \RuntimeException |
||
57 | */ |
||
58 | 2 | public function mkdir($dest, $recursive = true) |
|
76 | |||
77 | /** |
||
78 | * Retrieve file or directory from remote server |
||
79 | * |
||
80 | * @param string $src remote source path |
||
81 | * @param string $dest (optional) local destination path |
||
82 | * @throws \RuntimeException |
||
83 | * @return string |
||
84 | */ |
||
85 | 2 | View Code Duplication | public function get($src, $dest = null) |
108 | |||
109 | /** |
||
110 | * Upload a file or directory to remote server |
||
111 | * |
||
112 | * @param string $src local source path |
||
113 | * @param string $dest remote destination path |
||
114 | * @throws \RuntimeException |
||
115 | * @throws \InvalidArgumentException |
||
116 | */ |
||
117 | 2 | View Code Duplication | public function put($src, $dest) |
138 | |||
139 | /** |
||
140 | * Upload a string to remote server |
||
141 | * |
||
142 | * @param string $content content |
||
143 | * @param string $dest remote destination path |
||
144 | * @throws \RuntimeException |
||
145 | */ |
||
146 | 1 | View Code Duplication | public function putContent($content, $dest) |
166 | |||
167 | /** |
||
168 | * Creates a symlink on the remote server |
||
169 | * |
||
170 | * @param $src |
||
171 | * @param $dest |
||
172 | * @throws \RuntimeException |
||
173 | * @return mixed |
||
174 | */ |
||
175 | 1 | public function symlink($src, $dest) |
|
200 | |||
201 | /** |
||
202 | * Checks for symlink on the remote server |
||
203 | * |
||
204 | * @param $dest |
||
205 | * @return bool |
||
206 | */ |
||
207 | View Code Duplication | public function isSymlink($dest) |
|
218 | |||
219 | /** |
||
220 | * Copies a file/directory on the remote host |
||
221 | * |
||
222 | * @param string $src |
||
223 | * @param string $dest |
||
224 | * @param bool $recursive |
||
225 | * @throws \RuntimeException |
||
226 | * @return mixed |
||
227 | */ |
||
228 | 1 | public function copy($src, $dest, $recursive = true) |
|
241 | |||
242 | /** |
||
243 | * Removes a file/directory on the remote host |
||
244 | * |
||
245 | * @param string $path |
||
246 | * @param bool $recursive |
||
247 | * @throws \RuntimeException |
||
248 | * @return mixed |
||
249 | */ |
||
250 | 1 | View Code Duplication | public function remove($path, $recursive = true) |
263 | |||
264 | /** |
||
265 | * @todo this is just a simple implementation which should be improved |
||
266 | * |
||
267 | * @param $command |
||
268 | * @param null $callback |
||
269 | * @throws \RuntimeException |
||
270 | */ |
||
271 | View Code Duplication | public function exec($command, $callback = null) |
|
279 | |||
280 | /** |
||
281 | * Lists files and directories |
||
282 | * |
||
283 | * returns an array with the following format: |
||
284 | * |
||
285 | * array( |
||
286 | * 'filename' => array( |
||
287 | * 'type' => 'directory', // or 'file' |
||
288 | * 'mtime' => new \DateTime(), |
||
289 | * ), |
||
290 | * ); |
||
291 | * |
||
292 | * @param string $path |
||
293 | * @throws \RuntimeException |
||
294 | * @return array |
||
295 | */ |
||
296 | public function ls($path) |
||
300 | } |
||
301 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.