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 |
||
10 | class NullAdapter extends AbstractAdapter |
||
11 | { |
||
12 | /** |
||
13 | * Reads a file as a stream. |
||
14 | * |
||
15 | * @param string $path |
||
16 | * |
||
17 | * @return array|false |
||
18 | * |
||
19 | * @see League\Flysystem\ReadInterface::readStream() |
||
20 | */ |
||
21 | View Code Duplication | public function readStream($path) |
|
35 | /** |
||
36 | * Stream fallback delegator. |
||
37 | * |
||
38 | * @param string $path |
||
39 | * @param resource $resource |
||
40 | * @param Config $config |
||
41 | * @param string $fallback |
||
42 | * |
||
43 | * @return mixed fallback result |
||
44 | */ |
||
45 | View Code Duplication | protected function stream($path, $resource, Config $config, $fallback) |
|
53 | |||
54 | /** |
||
55 | * Write using a stream. |
||
56 | * |
||
57 | * @param string $path |
||
58 | * @param resource $resource |
||
59 | * @param Config $config |
||
60 | * |
||
61 | * @return mixed false or file metadata |
||
62 | */ |
||
63 | public function writeStream($path, $resource, Config $config) |
||
67 | |||
68 | /** |
||
69 | * Update a file using a stream. |
||
70 | * |
||
71 | * @param string $path |
||
72 | * @param resource $resource |
||
73 | * @param Config $config Config object or visibility setting |
||
74 | * |
||
75 | * @return mixed false of file metadata |
||
76 | */ |
||
77 | public function updateStream($path, $resource, Config $config) |
||
81 | |||
82 | View Code Duplication | public function copy($path, $newpath) |
|
98 | |||
99 | /** |
||
100 | * Check whether a file is present. |
||
101 | * |
||
102 | * @param string $path |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | 9 | public function has($path) |
|
110 | |||
111 | /** |
||
112 | * @inheritdoc |
||
113 | */ |
||
114 | 6 | public function write($path, $contents, Config $config) |
|
125 | |||
126 | /** |
||
127 | * @inheritdoc |
||
128 | */ |
||
129 | 3 | public function update($path, $contents, Config $config) |
|
133 | |||
134 | /** |
||
135 | * @inheritdoc |
||
136 | */ |
||
137 | 6 | public function read($path) |
|
141 | |||
142 | /** |
||
143 | * @inheritdoc |
||
144 | */ |
||
145 | 3 | public function rename($path, $newpath) |
|
149 | |||
150 | /** |
||
151 | * @inheritdoc |
||
152 | */ |
||
153 | 6 | public function delete($path) |
|
157 | |||
158 | /** |
||
159 | * @inheritdoc |
||
160 | */ |
||
161 | 3 | public function listContents($directory = '', $recursive = false) |
|
165 | |||
166 | /** |
||
167 | * @inheritdoc |
||
168 | */ |
||
169 | 3 | public function getMetadata($path) |
|
173 | |||
174 | /** |
||
175 | * @inheritdoc |
||
176 | */ |
||
177 | 3 | public function getSize($path) |
|
181 | |||
182 | /** |
||
183 | * @inheritdoc |
||
184 | */ |
||
185 | 3 | public function getMimetype($path) |
|
189 | |||
190 | /** |
||
191 | * @inheritdoc |
||
192 | */ |
||
193 | 3 | public function getTimestamp($path) |
|
197 | |||
198 | /** |
||
199 | * @inheritdoc |
||
200 | */ |
||
201 | 3 | public function getVisibility($path) |
|
205 | |||
206 | /** |
||
207 | * @inheritdoc |
||
208 | */ |
||
209 | 3 | public function setVisibility($path, $visibility) |
|
213 | |||
214 | /** |
||
215 | * @inheritdoc |
||
216 | */ |
||
217 | 3 | public function createDir($dirname, Config $config) |
|
221 | |||
222 | /** |
||
223 | * @inheritdoc |
||
224 | */ |
||
225 | 3 | public function deleteDir($dirname) |
|
229 | } |
||
230 |
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.