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 | 6 | public function __construct($service, array $parameters, $localTmpDir) |
|
44 | |||
45 | /** |
||
46 | * {@inheritDoc} |
||
47 | */ |
||
48 | 2 | public function read($path) |
|
49 | { |
||
50 | // In this method only, $path can be absolute, for reading files from |
||
51 | // outside of the file system directory, e.g. uploads, fixtures. |
||
52 | 2 | if (file_exists($path)) { |
|
53 | 1 | $fullPath = $path; |
|
54 | 1 | } else { |
|
55 | 1 | $path = $this->pathOrUrlToPath($path); |
|
56 | 1 | $fullPath = $this->absolutePath . '/' . $path; |
|
57 | } |
||
58 | |||
59 | 2 | return (string) file_get_contents($fullPath); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * {@inheritDoc} |
||
64 | */ |
||
65 | public function write($path, $source) |
||
71 | |||
72 | /** |
||
73 | * {@inheritDoc} |
||
74 | */ |
||
75 | 1 | public function writeContent($path, $content) |
|
76 | { |
||
77 | 1 | $path = $this->pathOrUrlToPath($path); |
|
78 | |||
79 | 1 | $this->service->dumpFile($this->absolutePath.'/'.$path, $content); |
|
80 | |||
81 | 1 | return $path; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritDoc} |
||
86 | */ |
||
87 | public function rename($sourcePath, $targetPath) |
||
96 | |||
97 | /** |
||
98 | * {@inheritDoc} |
||
99 | */ |
||
100 | 1 | public function delete($path) |
|
101 | { |
||
102 | 1 | $path = $this->pathOrUrlToPath($path); |
|
103 | |||
104 | 1 | $resp = $this->service->remove($this->absolutePath.'/'.$path); |
|
|
|||
105 | |||
106 | 1 | return true; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritDoc} |
||
111 | */ |
||
112 | 2 | 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 | 1 | public function exists($path) |
|
171 | { |
||
172 | 1 | $path = $this->pathOrUrlToPath($path); |
|
173 | |||
174 | 1 | return $this->service->exists($this->absolutePath.'/'.$path); |
|
175 | } |
||
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 | 1 | View Code Duplication | public function copyToLocalTemporaryFile($path) |
215 | { |
||
216 | 1 | $content = $this->read($path); |
|
217 | 1 | $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
218 | 1 | $target = tempnam($this->localTmpDir, null) . '.' . $extension; |
|
219 | |||
220 | 1 | file_put_contents($target, $content); |
|
221 | |||
222 | 1 | return $target; |
|
223 | } |
||
224 | |||
225 | /** |
||
226 | * {@inheritDoc} |
||
227 | */ |
||
228 | public function getService() |
||
229 | { |
||
230 | return $this->service; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param string $input |
||
235 | * @return array The path to the file inside the file system. Does not contain a preceding |
||
236 | * slash. |
||
237 | */ |
||
238 | 4 | private function pathOrUrlToPath($input) |
|
252 | } |
||
253 |
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.