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 FtpTransporter extends AbstractTransporter |
||
20 | { |
||
21 | protected $stream; |
||
22 | |||
23 | 1 | public function __construct(EventDispatcherInterface $dispatcher) |
|
27 | |||
28 | public function connect() |
||
29 | { |
||
30 | $this->dispatcher->dispatch(TransporterEvents::TRANSPORTER_CONNECT, new TransporterEvent($this)); |
||
31 | |||
32 | $port = $this->port ?: 21; |
||
33 | |||
34 | $this->stream = ftp_connect($this->host, $port); |
||
35 | if (false == $this->stream) { |
||
36 | throw new \RuntimeException(sprintf('Could not connect to host %s on port %s', $this->host, $port)); |
||
37 | } |
||
38 | ftp_pasv($this->stream, true); |
||
39 | } |
||
40 | |||
41 | public function login() |
||
42 | { |
||
43 | $success = ftp_login($this->stream, $this->username, $this->password); |
||
|
|||
44 | } |
||
45 | |||
46 | 1 | public function connectAndLogin() |
|
51 | |||
52 | public function exists($path) |
||
53 | { |
||
83 | |||
84 | public function mkdir($dest) |
||
95 | |||
96 | View Code Duplication | public function get($src, $dest = null) |
|
119 | |||
120 | 1 | public function put($src, $dest) |
|
154 | |||
155 | public function putContent($content, $dest) |
||
171 | |||
172 | /** |
||
173 | * Creates a symlink on the remote server |
||
174 | * |
||
175 | * @param $src |
||
176 | * @param $dest |
||
177 | * @throws \RuntimeException |
||
178 | * @return mixed |
||
179 | */ |
||
180 | public function symlink($src, $dest) |
||
184 | |||
185 | /** |
||
186 | * Checks for symlink on the remote server |
||
187 | * |
||
188 | * @param $dest |
||
189 | * @throws \RuntimeException |
||
190 | * @return bool |
||
191 | */ |
||
192 | public function isSymlink($dest) |
||
196 | |||
197 | /** |
||
198 | * Copies a file/directory on the remote host |
||
199 | * |
||
200 | * @param string $src |
||
201 | * @param string $dest |
||
202 | * @param bool $recursive |
||
203 | * @throws \RuntimeException |
||
204 | * @return mixed |
||
205 | */ |
||
206 | public function copy($src, $dest, $recursive = true) |
||
210 | |||
211 | /** |
||
212 | * Removes a file/directory on the remote host |
||
213 | * |
||
214 | * @param string $path |
||
215 | * @param bool $recursive |
||
216 | * @throws \RuntimeException |
||
217 | * @return mixed |
||
218 | */ |
||
219 | public function remove($path, $recursive = true) |
||
223 | |||
224 | /** |
||
225 | * Lists files and directories |
||
226 | * |
||
227 | * @todo WARNING: this is untested code!! |
||
228 | * |
||
229 | * @param string $path |
||
230 | * @return mixed |
||
231 | */ |
||
232 | public function ls($path) |
||
273 | |||
274 | public function __destruct() |
||
280 | } |
||
281 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.