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 |
||
17 | class Client |
||
18 | { |
||
19 | const THUMBNAIL_FORMAT_JPEG = 'jpeg'; |
||
20 | const THUMBNAIL_FORMAT_PNG = 'png'; |
||
21 | |||
22 | const THUMBNAIL_SIZE_XS = 'w32h32'; |
||
23 | const THUMBNAIL_SIZE_S = 'w64h64'; |
||
24 | const THUMBNAIL_SIZE_M = 'w128h128'; |
||
25 | const THUMBNAIL_SIZE_L = 'w640h480'; |
||
26 | const THUMBNAIL_SIZE_XL = 'w1024h768'; |
||
27 | |||
28 | const MAX_CHUNK_SIZE = 1024 * 1024 * 150; |
||
29 | |||
30 | const UPLOAD_SESSION_START = 0; |
||
31 | const UPLOAD_SESSION_APPEND = 1; |
||
32 | |||
33 | /** @var string */ |
||
34 | protected $accessToken; |
||
35 | |||
36 | /** @var array */ |
||
37 | protected $customHeaders; |
||
38 | |||
39 | /** @var \GuzzleHttp\Client */ |
||
40 | protected $client; |
||
41 | |||
42 | /** @var int */ |
||
43 | protected $maxChunkSize; |
||
44 | |||
45 | /** @var int */ |
||
46 | protected $maxUploadChunkRetries; |
||
47 | |||
48 | /** |
||
49 | * @param string $accessToken |
||
50 | * @param GuzzleClient|null $client |
||
51 | * @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). |
||
52 | * @param int $maxUploadChunkRetries How many times to retry an upload session start or append after RequestException. |
||
53 | */ |
||
54 | public function __construct(string $accessToken, GuzzleClient $client = null, int $maxChunkSize = self::MAX_CHUNK_SIZE, int $maxUploadChunkRetries = 0) |
||
64 | |||
65 | /** |
||
66 | * Copy a file or folder to a different location in the user's Dropbox. |
||
67 | * |
||
68 | * If the source path is a folder all its contents will be copied. |
||
69 | * |
||
70 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy_v2 |
||
71 | */ |
||
72 | public function copy(string $fromPath, string $toPath): array |
||
81 | |||
82 | /** |
||
83 | * Create a folder at a given path. |
||
84 | * |
||
85 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder |
||
86 | */ |
||
87 | public function createFolder(string $path): array |
||
99 | |||
100 | /** |
||
101 | * Create a shared link with custom settings. |
||
102 | * |
||
103 | * If no settings are given then the default visibility is RequestedVisibility.public. |
||
104 | * The resolved visibility, though, may depend on other aspects such as team and |
||
105 | * shared folder settings). Only for paid users. |
||
106 | * |
||
107 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings |
||
108 | */ |
||
109 | public function createSharedLinkWithSettings(string $path, array $settings = []) |
||
121 | |||
122 | /** |
||
123 | * Search a file or folder in the user's Dropbox. |
||
124 | * |
||
125 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-search |
||
126 | */ |
||
127 | public function search(string $query, bool $includeHighlights = false) |
||
136 | |||
137 | /** |
||
138 | * List shared links. |
||
139 | * |
||
140 | * For empty path returns a list of all shared links. For non-empty path |
||
141 | * returns a list of all shared links with access to the given path. |
||
142 | * |
||
143 | * If direct_only is set true, only direct links to the path will be returned, otherwise |
||
144 | * it may return link to the path itself and parent folders as described on docs. |
||
145 | * |
||
146 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links |
||
147 | */ |
||
148 | public function listSharedLinks(string $path = null, bool $direct_only = false, string $cursor = null): array |
||
160 | |||
161 | /** |
||
162 | * Delete the file or folder at a given path. |
||
163 | * |
||
164 | * If the path is a folder, all its contents will be deleted too. |
||
165 | * A successful response indicates that the file or folder was deleted. |
||
166 | * |
||
167 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete |
||
168 | */ |
||
169 | public function delete(string $path): array |
||
177 | |||
178 | /** |
||
179 | * Download a file from a user's Dropbox. |
||
180 | * |
||
181 | * @param string $path |
||
182 | * |
||
183 | * @return resource |
||
184 | * |
||
185 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download |
||
186 | */ |
||
187 | public function download(string $path) |
||
197 | |||
198 | /** |
||
199 | * Returns the metadata for a file or folder. |
||
200 | * |
||
201 | * Note: Metadata for the root folder is unsupported. |
||
202 | * |
||
203 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata |
||
204 | */ |
||
205 | public function getMetadata(string $path): array |
||
213 | |||
214 | /** |
||
215 | * Get a temporary link to stream content of a file. |
||
216 | * |
||
217 | * This link will expire in four hours and afterwards you will get 410 Gone. |
||
218 | * Content-Type of the link is determined automatically by the file's mime type. |
||
219 | * |
||
220 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link |
||
221 | */ |
||
222 | public function getTemporaryLink(string $path): string |
||
232 | |||
233 | /** |
||
234 | * Get a thumbnail for an image. |
||
235 | * |
||
236 | * This method currently supports files with the following file extensions: |
||
237 | * jpg, jpeg, png, tiff, tif, gif and bmp. |
||
238 | * |
||
239 | * Photos that are larger than 20MB in size won't be converted to a thumbnail. |
||
240 | * |
||
241 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail |
||
242 | */ |
||
243 | public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string |
||
255 | |||
256 | /** |
||
257 | * Starts returning the contents of a folder. |
||
258 | * |
||
259 | * If the result's ListFolderResult.has_more field is true, call |
||
260 | * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries. |
||
261 | * |
||
262 | * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls |
||
263 | * with same parameters are made simultaneously by same API app for same user. If your app implements |
||
264 | * retry logic, please hold off the retry until the previous request finishes. |
||
265 | * |
||
266 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder |
||
267 | */ |
||
268 | public function listFolder(string $path = '', bool $recursive = false): array |
||
277 | |||
278 | /** |
||
279 | * Once a cursor has been retrieved from list_folder, use this to paginate through all files and |
||
280 | * retrieve updates to the folder, following the same rules as documented for list_folder. |
||
281 | * |
||
282 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue |
||
283 | */ |
||
284 | public function listFolderContinue(string $cursor = ''): array |
||
288 | |||
289 | /** |
||
290 | * Move a file or folder to a different location in the user's Dropbox. |
||
291 | * |
||
292 | * If the source path is a folder all its contents will be moved. |
||
293 | * |
||
294 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move_v2 |
||
295 | */ |
||
296 | public function move(string $fromPath, string $toPath): array |
||
305 | |||
306 | /** |
||
307 | * The file should be uploaded in chunks if it size exceeds the 150 MB threshold |
||
308 | * or if the resource size could not be determined (eg. a popen() stream). |
||
309 | * |
||
310 | * @param string|resource $contents |
||
311 | * |
||
312 | * @return bool |
||
313 | */ |
||
314 | protected function shouldUploadChunked($contents): bool |
||
328 | |||
329 | /** |
||
330 | * Check if the contents is a pipe stream (not seekable, no size defined). |
||
331 | * |
||
332 | * @param string|resource $contents |
||
333 | * |
||
334 | * @return bool |
||
335 | */ |
||
336 | protected function isPipe($contents): bool |
||
340 | |||
341 | /** |
||
342 | * Create a new file with the contents provided in the request. |
||
343 | * |
||
344 | * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start. |
||
345 | * |
||
346 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload |
||
347 | * |
||
348 | * @param string $path |
||
349 | * @param string|resource $contents |
||
350 | * @param string $mode |
||
351 | * |
||
352 | * @return array |
||
353 | */ |
||
354 | public function upload(string $path, $contents, $mode = 'add'): array |
||
373 | |||
374 | /** |
||
375 | * Upload file split in chunks. This allows uploading large files, since |
||
376 | * Dropbox API v2 limits the content size to 150MB. |
||
377 | * |
||
378 | * The chunk size will affect directly the memory usage, so be careful. |
||
379 | * Large chunks tends to speed up the upload, while smaller optimizes memory usage. |
||
380 | * |
||
381 | * @param string $path |
||
382 | * @param string|resource $contents |
||
383 | * @param string $mode |
||
384 | * @param int $chunkSize |
||
385 | * |
||
386 | * @return array |
||
387 | */ |
||
388 | public function uploadChunked(string $path, $contents, $mode = 'add', $chunkSize = null): array |
||
404 | |||
405 | /** |
||
406 | * @param int $type |
||
407 | * @param Psr7\Stream $stream |
||
408 | * @param int $chunkSize |
||
409 | * @param \Spatie\Dropbox\UploadSessionCursor|null $cursor |
||
410 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
411 | * @throws Exception |
||
412 | */ |
||
413 | protected function uploadChunk($type, &$stream, $chunkSize, $cursor = null): UploadSessionCursor |
||
444 | |||
445 | /** |
||
446 | * Upload sessions allow you to upload a single file in one or more requests, |
||
447 | * for example where the size of the file is greater than 150 MB. |
||
448 | * This call starts a new upload session with the given data. |
||
449 | * |
||
450 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start |
||
451 | * |
||
452 | * @param string|StreamInterface $contents |
||
453 | * @param bool $close |
||
454 | * |
||
455 | * @return UploadSessionCursor |
||
456 | */ |
||
457 | public function uploadSessionStart($contents, bool $close = false): UploadSessionCursor |
||
468 | |||
469 | /** |
||
470 | * Append more data to an upload session. |
||
471 | * When the parameter close is set, this call will close the session. |
||
472 | * A single request should not upload more than 150 MB. |
||
473 | * |
||
474 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2 |
||
475 | * |
||
476 | * @param string|StreamInterface $contents |
||
477 | * @param UploadSessionCursor $cursor |
||
478 | * @param bool $close |
||
479 | * |
||
480 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
481 | */ |
||
482 | public function uploadSessionAppend($contents, UploadSessionCursor $cursor, bool $close = false): UploadSessionCursor |
||
493 | |||
494 | /** |
||
495 | * Finish an upload session and save the uploaded data to the given file path. |
||
496 | * A single request should not upload more than 150 MB. |
||
497 | * |
||
498 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish |
||
499 | * |
||
500 | * @param string|StreamInterface $contents |
||
501 | * @param \Spatie\Dropbox\UploadSessionCursor $cursor |
||
502 | * @param string $path |
||
503 | * @param string|array $mode |
||
504 | * @param bool $autorename |
||
505 | * @param bool $mute |
||
506 | * |
||
507 | * @return array |
||
508 | */ |
||
509 | public function uploadSessionFinish($contents, UploadSessionCursor $cursor, string $path, $mode = 'add', $autorename = false, $mute = false): array |
||
526 | |||
527 | /** |
||
528 | * Get Account Info for current authenticated user. |
||
529 | * |
||
530 | * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account |
||
531 | * |
||
532 | * @return array |
||
533 | */ |
||
534 | public function getAccountInfo(): array |
||
538 | |||
539 | /** |
||
540 | * Revoke current access token. |
||
541 | * |
||
542 | * @link https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke |
||
543 | */ |
||
544 | public function revokeToken() |
||
548 | |||
549 | protected function normalizePath(string $path): string |
||
559 | |||
560 | protected function getEndpointUrl(string $subdomain, string $endpoint): string |
||
568 | |||
569 | /** |
||
570 | * @param string $endpoint |
||
571 | * @param array $arguments |
||
572 | * @param string|resource|StreamInterface $body |
||
573 | * |
||
574 | * @return \Psr\Http\Message\ResponseInterface |
||
575 | * |
||
576 | * @throws \Exception |
||
577 | */ |
||
578 | public function contentEndpointRequest(string $endpoint, array $arguments, $body = ''): ResponseInterface |
||
597 | |||
598 | public function rpcEndpointRequest(string $endpoint, array $parameters = null): array |
||
616 | |||
617 | protected function determineException(ClientException $exception): Exception |
||
625 | |||
626 | /** |
||
627 | * @param $contents |
||
628 | * |
||
629 | * @return \GuzzleHttp\Psr7\PumpStream|\GuzzleHttp\Psr7\Stream |
||
630 | */ |
||
631 | protected function getStream($contents) |
||
647 | |||
648 | /** |
||
649 | * Get the access token. |
||
650 | */ |
||
651 | public function getAccessToken(): string |
||
655 | |||
656 | /** |
||
657 | * Set the access token. |
||
658 | */ |
||
659 | public function setAccessToken(string $accessToken): self |
||
665 | |||
666 | /** |
||
667 | * Get the HTTP headers. |
||
668 | */ |
||
669 | protected function getHeaders(array $headers = []): array |
||
675 | |||
676 | /** |
||
677 | * Set the HTTP headers |
||
678 | */ |
||
679 | public function setHeaders(array $headers = []){ |
||
682 | } |
||
683 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.