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 | * Returns the metadata for a file or folder. |
||
| 196 | * |
||
| 197 | * Note: Metadata for the root folder is unsupported. |
||
| 198 | * |
||
| 199 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata |
||
| 200 | */ |
||
| 201 | public function getMetadata(string $path): array |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get a temporary link to stream content of a file. |
||
| 212 | * |
||
| 213 | * This link will expire in four hours and afterwards you will get 410 Gone. |
||
| 214 | * Content-Type of the link is determined automatically by the file's mime type. |
||
| 215 | * |
||
| 216 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link |
||
| 217 | */ |
||
| 218 | public function getTemporaryLink(string $path): string |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get a thumbnail for an image. |
||
| 231 | * |
||
| 232 | * This method currently supports files with the following file extensions: |
||
| 233 | * jpg, jpeg, png, tiff, tif, gif and bmp. |
||
| 234 | * |
||
| 235 | * Photos that are larger than 20MB in size won't be converted to a thumbnail. |
||
| 236 | * |
||
| 237 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail |
||
| 238 | */ |
||
| 239 | public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Starts returning the contents of a folder. |
||
| 254 | * |
||
| 255 | * If the result's ListFolderResult.has_more field is true, call |
||
| 256 | * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries. |
||
| 257 | * |
||
| 258 | * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls |
||
| 259 | * with same parameters are made simultaneously by same API app for same user. If your app implements |
||
| 260 | * retry logic, please hold off the retry until the previous request finishes. |
||
| 261 | * |
||
| 262 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder |
||
| 263 | */ |
||
| 264 | public function listFolder(string $path = '', bool $recursive = false): array |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Once a cursor has been retrieved from list_folder, use this to paginate through all files and |
||
| 276 | * retrieve updates to the folder, following the same rules as documented for list_folder. |
||
| 277 | * |
||
| 278 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue |
||
| 279 | */ |
||
| 280 | public function listFolderContinue(string $cursor = ''): array |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Move a file or folder to a different location in the user's Dropbox. |
||
| 287 | * |
||
| 288 | * If the source path is a folder all its contents will be moved. |
||
| 289 | * |
||
| 290 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move_v2 |
||
| 291 | */ |
||
| 292 | public function move(string $fromPath, string $toPath): array |
||
| 301 | |||
| 302 | /** |
||
| 303 | * The file should be uploaded in chunks if it size exceeds the 150 MB threshold |
||
| 304 | * or if the resource size could not be determined (eg. a popen() stream). |
||
| 305 | * |
||
| 306 | * @param string|resource $contents |
||
| 307 | * |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | protected function shouldUploadChunked($contents): bool |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Check if the contents is a pipe stream (not seekable, no size defined). |
||
| 327 | * |
||
| 328 | * @param string|resource $contents |
||
| 329 | * |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | protected function isPipe($contents): bool |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Create a new file with the contents provided in the request. |
||
| 339 | * |
||
| 340 | * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start. |
||
| 341 | * |
||
| 342 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload |
||
| 343 | * |
||
| 344 | * @param string $path |
||
| 345 | * @param string|resource $contents |
||
| 346 | * @param string $mode |
||
| 347 | * |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | public function upload(string $path, $contents, $mode = 'add'): array |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Upload file split in chunks. This allows uploading large files, since |
||
| 372 | * Dropbox API v2 limits the content size to 150MB. |
||
| 373 | * |
||
| 374 | * The chunk size will affect directly the memory usage, so be careful. |
||
| 375 | * Large chunks tends to speed up the upload, while smaller optimizes memory usage. |
||
| 376 | * |
||
| 377 | * @param string $path |
||
| 378 | * @param string|resource $contents |
||
| 379 | * @param string $mode |
||
| 380 | * @param int $chunkSize |
||
| 381 | * |
||
| 382 | * @return array |
||
| 383 | */ |
||
| 384 | public function uploadChunked(string $path, $contents, $mode = 'add', $chunkSize = null): array |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param int $type |
||
| 403 | * @param Psr7\Stream $stream |
||
| 404 | * @param int $chunkSize |
||
| 405 | * @param \Spatie\Dropbox\UploadSessionCursor|null $cursor |
||
| 406 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
| 407 | * @throws Exception |
||
| 408 | */ |
||
| 409 | protected function uploadChunk($type, &$stream, $chunkSize, $cursor = null): UploadSessionCursor |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Upload sessions allow you to upload a single file in one or more requests, |
||
| 443 | * for example where the size of the file is greater than 150 MB. |
||
| 444 | * This call starts a new upload session with the given data. |
||
| 445 | * |
||
| 446 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start |
||
| 447 | * |
||
| 448 | * @param string|StreamInterface $contents |
||
| 449 | * @param bool $close |
||
| 450 | * |
||
| 451 | * @return UploadSessionCursor |
||
| 452 | */ |
||
| 453 | public function uploadSessionStart($contents, bool $close = false): UploadSessionCursor |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Append more data to an upload session. |
||
| 467 | * When the parameter close is set, this call will close the session. |
||
| 468 | * A single request should not upload more than 150 MB. |
||
| 469 | * |
||
| 470 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2 |
||
| 471 | * |
||
| 472 | * @param string|StreamInterface $contents |
||
| 473 | * @param UploadSessionCursor $cursor |
||
| 474 | * @param bool $close |
||
| 475 | * |
||
| 476 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
| 477 | */ |
||
| 478 | public function uploadSessionAppend($contents, UploadSessionCursor $cursor, bool $close = false): UploadSessionCursor |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Finish an upload session and save the uploaded data to the given file path. |
||
| 492 | * A single request should not upload more than 150 MB. |
||
| 493 | * |
||
| 494 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish |
||
| 495 | * |
||
| 496 | * @param string|StreamInterface $contents |
||
| 497 | * @param \Spatie\Dropbox\UploadSessionCursor $cursor |
||
| 498 | * @param string $path |
||
| 499 | * @param string|array $mode |
||
| 500 | * @param bool $autorename |
||
| 501 | * @param bool $mute |
||
| 502 | * |
||
| 503 | * @return array |
||
| 504 | */ |
||
| 505 | public function uploadSessionFinish($contents, UploadSessionCursor $cursor, string $path, $mode = 'add', $autorename = false, $mute = false): array |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Get Account Info for current authenticated user. |
||
| 525 | * |
||
| 526 | * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account |
||
| 527 | * |
||
| 528 | * @return array |
||
| 529 | */ |
||
| 530 | public function getAccountInfo(): array |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Revoke current access token. |
||
| 537 | * |
||
| 538 | * @link https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke |
||
| 539 | */ |
||
| 540 | public function revokeToken() |
||
| 544 | |||
| 545 | protected function normalizePath(string $path): string |
||
| 555 | |||
| 556 | protected function getEndpointUrl(string $subdomain, string $endpoint): string |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @param string $endpoint |
||
| 567 | * @param array $arguments |
||
| 568 | * @param string|resource|StreamInterface $body |
||
| 569 | * |
||
| 570 | * @return \Psr\Http\Message\ResponseInterface |
||
| 571 | * |
||
| 572 | * @throws \Exception |
||
| 573 | */ |
||
| 574 | public function contentEndpointRequest(string $endpoint, array $arguments, $body = ''): ResponseInterface |
||
| 593 | |||
| 594 | public function rpcEndpointRequest(string $endpoint, array $parameters = null): array |
||
| 612 | |||
| 613 | protected function determineException(ClientException $exception): Exception |
||
| 621 | |||
| 622 | /** |
||
| 623 | * @param $contents |
||
| 624 | * |
||
| 625 | * @return \GuzzleHttp\Psr7\PumpStream|\GuzzleHttp\Psr7\Stream |
||
| 626 | */ |
||
| 627 | protected function getStream($contents) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Get the access token. |
||
| 646 | */ |
||
| 647 | public function getAccessToken(): string |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Set the access token. |
||
| 654 | */ |
||
| 655 | public function setAccessToken(string $accessToken): self |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Get the HTTP headers. |
||
| 664 | */ |
||
| 665 | protected function getHeaders(array $headers = []): array |
||
| 671 | } |
||
| 672 |
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.