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 $teamMemberId; |
||
| 38 | |||
| 39 | /** @var string */ |
||
| 40 | protected $appKey; |
||
| 41 | |||
| 42 | /** @var string */ |
||
| 43 | protected $appSecret; |
||
| 44 | |||
| 45 | /** @var \GuzzleHttp\Client */ |
||
| 46 | protected $client; |
||
| 47 | |||
| 48 | /** @var int */ |
||
| 49 | protected $maxChunkSize; |
||
| 50 | |||
| 51 | /** @var int */ |
||
| 52 | protected $maxUploadChunkRetries; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param string|array|null $accessTokenOrAppCredentials |
||
| 56 | * @param GuzzleClient|null $client |
||
| 57 | * @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). |
||
| 58 | * @param int $maxUploadChunkRetries How many times to retry an upload session start or append after RequestException. |
||
| 59 | * @param string $teamMemberID The team member ID to be specified for Dropbox business accounts |
||
|
|
|||
| 60 | */ |
||
| 61 | public function __construct($accessTokenOrAppCredentials = null, GuzzleClient $client = null, int $maxChunkSize = self::MAX_CHUNK_SIZE, int $maxUploadChunkRetries = 0, string $teamMemberId = null) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Copy a file or folder to a different location in the user's Dropbox. |
||
| 82 | * |
||
| 83 | * If the source path is a folder all its contents will be copied. |
||
| 84 | * |
||
| 85 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy_v2 |
||
| 86 | */ |
||
| 87 | public function copy(string $fromPath, string $toPath): array |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Create a folder at a given path. |
||
| 99 | * |
||
| 100 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder |
||
| 101 | */ |
||
| 102 | public function createFolder(string $path): array |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Create a shared link with custom settings. |
||
| 117 | * |
||
| 118 | * If no settings are given then the default visibility is RequestedVisibility.public. |
||
| 119 | * The resolved visibility, though, may depend on other aspects such as team and |
||
| 120 | * shared folder settings). Only for paid users. |
||
| 121 | * |
||
| 122 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings |
||
| 123 | */ |
||
| 124 | public function createSharedLinkWithSettings(string $path, array $settings = []): array |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Search a file or folder in the user's Dropbox. |
||
| 139 | * |
||
| 140 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-search |
||
| 141 | */ |
||
| 142 | public function search(string $query, bool $includeHighlights = false): array |
||
| 151 | |||
| 152 | /** |
||
| 153 | * List shared links. |
||
| 154 | * |
||
| 155 | * For empty path returns a list of all shared links. For non-empty path |
||
| 156 | * returns a list of all shared links with access to the given path. |
||
| 157 | * |
||
| 158 | * If direct_only is set true, only direct links to the path will be returned, otherwise |
||
| 159 | * it may return link to the path itself and parent folders as described on docs. |
||
| 160 | * |
||
| 161 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links |
||
| 162 | */ |
||
| 163 | public function listSharedLinks(string $path = null, bool $direct_only = false, string $cursor = null): array |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Delete the file or folder at a given path. |
||
| 178 | * |
||
| 179 | * If the path is a folder, all its contents will be deleted too. |
||
| 180 | * A successful response indicates that the file or folder was deleted. |
||
| 181 | * |
||
| 182 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete |
||
| 183 | */ |
||
| 184 | public function delete(string $path): array |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Download a file from a user's Dropbox. |
||
| 195 | * |
||
| 196 | * @param string $path |
||
| 197 | * |
||
| 198 | * @return resource |
||
| 199 | * |
||
| 200 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download |
||
| 201 | */ |
||
| 202 | public function download(string $path) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Download a folder from the user's Dropbox, as a zip file. |
||
| 215 | * The folder must be less than 20 GB in size and have fewer than 10,000 total files. |
||
| 216 | * The input cannot be a single file. Any single file must be less than 4GB in size. |
||
| 217 | * |
||
| 218 | * @param string $path |
||
| 219 | * |
||
| 220 | * @return resource |
||
| 221 | * |
||
| 222 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download_zip |
||
| 223 | */ |
||
| 224 | public function downloadZip(string $path) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Returns the metadata for a file or folder. |
||
| 237 | * |
||
| 238 | * Note: Metadata for the root folder is unsupported. |
||
| 239 | * |
||
| 240 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata |
||
| 241 | */ |
||
| 242 | public function getMetadata(string $path): array |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get a temporary link to stream content of a file. |
||
| 253 | * |
||
| 254 | * This link will expire in four hours and afterwards you will get 410 Gone. |
||
| 255 | * Content-Type of the link is determined automatically by the file's mime type. |
||
| 256 | * |
||
| 257 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link |
||
| 258 | */ |
||
| 259 | public function getTemporaryLink(string $path): string |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Get a thumbnail for an image. |
||
| 272 | * |
||
| 273 | * This method currently supports files with the following file extensions: |
||
| 274 | * jpg, jpeg, png, tiff, tif, gif and bmp. |
||
| 275 | * |
||
| 276 | * Photos that are larger than 20MB in size won't be converted to a thumbnail. |
||
| 277 | * |
||
| 278 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail |
||
| 279 | */ |
||
| 280 | public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Starts returning the contents of a folder. |
||
| 295 | * |
||
| 296 | * If the result's ListFolderResult.has_more field is true, call |
||
| 297 | * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries. |
||
| 298 | * |
||
| 299 | * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls |
||
| 300 | * with same parameters are made simultaneously by same API app for same user. If your app implements |
||
| 301 | * retry logic, please hold off the retry until the previous request finishes. |
||
| 302 | * |
||
| 303 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder |
||
| 304 | */ |
||
| 305 | public function listFolder(string $path = '', bool $recursive = false): array |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Once a cursor has been retrieved from list_folder, use this to paginate through all files and |
||
| 317 | * retrieve updates to the folder, following the same rules as documented for list_folder. |
||
| 318 | * |
||
| 319 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue |
||
| 320 | */ |
||
| 321 | public function listFolderContinue(string $cursor = ''): array |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Move a file or folder to a different location in the user's Dropbox. |
||
| 328 | * |
||
| 329 | * If the source path is a folder all its contents will be moved. |
||
| 330 | * |
||
| 331 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move_v2 |
||
| 332 | */ |
||
| 333 | public function move(string $fromPath, string $toPath): array |
||
| 342 | |||
| 343 | /** |
||
| 344 | * The file should be uploaded in chunks if it size exceeds the 150 MB threshold |
||
| 345 | * or if the resource size could not be determined (eg. a popen() stream). |
||
| 346 | * |
||
| 347 | * @param string|resource $contents |
||
| 348 | * |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | protected function shouldUploadChunked($contents): bool |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Check if the contents is a pipe stream (not seekable, no size defined). |
||
| 368 | * |
||
| 369 | * @param string|resource $contents |
||
| 370 | * |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | protected function isPipe($contents): bool |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Create a new file with the contents provided in the request. |
||
| 380 | * |
||
| 381 | * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start. |
||
| 382 | * |
||
| 383 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload |
||
| 384 | * |
||
| 385 | * @param string $path |
||
| 386 | * @param string|resource $contents |
||
| 387 | * @param string $mode |
||
| 388 | * |
||
| 389 | * @return array |
||
| 390 | */ |
||
| 391 | public function upload(string $path, $contents, $mode = 'add'): array |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Upload file split in chunks. This allows uploading large files, since |
||
| 413 | * Dropbox API v2 limits the content size to 150MB. |
||
| 414 | * |
||
| 415 | * The chunk size will affect directly the memory usage, so be careful. |
||
| 416 | * Large chunks tends to speed up the upload, while smaller optimizes memory usage. |
||
| 417 | * |
||
| 418 | * @param string $path |
||
| 419 | * @param string|resource $contents |
||
| 420 | * @param string $mode |
||
| 421 | * @param int|null $chunkSize |
||
| 422 | * |
||
| 423 | * @return array |
||
| 424 | */ |
||
| 425 | public function uploadChunked(string $path, $contents, $mode = 'add', $chunkSize = null): array |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param int $type |
||
| 444 | * @param Psr7\Stream $stream |
||
| 445 | * @param int $chunkSize |
||
| 446 | * @param \Spatie\Dropbox\UploadSessionCursor|null $cursor |
||
| 447 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
| 448 | * @throws Exception |
||
| 449 | */ |
||
| 450 | protected function uploadChunk($type, &$stream, $chunkSize, $cursor = null): UploadSessionCursor |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Upload sessions allow you to upload a single file in one or more requests, |
||
| 484 | * for example where the size of the file is greater than 150 MB. |
||
| 485 | * This call starts a new upload session with the given data. |
||
| 486 | * |
||
| 487 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start |
||
| 488 | * |
||
| 489 | * @param string|StreamInterface $contents |
||
| 490 | * @param bool $close |
||
| 491 | * |
||
| 492 | * @return UploadSessionCursor |
||
| 493 | */ |
||
| 494 | public function uploadSessionStart($contents, bool $close = false): UploadSessionCursor |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Append more data to an upload session. |
||
| 508 | * When the parameter close is set, this call will close the session. |
||
| 509 | * A single request should not upload more than 150 MB. |
||
| 510 | * |
||
| 511 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2 |
||
| 512 | * |
||
| 513 | * @param string|StreamInterface $contents |
||
| 514 | * @param UploadSessionCursor $cursor |
||
| 515 | * @param bool $close |
||
| 516 | * |
||
| 517 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
| 518 | */ |
||
| 519 | public function uploadSessionAppend($contents, UploadSessionCursor $cursor, bool $close = false): UploadSessionCursor |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Finish an upload session and save the uploaded data to the given file path. |
||
| 533 | * A single request should not upload more than 150 MB. |
||
| 534 | * |
||
| 535 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish |
||
| 536 | * |
||
| 537 | * @param string|StreamInterface $contents |
||
| 538 | * @param \Spatie\Dropbox\UploadSessionCursor $cursor |
||
| 539 | * @param string $path |
||
| 540 | * @param string|array $mode |
||
| 541 | * @param bool $autorename |
||
| 542 | * @param bool $mute |
||
| 543 | * |
||
| 544 | * @return array |
||
| 545 | */ |
||
| 546 | public function uploadSessionFinish($contents, UploadSessionCursor $cursor, string $path, $mode = 'add', $autorename = false, $mute = false): array |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Get Account Info for current authenticated user. |
||
| 566 | * |
||
| 567 | * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account |
||
| 568 | * |
||
| 569 | * @return array |
||
| 570 | */ |
||
| 571 | public function getAccountInfo(): array |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Revoke current access token. |
||
| 578 | * |
||
| 579 | * @link https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke |
||
| 580 | */ |
||
| 581 | public function revokeToken(): void |
||
| 585 | |||
| 586 | protected function normalizePath(string $path): string |
||
| 596 | |||
| 597 | protected function getEndpointUrl(string $subdomain, string $endpoint): string |
||
| 605 | |||
| 606 | /** |
||
| 607 | * @param string $endpoint |
||
| 608 | * @param array $arguments |
||
| 609 | * @param string|resource|StreamInterface $body |
||
| 610 | * |
||
| 611 | * @return \Psr\Http\Message\ResponseInterface |
||
| 612 | * |
||
| 613 | * @throws \Exception |
||
| 614 | */ |
||
| 615 | public function contentEndpointRequest(string $endpoint, array $arguments, $body = ''): ResponseInterface |
||
| 634 | |||
| 635 | public function rpcEndpointRequest(string $endpoint, array $parameters = null): array |
||
| 653 | |||
| 654 | protected function determineException(ClientException $exception): Exception |
||
| 662 | |||
| 663 | /** |
||
| 664 | * @param $contents |
||
| 665 | * |
||
| 666 | * @return \GuzzleHttp\Psr7\PumpStream|\GuzzleHttp\Psr7\Stream |
||
| 667 | */ |
||
| 668 | protected function getStream($contents) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Get the access token. |
||
| 687 | */ |
||
| 688 | public function getAccessToken(): string |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Set the access token. |
||
| 695 | */ |
||
| 696 | public function setAccessToken(string $accessToken): self |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Get the HTTP headers. |
||
| 705 | */ |
||
| 706 | protected function getHeaders(array $headers = []): array |
||
| 724 | |||
| 725 | /** |
||
| 726 | * @return array |
||
| 727 | */ |
||
| 728 | protected function getHeadersForBearerToken() |
||
| 734 | |||
| 735 | /** |
||
| 736 | * @return array |
||
| 737 | */ |
||
| 738 | protected function getHeadersForCredentials() |
||
| 744 | } |
||
| 745 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.