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:
Complex classes like GuzzleAdapter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GuzzleAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class GuzzleAdapter implements AdapterInterface |
||
17 | { |
||
18 | /** |
||
19 | * Whether this endpoint supports head requests. |
||
20 | * |
||
21 | * @var bool |
||
22 | */ |
||
23 | protected $supportsHead = true; |
||
24 | |||
25 | /** |
||
26 | * The base URL. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $base; |
||
31 | |||
32 | /** |
||
33 | * The Guzzle HTTP client. |
||
34 | * |
||
35 | * @var \GuzzleHttp\ClientInterface |
||
36 | */ |
||
37 | protected $client; |
||
38 | |||
39 | /** |
||
40 | * The visibility of this adapter. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $visibility = AdapterInterface::VISIBILITY_PUBLIC; |
||
45 | |||
46 | /** |
||
47 | * Constructs a GuzzleAdapter object. |
||
48 | * |
||
49 | * @param string $base The base URL. |
||
50 | * @param \GuzzleHttp\ClientInterface $client An optional Guzzle client. |
||
51 | */ |
||
52 | 3 | public function __construct($base, ClientInterface $client = null) |
|
76 | |||
77 | /** |
||
78 | * Returns the base URL. |
||
79 | * |
||
80 | * @return string The base URL. |
||
81 | */ |
||
82 | 3 | public function getBaseUrl() |
|
86 | |||
87 | /** |
||
88 | * @inheritdoc |
||
89 | */ |
||
90 | 3 | public function copy($path, $newpath) |
|
94 | |||
95 | /** |
||
96 | * @inheritdoc |
||
97 | */ |
||
98 | 3 | public function createDir($path, Config $config) |
|
102 | |||
103 | /** |
||
104 | * @inheritdoc |
||
105 | */ |
||
106 | 3 | public function delete($path) |
|
110 | |||
111 | /** |
||
112 | * @inheritdoc |
||
113 | */ |
||
114 | 3 | public function deleteDir($path) |
|
118 | |||
119 | /** |
||
120 | * @inheritdoc |
||
121 | */ |
||
122 | 3 | public function getMetadata($path) |
|
151 | |||
152 | /** |
||
153 | * @inheritdoc |
||
154 | */ |
||
155 | 3 | public function getMimetype($path) |
|
159 | |||
160 | /** |
||
161 | * @inheritdoc |
||
162 | */ |
||
163 | 3 | public function getSize($path) |
|
167 | |||
168 | /** |
||
169 | * @inheritdoc |
||
170 | */ |
||
171 | 3 | public function getTimestamp($path) |
|
175 | |||
176 | /** |
||
177 | * @inheritdoc |
||
178 | */ |
||
179 | 3 | public function getVisibility($path) |
|
186 | |||
187 | /** |
||
188 | * @inheritdoc |
||
189 | */ |
||
190 | 3 | public function has($path) |
|
198 | |||
199 | /** |
||
200 | * @inheritdoc |
||
201 | */ |
||
202 | 3 | public function listContents($directory = '', $recursive = false) |
|
206 | |||
207 | /** |
||
208 | * @inheritdoc |
||
209 | */ |
||
210 | 3 | public function read($path) |
|
221 | |||
222 | /** |
||
223 | * @inheritdoc |
||
224 | */ |
||
225 | 3 | public function readStream($path) |
|
236 | |||
237 | /** |
||
238 | * @inheritdoc |
||
239 | */ |
||
240 | 3 | public function rename($path, $newpath) |
|
244 | |||
245 | /** |
||
246 | * @inheritdoc |
||
247 | */ |
||
248 | 3 | public function setVisibility($path, $visibility) |
|
256 | |||
257 | /** |
||
258 | * @inheritdoc |
||
259 | */ |
||
260 | 3 | public function update($path, $contents, Config $conf) |
|
264 | |||
265 | /** |
||
266 | * @inheritdoc |
||
267 | */ |
||
268 | 3 | public function updateStream($path, $resource, Config $config) |
|
272 | |||
273 | /** |
||
274 | * @inheritdoc |
||
275 | */ |
||
276 | 3 | public function write($path, $contents, Config $config) |
|
280 | |||
281 | /** |
||
282 | * @inheritdoc |
||
283 | */ |
||
284 | 3 | public function writeStream($path, $resource, Config $config) |
|
288 | |||
289 | /** |
||
290 | * Performs a GET request. |
||
291 | * |
||
292 | * @param string $path The path to GET. |
||
293 | * |
||
294 | * @return \GuzzleHttp\Psr7\Response|false The response or false if failed. |
||
295 | */ |
||
296 | 6 | protected function get($path) |
|
310 | |||
311 | /** |
||
312 | * Performs a HEAD request. |
||
313 | * |
||
314 | * @param string $path The path to HEAD. |
||
315 | * |
||
316 | * @return \GuzzleHttp\Psr7\Response|false The response or false if failed. |
||
317 | */ |
||
318 | 6 | protected function head($path) |
|
344 | } |
||
345 |
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.