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 |
||
11 | class LocalStorage implements AdapterInterface |
||
12 | { |
||
13 | private $service; |
||
14 | |||
15 | /** |
||
16 | * Contains a preceding slash, and no trailing slash. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | private $absolutePath; |
||
21 | |||
22 | private $webUrl; |
||
23 | |||
24 | private $localTmpDir; |
||
25 | |||
26 | /** |
||
27 | * Constructor for LocalStorage adapter |
||
28 | */ |
||
29 | 5 | public function __construct($service, array $parameters, $localTmpDir) |
|
44 | |||
45 | /** |
||
46 | * {@inheritDoc} |
||
47 | */ |
||
48 | 1 | public function read($path) |
|
61 | |||
62 | /** |
||
63 | * {@inheritDoc} |
||
64 | */ |
||
65 | public function write($path, $source) |
||
71 | |||
72 | /** |
||
73 | * {@inheritDoc} |
||
74 | */ |
||
75 | public function writeContent($path, $content) |
||
83 | |||
84 | /** |
||
85 | * {@inheritDoc} |
||
86 | */ |
||
87 | public function rename($sourcePath, $targetPath) |
||
96 | |||
97 | /** |
||
98 | * {@inheritDoc} |
||
99 | */ |
||
100 | public function delete($path) |
||
108 | |||
109 | /** |
||
110 | * {@inheritDoc} |
||
111 | */ |
||
112 | 1 | public function getFiles($directory = "") |
|
122 | |||
123 | public function mkdir($dir) |
||
129 | |||
130 | public function copy($originFile, $targetFile) |
||
137 | |||
138 | /** |
||
139 | * {@inheritDoc} |
||
140 | */ |
||
141 | public function copyFiles($sourceDir, $targetDir) |
||
166 | |||
167 | /** |
||
168 | * {@inheritDoc} |
||
169 | */ |
||
170 | public function exists($path) |
||
176 | |||
177 | /** |
||
178 | * {@inheritDoc} |
||
179 | */ |
||
180 | public function isDirectory($path) |
||
190 | |||
191 | /** |
||
192 | * {@inheritDoc} |
||
193 | */ |
||
194 | public function getURL($path) |
||
200 | |||
201 | /** |
||
202 | * {@inheritDoc} |
||
203 | */ |
||
204 | 1 | public function getFileSize($path) |
|
210 | |||
211 | /** |
||
212 | * {@inheritDoc} |
||
213 | */ |
||
214 | View Code Duplication | public function copyToLocalTemporaryFile($path) |
|
224 | |||
225 | /** |
||
226 | * {@inheritDoc} |
||
227 | */ |
||
228 | public function getService() { |
||
231 | |||
232 | /** |
||
233 | * @param string $input |
||
234 | * @return array The path to the file inside the file system. Does not contain a preceding |
||
235 | * slash. |
||
236 | */ |
||
237 | 3 | private function pathOrUrlToPath($input) |
|
251 | } |
||
252 |
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.