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 Client 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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Client |
||
17 | { |
||
18 | const THUMBNAIL_FORMAT_JPEG = 'jpeg'; |
||
19 | const THUMBNAIL_FORMAT_PNG = 'png'; |
||
20 | |||
21 | const THUMBNAIL_SIZE_XS = 'w32h32'; |
||
22 | const THUMBNAIL_SIZE_S = 'w64h64'; |
||
23 | const THUMBNAIL_SIZE_M = 'w128h128'; |
||
24 | const THUMBNAIL_SIZE_L = 'w640h480'; |
||
25 | const THUMBNAIL_SIZE_XL = 'w1024h768'; |
||
26 | |||
27 | const MAX_CHUNK_SIZE = 1024 * 1024 * 150; |
||
28 | |||
29 | const UPLOAD_SESSION_START = 0; |
||
30 | const UPLOAD_SESSION_APPEND = 1; |
||
31 | |||
32 | /** @var string */ |
||
33 | protected $accessToken; |
||
34 | |||
35 | /** @var \GuzzleHttp\Client */ |
||
36 | protected $client; |
||
37 | |||
38 | /** @var int */ |
||
39 | protected $maxChunkSize; |
||
40 | |||
41 | /** @var int */ |
||
42 | protected $maxUploadChunkRetries; |
||
43 | |||
44 | /** |
||
45 | * @param string $accessToken |
||
46 | * @param GuzzleClient|null $client |
||
47 | * @param int $maxChunkSize Set max chunk size per request (determines when to switch from "one shot upload" to upload session and defines chunk size for uploads via session). |
||
48 | * @param int $maxUploadChunkRetries How many times to retry an upload session start or append after RequestException. |
||
49 | */ |
||
50 | public function __construct(string $accessToken, GuzzleClient $client = null, int $maxChunkSize = self::MAX_CHUNK_SIZE, int $maxUploadChunkRetries = 0) |
||
59 | |||
60 | /** |
||
61 | * Copy a file or folder to a different location in the user's Dropbox. |
||
62 | * |
||
63 | * If the source path is a folder all its contents will be copied. |
||
64 | * |
||
65 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy_v2 |
||
66 | */ |
||
67 | View Code Duplication | public function copy(string $fromPath, string $toPath): array |
|
76 | |||
77 | /** |
||
78 | * Create a folder at a given path. |
||
79 | * |
||
80 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder |
||
81 | */ |
||
82 | public function createFolder(string $path): array |
||
94 | |||
95 | /** |
||
96 | * Create a shared link with custom settings. |
||
97 | * |
||
98 | * If no settings are given then the default visibility is RequestedVisibility.public. |
||
99 | * The resolved visibility, though, may depend on other aspects such as team and |
||
100 | * shared folder settings). Only for paid users. |
||
101 | * |
||
102 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings |
||
103 | */ |
||
104 | public function createSharedLinkWithSettings(string $path, array $settings = []) |
||
113 | |||
114 | /** |
||
115 | * List shared links. |
||
116 | * |
||
117 | * For empty path returns a list of all shared links. For non-empty path |
||
118 | * returns a list of all shared links with access to the given path. |
||
119 | * |
||
120 | * If direct_only is set true, only direct links to the path will be returned, otherwise |
||
121 | * it may return link to the path itself and parent folders as described on docs. |
||
122 | * |
||
123 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links |
||
124 | */ |
||
125 | public function listSharedLinks(string $path = null, bool $direct_only = false, string $cursor = null): array |
||
137 | |||
138 | /** |
||
139 | * Delete the file or folder at a given path. |
||
140 | * |
||
141 | * If the path is a folder, all its contents will be deleted too. |
||
142 | * A successful response indicates that the file or folder was deleted. |
||
143 | * |
||
144 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete |
||
145 | */ |
||
146 | public function delete(string $path): array |
||
154 | |||
155 | /** |
||
156 | * Download a file from a user's Dropbox. |
||
157 | * |
||
158 | * @param string $path |
||
159 | * |
||
160 | * @return resource |
||
161 | * |
||
162 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download |
||
163 | */ |
||
164 | public function download(string $path) |
||
174 | |||
175 | /** |
||
176 | * Returns the metadata for a file or folder. |
||
177 | * |
||
178 | * Note: Metadata for the root folder is unsupported. |
||
179 | * |
||
180 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata |
||
181 | */ |
||
182 | public function getMetadata(string $path): array |
||
190 | |||
191 | /** |
||
192 | * Get a temporary link to stream content of a file. |
||
193 | * |
||
194 | * This link will expire in four hours and afterwards you will get 410 Gone. |
||
195 | * Content-Type of the link is determined automatically by the file's mime type. |
||
196 | * |
||
197 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link |
||
198 | */ |
||
199 | View Code Duplication | public function getTemporaryLink(string $path): string |
|
209 | |||
210 | /** |
||
211 | * Get a thumbnail for an image. |
||
212 | * |
||
213 | * This method currently supports files with the following file extensions: |
||
214 | * jpg, jpeg, png, tiff, tif, gif and bmp. |
||
215 | * |
||
216 | * Photos that are larger than 20MB in size won't be converted to a thumbnail. |
||
217 | * |
||
218 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail |
||
219 | */ |
||
220 | public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string |
||
232 | |||
233 | /** |
||
234 | * Starts returning the contents of a folder. |
||
235 | * |
||
236 | * If the result's ListFolderResult.has_more field is true, call |
||
237 | * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries. |
||
238 | * |
||
239 | * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls |
||
240 | * with same parameters are made simultaneously by same API app for same user. If your app implements |
||
241 | * retry logic, please hold off the retry until the previous request finishes. |
||
242 | * |
||
243 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder |
||
244 | */ |
||
245 | View Code Duplication | public function listFolder(string $path = '', bool $recursive = false): array |
|
254 | |||
255 | /** |
||
256 | * Once a cursor has been retrieved from list_folder, use this to paginate through all files and |
||
257 | * retrieve updates to the folder, following the same rules as documented for list_folder. |
||
258 | * |
||
259 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue |
||
260 | */ |
||
261 | public function listFolderContinue(string $cursor = ''): array |
||
265 | |||
266 | /** |
||
267 | * Move a file or folder to a different location in the user's Dropbox. |
||
268 | * |
||
269 | * If the source path is a folder all its contents will be moved. |
||
270 | * |
||
271 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move_v2 |
||
272 | */ |
||
273 | View Code Duplication | public function move(string $fromPath, string $toPath): array |
|
282 | |||
283 | /** |
||
284 | * The file should be uploaded in chunks if it size exceeds the 150 MB threshold |
||
285 | * or if the resource size could not be determined (eg. a popen() stream). |
||
286 | * |
||
287 | * @param string|resource $contents |
||
288 | * |
||
289 | * @return bool |
||
290 | */ |
||
291 | protected function shouldUploadChunked($contents): bool |
||
305 | |||
306 | /** |
||
307 | * Check if the contents is a pipe stream (not seekable, no size defined). |
||
308 | * |
||
309 | * @param string|resource $contents |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | protected function isPipe($contents): bool |
||
317 | |||
318 | /** |
||
319 | * Create a new file with the contents provided in the request. |
||
320 | * |
||
321 | * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start. |
||
322 | * |
||
323 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload |
||
324 | * |
||
325 | * @param string $path |
||
326 | * @param string|resource $contents |
||
327 | * @param string $mode |
||
328 | * |
||
329 | * @return array |
||
330 | */ |
||
331 | public function upload(string $path, $contents, $mode = 'add'): array |
||
350 | |||
351 | /** |
||
352 | * Upload file split in chunks. This allows uploading large files, since |
||
353 | * Dropbox API v2 limits the content size to 150MB. |
||
354 | * |
||
355 | * The chunk size will affect directly the memory usage, so be careful. |
||
356 | * Large chunks tends to speed up the upload, while smaller optimizes memory usage. |
||
357 | * |
||
358 | * @param string $path |
||
359 | * @param string|resource $contents |
||
360 | * @param string $mode |
||
361 | * @param int $chunkSize |
||
362 | * |
||
363 | * @return array |
||
364 | */ |
||
365 | public function uploadChunked(string $path, $contents, $mode = 'add', $chunkSize = null): array |
||
381 | |||
382 | /** |
||
383 | * @param int $type |
||
384 | * @param Psr7\Stream $stream |
||
385 | * @param int $chunkSize |
||
386 | * @param \Spatie\Dropbox\UploadSessionCursor|null $cursor |
||
387 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
388 | * @throws Exception |
||
389 | */ |
||
390 | protected function uploadChunk($type, &$stream, $chunkSize, $cursor = null): UploadSessionCursor |
||
421 | |||
422 | /** |
||
423 | * Upload sessions allow you to upload a single file in one or more requests, |
||
424 | * for example where the size of the file is greater than 150 MB. |
||
425 | * This call starts a new upload session with the given data. |
||
426 | * |
||
427 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start |
||
428 | * |
||
429 | * @param string|StreamInterface $contents |
||
430 | * @param bool $close |
||
431 | * |
||
432 | * @return UploadSessionCursor |
||
433 | */ |
||
434 | public function uploadSessionStart($contents, bool $close = false): UploadSessionCursor |
||
445 | |||
446 | /** |
||
447 | * Append more data to an upload session. |
||
448 | * When the parameter close is set, this call will close the session. |
||
449 | * A single request should not upload more than 150 MB. |
||
450 | * |
||
451 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2 |
||
452 | * |
||
453 | * @param string|StreamInterface $contents |
||
454 | * @param UploadSessionCursor $cursor |
||
455 | * @param bool $close |
||
456 | * |
||
457 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
458 | */ |
||
459 | public function uploadSessionAppend($contents, UploadSessionCursor $cursor, bool $close = false): UploadSessionCursor |
||
470 | |||
471 | /** |
||
472 | * Finish an upload session and save the uploaded data to the given file path. |
||
473 | * A single request should not upload more than 150 MB. |
||
474 | * |
||
475 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish |
||
476 | * |
||
477 | * @param string|StreamInterface $contents |
||
478 | * @param \Spatie\Dropbox\UploadSessionCursor $cursor |
||
479 | * @param string $path |
||
480 | * @param string|array $mode |
||
481 | * @param bool $autorename |
||
482 | * @param bool $mute |
||
483 | * |
||
484 | * @return array |
||
485 | */ |
||
486 | public function uploadSessionFinish($contents, UploadSessionCursor $cursor, string $path, $mode = 'add', $autorename = false, $mute = false): array |
||
503 | |||
504 | /** |
||
505 | * Get Account Info for current authenticated user. |
||
506 | * |
||
507 | * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account |
||
508 | * |
||
509 | * @return array |
||
510 | */ |
||
511 | public function getAccountInfo(): array |
||
515 | |||
516 | /** |
||
517 | * Revoke current access token. |
||
518 | * |
||
519 | * @link https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke |
||
520 | */ |
||
521 | public function revokeToken() |
||
525 | |||
526 | protected function normalizePath(string $path): string |
||
536 | |||
537 | /** |
||
538 | * @param string $endpoint |
||
539 | * @param array $arguments |
||
540 | * @param string|resource|StreamInterface $body |
||
541 | * |
||
542 | * @return \Psr\Http\Message\ResponseInterface |
||
543 | * |
||
544 | * @throws \Exception |
||
545 | */ |
||
546 | public function contentEndpointRequest(string $endpoint, array $arguments, $body = ''): ResponseInterface |
||
565 | |||
566 | public function rpcEndpointRequest(string $endpoint, array $parameters = null): array |
||
584 | |||
585 | protected function determineException(ClientException $exception): Exception |
||
593 | |||
594 | /** |
||
595 | * @param $contents |
||
596 | * |
||
597 | * @return \GuzzleHttp\Psr7\PumpStream|\GuzzleHttp\Psr7\Stream |
||
598 | */ |
||
599 | protected function getStream($contents) |
||
615 | |||
616 | /** |
||
617 | * Get the access token. |
||
618 | */ |
||
619 | public function getAccessToken(): string |
||
623 | |||
624 | /** |
||
625 | * Set the access token. |
||
626 | */ |
||
627 | public function setAccessToken(string $accessToken): self |
||
633 | |||
634 | /** |
||
635 | * Get the HTTP headers. |
||
636 | */ |
||
637 | protected function getHeaders(array $headers = []): array |
||
643 | } |
||
644 |
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.