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 |
||
15 | class GuzzleAdapter implements AdapterInterface |
||
16 | { |
||
17 | /** |
||
18 | * The base URL. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $base; |
||
23 | |||
24 | /** |
||
25 | * The Guzzle HTTP client. |
||
26 | * |
||
27 | * @var \GuzzleHttp\ClientInterface |
||
28 | */ |
||
29 | protected $client; |
||
30 | |||
31 | /** |
||
32 | * The visibility of this adapter. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $visibility = AdapterInterface::VISIBILITY_PUBLIC; |
||
37 | |||
38 | /** |
||
39 | * Constructs an Http object. |
||
40 | * |
||
41 | * @param string $base The base URL. |
||
42 | * @param \GuzzleHttp\ClientInterface $client An optional Guzzle client. |
||
43 | */ |
||
44 | 3 | public function __construct($base, ClientInterface $client = null) |
|
68 | |||
69 | /** |
||
70 | * Returns the base URL. |
||
71 | * |
||
72 | * @return string The base URL. |
||
73 | */ |
||
74 | 3 | public function getBaseUrl() |
|
78 | |||
79 | /** |
||
80 | * @inheritdoc |
||
81 | */ |
||
82 | 3 | public function copy($path, $newpath) |
|
86 | |||
87 | /** |
||
88 | * @inheritdoc |
||
89 | */ |
||
90 | 3 | public function createDir($path, Config $config) |
|
94 | |||
95 | /** |
||
96 | * @inheritdoc |
||
97 | */ |
||
98 | 3 | public function delete($path) |
|
102 | |||
103 | /** |
||
104 | * @inheritdoc |
||
105 | */ |
||
106 | 3 | public function deleteDir($path) |
|
110 | |||
111 | /** |
||
112 | * @inheritdoc |
||
113 | */ |
||
114 | 3 | public function getMetadata($path) |
|
142 | |||
143 | /** |
||
144 | * @inheritdoc |
||
145 | */ |
||
146 | 3 | public function getMimetype($path) |
|
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | 3 | public function getSize($path) |
|
158 | |||
159 | /** |
||
160 | * @inheritdoc |
||
161 | */ |
||
162 | 3 | public function getTimestamp($path) |
|
166 | |||
167 | /** |
||
168 | * @inheritdoc |
||
169 | */ |
||
170 | 3 | public function getVisibility($path) |
|
177 | |||
178 | /** |
||
179 | * @inheritdoc |
||
180 | */ |
||
181 | 3 | public function has($path) |
|
191 | |||
192 | /** |
||
193 | * @inheritdoc |
||
194 | */ |
||
195 | 3 | public function listContents($directory = '', $recursive = false) |
|
199 | |||
200 | /** |
||
201 | * @inheritdoc |
||
202 | */ |
||
203 | 3 | public function read($path) |
|
220 | |||
221 | /** |
||
222 | * @inheritdoc |
||
223 | */ |
||
224 | 3 | public function readStream($path) |
|
237 | |||
238 | /** |
||
239 | * @inheritdoc |
||
240 | */ |
||
241 | 3 | public function rename($path, $newpath) |
|
245 | |||
246 | /** |
||
247 | * @inheritdoc |
||
248 | */ |
||
249 | 3 | public function setVisibility($path, $visibility) |
|
257 | |||
258 | /** |
||
259 | * @inheritdoc |
||
260 | */ |
||
261 | 3 | public function update($path, $contents, Config $conf) |
|
265 | |||
266 | /** |
||
267 | * @inheritdoc |
||
268 | */ |
||
269 | 3 | public function updateStream($path, $resource, Config $config) |
|
273 | |||
274 | /** |
||
275 | * @inheritdoc |
||
276 | */ |
||
277 | 3 | public function write($path, $contents, Config $config) |
|
281 | |||
282 | /** |
||
283 | * @inheritdoc |
||
284 | */ |
||
285 | 3 | public function writeStream($path, $resource, Config $config) |
|
289 | } |
||
290 |
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.