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 string */ |
||
37 | protected $appKey; |
||
38 | |||
39 | /** @var string */ |
||
40 | protected $appSecret; |
||
41 | |||
42 | /** @var \GuzzleHttp\Client */ |
||
43 | protected $client; |
||
44 | |||
45 | /** @var int */ |
||
46 | protected $maxChunkSize; |
||
47 | |||
48 | /** @var int */ |
||
49 | protected $maxUploadChunkRetries; |
||
50 | |||
51 | /** |
||
52 | * @param string|array|null $accessTokenOrAppCredentials |
||
53 | * @param GuzzleClient|null $client |
||
54 | * @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). |
||
55 | * @param int $maxUploadChunkRetries How many times to retry an upload session start or append after RequestException. |
||
56 | */ |
||
57 | public function __construct($accessTokenOrAppCredentials = null, GuzzleClient $client = null, int $maxChunkSize = self::MAX_CHUNK_SIZE, int $maxUploadChunkRetries = 0) |
||
71 | |||
72 | /** |
||
73 | * Copy a file or folder to a different location in the user's Dropbox. |
||
74 | * |
||
75 | * If the source path is a folder all its contents will be copied. |
||
76 | * |
||
77 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy_v2 |
||
78 | */ |
||
79 | public function copy(string $fromPath, string $toPath): array |
||
88 | |||
89 | /** |
||
90 | * Create a folder at a given path. |
||
91 | * |
||
92 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder |
||
93 | */ |
||
94 | public function createFolder(string $path): array |
||
106 | |||
107 | /** |
||
108 | * Create a shared link with custom settings. |
||
109 | * |
||
110 | * If no settings are given then the default visibility is RequestedVisibility.public. |
||
111 | * The resolved visibility, though, may depend on other aspects such as team and |
||
112 | * shared folder settings). Only for paid users. |
||
113 | * |
||
114 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings |
||
115 | */ |
||
116 | public function createSharedLinkWithSettings(string $path, array $settings = []): array |
||
128 | |||
129 | /** |
||
130 | * Search a file or folder in the user's Dropbox. |
||
131 | * |
||
132 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-search |
||
133 | */ |
||
134 | public function search(string $query, bool $includeHighlights = false): array |
||
143 | |||
144 | /** |
||
145 | * List shared links. |
||
146 | * |
||
147 | * For empty path returns a list of all shared links. For non-empty path |
||
148 | * returns a list of all shared links with access to the given path. |
||
149 | * |
||
150 | * If direct_only is set true, only direct links to the path will be returned, otherwise |
||
151 | * it may return link to the path itself and parent folders as described on docs. |
||
152 | * |
||
153 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links |
||
154 | */ |
||
155 | public function listSharedLinks(string $path = null, bool $direct_only = false, string $cursor = null): array |
||
167 | |||
168 | /** |
||
169 | * Delete the file or folder at a given path. |
||
170 | * |
||
171 | * If the path is a folder, all its contents will be deleted too. |
||
172 | * A successful response indicates that the file or folder was deleted. |
||
173 | * |
||
174 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete |
||
175 | */ |
||
176 | public function delete(string $path): array |
||
184 | |||
185 | /** |
||
186 | * Download a file from a user's Dropbox. |
||
187 | * |
||
188 | * @param string $path |
||
189 | * |
||
190 | * @return resource |
||
191 | * |
||
192 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download |
||
193 | */ |
||
194 | public function download(string $path) |
||
204 | |||
205 | /** |
||
206 | * Download a folder from the user's Dropbox, as a zip file. |
||
207 | * The folder must be less than 20 GB in size and have fewer than 10,000 total files. |
||
208 | * The input cannot be a single file. Any single file must be less than 4GB in size. |
||
209 | * |
||
210 | * @param string $path |
||
211 | * |
||
212 | * @return resource |
||
213 | * |
||
214 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download_zip |
||
215 | */ |
||
216 | public function downloadZip(string $path) |
||
226 | |||
227 | /** |
||
228 | * Returns the metadata for a file or folder. |
||
229 | * |
||
230 | * Note: Metadata for the root folder is unsupported. |
||
231 | * |
||
232 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata |
||
233 | */ |
||
234 | public function getMetadata(string $path): array |
||
242 | |||
243 | /** |
||
244 | * Get a temporary link to stream content of a file. |
||
245 | * |
||
246 | * This link will expire in four hours and afterwards you will get 410 Gone. |
||
247 | * Content-Type of the link is determined automatically by the file's mime type. |
||
248 | * |
||
249 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link |
||
250 | */ |
||
251 | public function getTemporaryLink(string $path): string |
||
261 | |||
262 | /** |
||
263 | * Get a thumbnail for an image. |
||
264 | * |
||
265 | * This method currently supports files with the following file extensions: |
||
266 | * jpg, jpeg, png, tiff, tif, gif and bmp. |
||
267 | * |
||
268 | * Photos that are larger than 20MB in size won't be converted to a thumbnail. |
||
269 | * |
||
270 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail |
||
271 | */ |
||
272 | public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string |
||
284 | |||
285 | /** |
||
286 | * Starts returning the contents of a folder. |
||
287 | * |
||
288 | * If the result's ListFolderResult.has_more field is true, call |
||
289 | * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries. |
||
290 | * |
||
291 | * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls |
||
292 | * with same parameters are made simultaneously by same API app for same user. If your app implements |
||
293 | * retry logic, please hold off the retry until the previous request finishes. |
||
294 | * |
||
295 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder |
||
296 | */ |
||
297 | public function listFolder(string $path = '', bool $recursive = false): array |
||
306 | |||
307 | /** |
||
308 | * Once a cursor has been retrieved from list_folder, use this to paginate through all files and |
||
309 | * retrieve updates to the folder, following the same rules as documented for list_folder. |
||
310 | * |
||
311 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue |
||
312 | */ |
||
313 | public function listFolderContinue(string $cursor = ''): array |
||
317 | |||
318 | /** |
||
319 | * Move a file or folder to a different location in the user's Dropbox. |
||
320 | * |
||
321 | * If the source path is a folder all its contents will be moved. |
||
322 | * |
||
323 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move_v2 |
||
324 | */ |
||
325 | public function move(string $fromPath, string $toPath): array |
||
334 | |||
335 | /** |
||
336 | * The file should be uploaded in chunks if it size exceeds the 150 MB threshold |
||
337 | * or if the resource size could not be determined (eg. a popen() stream). |
||
338 | * |
||
339 | * @param string|resource $contents |
||
340 | * |
||
341 | * @return bool |
||
342 | */ |
||
343 | protected function shouldUploadChunked($contents): bool |
||
357 | |||
358 | /** |
||
359 | * Check if the contents is a pipe stream (not seekable, no size defined). |
||
360 | * |
||
361 | * @param string|resource $contents |
||
362 | * |
||
363 | * @return bool |
||
364 | */ |
||
365 | protected function isPipe($contents): bool |
||
369 | |||
370 | /** |
||
371 | * Create a new file with the contents provided in the request. |
||
372 | * |
||
373 | * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start. |
||
374 | * |
||
375 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload |
||
376 | * |
||
377 | * @param string $path |
||
378 | * @param string|resource $contents |
||
379 | * @param string $mode |
||
380 | * |
||
381 | * @return array |
||
382 | */ |
||
383 | public function upload(string $path, $contents, $mode = 'add'): array |
||
402 | |||
403 | /** |
||
404 | * Upload file split in chunks. This allows uploading large files, since |
||
405 | * Dropbox API v2 limits the content size to 150MB. |
||
406 | * |
||
407 | * The chunk size will affect directly the memory usage, so be careful. |
||
408 | * Large chunks tends to speed up the upload, while smaller optimizes memory usage. |
||
409 | * |
||
410 | * @param string $path |
||
411 | * @param string|resource $contents |
||
412 | * @param string $mode |
||
413 | * @param int|null $chunkSize |
||
414 | * |
||
415 | * @return array |
||
416 | */ |
||
417 | public function uploadChunked(string $path, $contents, $mode = 'add', $chunkSize = null): array |
||
433 | |||
434 | /** |
||
435 | * @param int $type |
||
436 | * @param Psr7\Stream $stream |
||
437 | * @param int $chunkSize |
||
438 | * @param \Spatie\Dropbox\UploadSessionCursor|null $cursor |
||
439 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
440 | * @throws Exception |
||
441 | */ |
||
442 | protected function uploadChunk($type, &$stream, $chunkSize, $cursor = null): UploadSessionCursor |
||
473 | |||
474 | /** |
||
475 | * Upload sessions allow you to upload a single file in one or more requests, |
||
476 | * for example where the size of the file is greater than 150 MB. |
||
477 | * This call starts a new upload session with the given data. |
||
478 | * |
||
479 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start |
||
480 | * |
||
481 | * @param string|StreamInterface $contents |
||
482 | * @param bool $close |
||
483 | * |
||
484 | * @return UploadSessionCursor |
||
485 | */ |
||
486 | public function uploadSessionStart($contents, bool $close = false): UploadSessionCursor |
||
497 | |||
498 | /** |
||
499 | * Append more data to an upload session. |
||
500 | * When the parameter close is set, this call will close the session. |
||
501 | * A single request should not upload more than 150 MB. |
||
502 | * |
||
503 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2 |
||
504 | * |
||
505 | * @param string|StreamInterface $contents |
||
506 | * @param UploadSessionCursor $cursor |
||
507 | * @param bool $close |
||
508 | * |
||
509 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
510 | */ |
||
511 | public function uploadSessionAppend($contents, UploadSessionCursor $cursor, bool $close = false): UploadSessionCursor |
||
522 | |||
523 | /** |
||
524 | * Finish an upload session and save the uploaded data to the given file path. |
||
525 | * A single request should not upload more than 150 MB. |
||
526 | * |
||
527 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish |
||
528 | * |
||
529 | * @param string|StreamInterface $contents |
||
530 | * @param \Spatie\Dropbox\UploadSessionCursor $cursor |
||
531 | * @param string $path |
||
532 | * @param string|array $mode |
||
533 | * @param bool $autorename |
||
534 | * @param bool $mute |
||
535 | * |
||
536 | * @return array |
||
537 | */ |
||
538 | public function uploadSessionFinish($contents, UploadSessionCursor $cursor, string $path, $mode = 'add', $autorename = false, $mute = false): array |
||
555 | |||
556 | /** |
||
557 | * Get Account Info for current authenticated user. |
||
558 | * |
||
559 | * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account |
||
560 | * |
||
561 | * @return array |
||
562 | */ |
||
563 | public function getAccountInfo(): array |
||
567 | |||
568 | /** |
||
569 | * Revoke current access token. |
||
570 | * |
||
571 | * @link https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke |
||
572 | */ |
||
573 | public function revokeToken(): void |
||
577 | |||
578 | protected function normalizePath(string $path): string |
||
588 | |||
589 | protected function getEndpointUrl(string $subdomain, string $endpoint): string |
||
597 | |||
598 | /** |
||
599 | * @param string $endpoint |
||
600 | * @param array $arguments |
||
601 | * @param string|resource|StreamInterface $body |
||
602 | * |
||
603 | * @return \Psr\Http\Message\ResponseInterface |
||
604 | * |
||
605 | * @throws \Exception |
||
606 | */ |
||
607 | public function contentEndpointRequest(string $endpoint, array $arguments, $body = ''): ResponseInterface |
||
626 | |||
627 | public function rpcEndpointRequest(string $endpoint, array $parameters = null): array |
||
645 | |||
646 | protected function determineException(ClientException $exception): Exception |
||
654 | |||
655 | /** |
||
656 | * @param $contents |
||
657 | * |
||
658 | * @return \GuzzleHttp\Psr7\PumpStream|\GuzzleHttp\Psr7\Stream |
||
659 | */ |
||
660 | protected function getStream($contents) |
||
676 | |||
677 | /** |
||
678 | * Get the access token. |
||
679 | */ |
||
680 | public function getAccessToken(): string |
||
684 | |||
685 | /** |
||
686 | * Set the access token. |
||
687 | */ |
||
688 | public function setAccessToken(string $accessToken): self |
||
694 | |||
695 | /** |
||
696 | * Get the HTTP headers. |
||
697 | */ |
||
698 | protected function getHeaders(array $headers = []): array |
||
707 | |||
708 | /** |
||
709 | * @return array |
||
710 | */ |
||
711 | protected function getHeadersForBearerToken() |
||
717 | |||
718 | /** |
||
719 | * @return array |
||
720 | */ |
||
721 | protected function getHeadersForCredentials() |
||
727 | } |
||
728 |
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.