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 \GuzzleHttp\Client */ |
||
| 37 | protected $client; |
||
| 38 | |||
| 39 | /** @var int */ |
||
| 40 | protected $maxChunkSize; |
||
| 41 | |||
| 42 | /** @var int */ |
||
| 43 | protected $maxUploadChunkRetries; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param string $accessToken |
||
| 47 | * @param GuzzleClient|null $client |
||
| 48 | * @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). |
||
| 49 | * @param int $maxUploadChunkRetries How many times to retry an upload session start or append after RequestException. |
||
| 50 | */ |
||
| 51 | public function __construct(string $accessToken, GuzzleClient $client = null, int $maxChunkSize = self::MAX_CHUNK_SIZE, int $maxUploadChunkRetries = 0) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Copy a file or folder to a different location in the user's Dropbox. |
||
| 63 | * |
||
| 64 | * If the source path is a folder all its contents will be copied. |
||
| 65 | * |
||
| 66 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy_v2 |
||
| 67 | */ |
||
| 68 | public function copy(string $fromPath, string $toPath): array |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Create a folder at a given path. |
||
| 80 | * |
||
| 81 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder |
||
| 82 | */ |
||
| 83 | public function createFolder(string $path): array |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Create a shared link with custom settings. |
||
| 98 | * |
||
| 99 | * If no settings are given then the default visibility is RequestedVisibility.public. |
||
| 100 | * The resolved visibility, though, may depend on other aspects such as team and |
||
| 101 | * shared folder settings). Only for paid users. |
||
| 102 | * |
||
| 103 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings |
||
| 104 | */ |
||
| 105 | public function createSharedLinkWithSettings(string $path, array $settings = []) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Search a file or folder in the user's Dropbox. |
||
| 120 | * |
||
| 121 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-search |
||
| 122 | */ |
||
| 123 | public function search(string $query, bool $includeHighlights = false) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * List shared links. |
||
| 135 | * |
||
| 136 | * For empty path returns a list of all shared links. For non-empty path |
||
| 137 | * returns a list of all shared links with access to the given path. |
||
| 138 | * |
||
| 139 | * If direct_only is set true, only direct links to the path will be returned, otherwise |
||
| 140 | * it may return link to the path itself and parent folders as described on docs. |
||
| 141 | * |
||
| 142 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links |
||
| 143 | */ |
||
| 144 | public function listSharedLinks(string $path = null, bool $direct_only = false, string $cursor = null): array |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Delete the file or folder at a given path. |
||
| 159 | * |
||
| 160 | * If the path is a folder, all its contents will be deleted too. |
||
| 161 | * A successful response indicates that the file or folder was deleted. |
||
| 162 | * |
||
| 163 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete |
||
| 164 | */ |
||
| 165 | public function delete(string $path): array |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Download a file from a user's Dropbox. |
||
| 176 | * |
||
| 177 | * @param string $path |
||
| 178 | * |
||
| 179 | * @return resource |
||
| 180 | * |
||
| 181 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download |
||
| 182 | */ |
||
| 183 | public function download(string $path) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Download a folder from the user's Dropbox, as a zip file. |
||
| 196 | * The folder must be less than 20 GB in size and have fewer than 10,000 total files. |
||
| 197 | * The input cannot be a single file. Any single file must be less than 4GB in size. |
||
| 198 | * |
||
| 199 | * @param string $path |
||
| 200 | * |
||
| 201 | * @return resource |
||
| 202 | * |
||
| 203 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download_zip |
||
| 204 | */ |
||
| 205 | public function downloadZip(string $path) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Returns the metadata for a file or folder. |
||
| 218 | * |
||
| 219 | * Note: Metadata for the root folder is unsupported. |
||
| 220 | * |
||
| 221 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata |
||
| 222 | */ |
||
| 223 | public function getMetadata(string $path): array |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get a temporary link to stream content of a file. |
||
| 234 | * |
||
| 235 | * This link will expire in four hours and afterwards you will get 410 Gone. |
||
| 236 | * Content-Type of the link is determined automatically by the file's mime type. |
||
| 237 | * |
||
| 238 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link |
||
| 239 | */ |
||
| 240 | public function getTemporaryLink(string $path): string |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get a thumbnail for an image. |
||
| 253 | * |
||
| 254 | * This method currently supports files with the following file extensions: |
||
| 255 | * jpg, jpeg, png, tiff, tif, gif and bmp. |
||
| 256 | * |
||
| 257 | * Photos that are larger than 20MB in size won't be converted to a thumbnail. |
||
| 258 | * |
||
| 259 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail |
||
| 260 | */ |
||
| 261 | public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Starts returning the contents of a folder. |
||
| 276 | * |
||
| 277 | * If the result's ListFolderResult.has_more field is true, call |
||
| 278 | * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries. |
||
| 279 | * |
||
| 280 | * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls |
||
| 281 | * with same parameters are made simultaneously by same API app for same user. If your app implements |
||
| 282 | * retry logic, please hold off the retry until the previous request finishes. |
||
| 283 | * |
||
| 284 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder |
||
| 285 | */ |
||
| 286 | public function listFolder(string $path = '', bool $recursive = false): array |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Once a cursor has been retrieved from list_folder, use this to paginate through all files and |
||
| 298 | * retrieve updates to the folder, following the same rules as documented for list_folder. |
||
| 299 | * |
||
| 300 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue |
||
| 301 | */ |
||
| 302 | public function listFolderContinue(string $cursor = ''): array |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Move a file or folder to a different location in the user's Dropbox. |
||
| 309 | * |
||
| 310 | * If the source path is a folder all its contents will be moved. |
||
| 311 | * |
||
| 312 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move_v2 |
||
| 313 | */ |
||
| 314 | public function move(string $fromPath, string $toPath): array |
||
| 323 | |||
| 324 | /** |
||
| 325 | * The file should be uploaded in chunks if it size exceeds the 150 MB threshold |
||
| 326 | * or if the resource size could not be determined (eg. a popen() stream). |
||
| 327 | * |
||
| 328 | * @param string|resource $contents |
||
| 329 | * |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | protected function shouldUploadChunked($contents): bool |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Check if the contents is a pipe stream (not seekable, no size defined). |
||
| 349 | * |
||
| 350 | * @param string|resource $contents |
||
| 351 | * |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | protected function isPipe($contents): bool |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Create a new file with the contents provided in the request. |
||
| 361 | * |
||
| 362 | * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start. |
||
| 363 | * |
||
| 364 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload |
||
| 365 | * |
||
| 366 | * @param string $path |
||
| 367 | * @param string|resource $contents |
||
| 368 | * @param string $mode |
||
| 369 | * |
||
| 370 | * @return array |
||
| 371 | */ |
||
| 372 | public function upload(string $path, $contents, $mode = 'add'): array |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Upload file split in chunks. This allows uploading large files, since |
||
| 394 | * Dropbox API v2 limits the content size to 150MB. |
||
| 395 | * |
||
| 396 | * The chunk size will affect directly the memory usage, so be careful. |
||
| 397 | * Large chunks tends to speed up the upload, while smaller optimizes memory usage. |
||
| 398 | * |
||
| 399 | * @param string $path |
||
| 400 | * @param string|resource $contents |
||
| 401 | * @param string $mode |
||
| 402 | * @param int $chunkSize |
||
| 403 | * |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | public function uploadChunked(string $path, $contents, $mode = 'add', $chunkSize = null): array |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param int $type |
||
| 425 | * @param Psr7\Stream $stream |
||
| 426 | * @param int $chunkSize |
||
| 427 | * @param \Spatie\Dropbox\UploadSessionCursor|null $cursor |
||
| 428 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
| 429 | * @throws Exception |
||
| 430 | */ |
||
| 431 | protected function uploadChunk($type, &$stream, $chunkSize, $cursor = null): UploadSessionCursor |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Upload sessions allow you to upload a single file in one or more requests, |
||
| 465 | * for example where the size of the file is greater than 150 MB. |
||
| 466 | * This call starts a new upload session with the given data. |
||
| 467 | * |
||
| 468 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start |
||
| 469 | * |
||
| 470 | * @param string|StreamInterface $contents |
||
| 471 | * @param bool $close |
||
| 472 | * |
||
| 473 | * @return UploadSessionCursor |
||
| 474 | */ |
||
| 475 | public function uploadSessionStart($contents, bool $close = false): UploadSessionCursor |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Append more data to an upload session. |
||
| 489 | * When the parameter close is set, this call will close the session. |
||
| 490 | * A single request should not upload more than 150 MB. |
||
| 491 | * |
||
| 492 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2 |
||
| 493 | * |
||
| 494 | * @param string|StreamInterface $contents |
||
| 495 | * @param UploadSessionCursor $cursor |
||
| 496 | * @param bool $close |
||
| 497 | * |
||
| 498 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
| 499 | */ |
||
| 500 | public function uploadSessionAppend($contents, UploadSessionCursor $cursor, bool $close = false): UploadSessionCursor |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Finish an upload session and save the uploaded data to the given file path. |
||
| 514 | * A single request should not upload more than 150 MB. |
||
| 515 | * |
||
| 516 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish |
||
| 517 | * |
||
| 518 | * @param string|StreamInterface $contents |
||
| 519 | * @param \Spatie\Dropbox\UploadSessionCursor $cursor |
||
| 520 | * @param string $path |
||
| 521 | * @param string|array $mode |
||
| 522 | * @param bool $autorename |
||
| 523 | * @param bool $mute |
||
| 524 | * |
||
| 525 | * @return array |
||
| 526 | */ |
||
| 527 | public function uploadSessionFinish($contents, UploadSessionCursor $cursor, string $path, $mode = 'add', $autorename = false, $mute = false): array |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Get Account Info for current authenticated user. |
||
| 547 | * |
||
| 548 | * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account |
||
| 549 | * |
||
| 550 | * @return array |
||
| 551 | */ |
||
| 552 | public function getAccountInfo(): array |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Revoke current access token. |
||
| 559 | * |
||
| 560 | * @link https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke |
||
| 561 | */ |
||
| 562 | public function revokeToken() |
||
| 566 | |||
| 567 | protected function normalizePath(string $path): string |
||
| 577 | |||
| 578 | protected function getEndpointUrl(string $subdomain, string $endpoint): string |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @param string $endpoint |
||
| 589 | * @param array $arguments |
||
| 590 | * @param string|resource|StreamInterface $body |
||
| 591 | * |
||
| 592 | * @return \Psr\Http\Message\ResponseInterface |
||
| 593 | * |
||
| 594 | * @throws \Exception |
||
| 595 | */ |
||
| 596 | public function contentEndpointRequest(string $endpoint, array $arguments, $body = ''): ResponseInterface |
||
| 615 | |||
| 616 | public function rpcEndpointRequest(string $endpoint, array $parameters = null): array |
||
| 634 | |||
| 635 | protected function determineException(ClientException $exception): Exception |
||
| 643 | |||
| 644 | /** |
||
| 645 | * @param $contents |
||
| 646 | * |
||
| 647 | * @return \GuzzleHttp\Psr7\PumpStream|\GuzzleHttp\Psr7\Stream |
||
| 648 | */ |
||
| 649 | protected function getStream($contents) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get the access token. |
||
| 668 | */ |
||
| 669 | public function getAccessToken(): string |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Set the access token. |
||
| 676 | */ |
||
| 677 | public function setAccessToken(string $accessToken): self |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Get the HTTP headers. |
||
| 686 | */ |
||
| 687 | protected function getHeaders(array $headers = []): array |
||
| 693 | } |
||
| 694 |
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.