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 |
||
| 19 | class Client |
||
| 20 | { |
||
| 21 | const THUMBNAIL_FORMAT_JPEG = 'jpeg'; |
||
| 22 | const THUMBNAIL_FORMAT_PNG = 'png'; |
||
| 23 | |||
| 24 | const THUMBNAIL_SIZE_XS = 'w32h32'; |
||
| 25 | const THUMBNAIL_SIZE_S = 'w64h64'; |
||
| 26 | const THUMBNAIL_SIZE_M = 'w128h128'; |
||
| 27 | const THUMBNAIL_SIZE_L = 'w640h480'; |
||
| 28 | const THUMBNAIL_SIZE_XL = 'w1024h768'; |
||
| 29 | |||
| 30 | const MAX_CHUNK_SIZE = 1024 * 1024 * 150; |
||
| 31 | |||
| 32 | const RETRY_BACKOFF = 1000; |
||
| 33 | const RETRY_CODES = [429]; |
||
| 34 | |||
| 35 | const UPLOAD_SESSION_START = 0; |
||
| 36 | const UPLOAD_SESSION_APPEND = 1; |
||
| 37 | |||
| 38 | /** @var string */ |
||
| 39 | protected $accessToken; |
||
| 40 | |||
| 41 | /** @var \GuzzleHttp\Client */ |
||
| 42 | protected $client; |
||
| 43 | |||
| 44 | /** @var int */ |
||
| 45 | protected $maxChunkSize; |
||
| 46 | |||
| 47 | /** @var int */ |
||
| 48 | protected $maxUploadChunkRetries; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param string $accessToken |
||
| 52 | * @param GuzzleClient|null $client |
||
| 53 | * @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). |
||
| 54 | * @param int $maxUploadChunkRetries How many times to retry an upload session start or append after RequestException. |
||
| 55 | */ |
||
| 56 | public function __construct(string $accessToken, GuzzleClient $client = null, int $maxChunkSize = self::MAX_CHUNK_SIZE, int $maxUploadChunkRetries = 0) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Create a new guzzle handler stack. |
||
| 68 | * |
||
| 69 | * @return \GuzzleHttp\HandlerStack |
||
| 70 | */ |
||
| 71 | protected static function createHandler() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Copy a file or folder to a different location in the user's Dropbox. |
||
| 86 | * |
||
| 87 | * If the source path is a folder all its contents will be copied. |
||
| 88 | * |
||
| 89 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy_v2 |
||
| 90 | */ |
||
| 91 | public function copy(string $fromPath, string $toPath): array |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Create a folder at a given path. |
||
| 103 | * |
||
| 104 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder |
||
| 105 | */ |
||
| 106 | public function createFolder(string $path): array |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Create a shared link with custom settings. |
||
| 121 | * |
||
| 122 | * If no settings are given then the default visibility is RequestedVisibility.public. |
||
| 123 | * The resolved visibility, though, may depend on other aspects such as team and |
||
| 124 | * shared folder settings). Only for paid users. |
||
| 125 | * |
||
| 126 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings |
||
| 127 | */ |
||
| 128 | public function createSharedLinkWithSettings(string $path, array $settings = []) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * List shared links. |
||
| 143 | * |
||
| 144 | * For empty path returns a list of all shared links. For non-empty path |
||
| 145 | * returns a list of all shared links with access to the given path. |
||
| 146 | * |
||
| 147 | * If direct_only is set true, only direct links to the path will be returned, otherwise |
||
| 148 | * it may return link to the path itself and parent folders as described on docs. |
||
| 149 | * |
||
| 150 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links |
||
| 151 | */ |
||
| 152 | public function listSharedLinks(string $path = null, bool $direct_only = false, string $cursor = null): array |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Delete the file or folder at a given path. |
||
| 167 | * |
||
| 168 | * If the path is a folder, all its contents will be deleted too. |
||
| 169 | * A successful response indicates that the file or folder was deleted. |
||
| 170 | * |
||
| 171 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete |
||
| 172 | */ |
||
| 173 | public function delete(string $path): array |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Download a file from a user's Dropbox. |
||
| 184 | * |
||
| 185 | * @param string $path |
||
| 186 | * |
||
| 187 | * @return resource |
||
| 188 | * |
||
| 189 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download |
||
| 190 | */ |
||
| 191 | public function download(string $path) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Returns the metadata for a file or folder. |
||
| 204 | * |
||
| 205 | * Note: Metadata for the root folder is unsupported. |
||
| 206 | * |
||
| 207 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata |
||
| 208 | */ |
||
| 209 | public function getMetadata(string $path): array |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get a temporary link to stream content of a file. |
||
| 220 | * |
||
| 221 | * This link will expire in four hours and afterwards you will get 410 Gone. |
||
| 222 | * Content-Type of the link is determined automatically by the file's mime type. |
||
| 223 | * |
||
| 224 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link |
||
| 225 | */ |
||
| 226 | public function getTemporaryLink(string $path): string |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get a thumbnail for an image. |
||
| 239 | * |
||
| 240 | * This method currently supports files with the following file extensions: |
||
| 241 | * jpg, jpeg, png, tiff, tif, gif and bmp. |
||
| 242 | * |
||
| 243 | * Photos that are larger than 20MB in size won't be converted to a thumbnail. |
||
| 244 | * |
||
| 245 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail |
||
| 246 | */ |
||
| 247 | public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Starts returning the contents of a folder. |
||
| 262 | * |
||
| 263 | * If the result's ListFolderResult.has_more field is true, call |
||
| 264 | * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries. |
||
| 265 | * |
||
| 266 | * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls |
||
| 267 | * with same parameters are made simultaneously by same API app for same user. If your app implements |
||
| 268 | * retry logic, please hold off the retry until the previous request finishes. |
||
| 269 | * |
||
| 270 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder |
||
| 271 | */ |
||
| 272 | public function listFolder(string $path = '', bool $recursive = false): array |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Once a cursor has been retrieved from list_folder, use this to paginate through all files and |
||
| 284 | * retrieve updates to the folder, following the same rules as documented for list_folder. |
||
| 285 | * |
||
| 286 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue |
||
| 287 | */ |
||
| 288 | public function listFolderContinue(string $cursor = ''): array |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Move a file or folder to a different location in the user's Dropbox. |
||
| 295 | * |
||
| 296 | * If the source path is a folder all its contents will be moved. |
||
| 297 | * |
||
| 298 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move_v2 |
||
| 299 | */ |
||
| 300 | public function move(string $fromPath, string $toPath): array |
||
| 309 | |||
| 310 | /** |
||
| 311 | * The file should be uploaded in chunks if it size exceeds the 150 MB threshold |
||
| 312 | * or if the resource size could not be determined (eg. a popen() stream). |
||
| 313 | * |
||
| 314 | * @param string|resource $contents |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | protected function shouldUploadChunked($contents): bool |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Check if the contents is a pipe stream (not seekable, no size defined). |
||
| 335 | * |
||
| 336 | * @param string|resource $contents |
||
| 337 | * |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | protected function isPipe($contents): bool |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Create a new file with the contents provided in the request. |
||
| 347 | * |
||
| 348 | * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start. |
||
| 349 | * |
||
| 350 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload |
||
| 351 | * |
||
| 352 | * @param string $path |
||
| 353 | * @param string|resource $contents |
||
| 354 | * @param string $mode |
||
| 355 | * |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | public function upload(string $path, $contents, $mode = 'add'): array |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Upload file split in chunks. This allows uploading large files, since |
||
| 380 | * Dropbox API v2 limits the content size to 150MB. |
||
| 381 | * |
||
| 382 | * The chunk size will affect directly the memory usage, so be careful. |
||
| 383 | * Large chunks tends to speed up the upload, while smaller optimizes memory usage. |
||
| 384 | * |
||
| 385 | * @param string $path |
||
| 386 | * @param string|resource $contents |
||
| 387 | * @param string $mode |
||
| 388 | * @param int $chunkSize |
||
| 389 | * |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | public function uploadChunked(string $path, $contents, $mode = 'add', $chunkSize = null): array |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param int $type |
||
| 411 | * @param Psr7\Stream $stream |
||
| 412 | * @param int $chunkSize |
||
| 413 | * @param \Spatie\Dropbox\UploadSessionCursor|null $cursor |
||
| 414 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
| 415 | * @throws Exception |
||
| 416 | */ |
||
| 417 | protected function uploadChunk($type, &$stream, $chunkSize, $cursor = null): UploadSessionCursor |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Upload sessions allow you to upload a single file in one or more requests, |
||
| 451 | * for example where the size of the file is greater than 150 MB. |
||
| 452 | * This call starts a new upload session with the given data. |
||
| 453 | * |
||
| 454 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start |
||
| 455 | * |
||
| 456 | * @param string|StreamInterface $contents |
||
| 457 | * @param bool $close |
||
| 458 | * |
||
| 459 | * @return UploadSessionCursor |
||
| 460 | */ |
||
| 461 | public function uploadSessionStart($contents, bool $close = false): UploadSessionCursor |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Append more data to an upload session. |
||
| 475 | * When the parameter close is set, this call will close the session. |
||
| 476 | * A single request should not upload more than 150 MB. |
||
| 477 | * |
||
| 478 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2 |
||
| 479 | * |
||
| 480 | * @param string|StreamInterface $contents |
||
| 481 | * @param UploadSessionCursor $cursor |
||
| 482 | * @param bool $close |
||
| 483 | * |
||
| 484 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
| 485 | */ |
||
| 486 | public function uploadSessionAppend($contents, UploadSessionCursor $cursor, bool $close = false): UploadSessionCursor |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Finish an upload session and save the uploaded data to the given file path. |
||
| 500 | * A single request should not upload more than 150 MB. |
||
| 501 | * |
||
| 502 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish |
||
| 503 | * |
||
| 504 | * @param string|StreamInterface $contents |
||
| 505 | * @param \Spatie\Dropbox\UploadSessionCursor $cursor |
||
| 506 | * @param string $path |
||
| 507 | * @param string|array $mode |
||
| 508 | * @param bool $autorename |
||
| 509 | * @param bool $mute |
||
| 510 | * |
||
| 511 | * @return array |
||
| 512 | */ |
||
| 513 | public function uploadSessionFinish($contents, UploadSessionCursor $cursor, string $path, $mode = 'add', $autorename = false, $mute = false): array |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Get Account Info for current authenticated user. |
||
| 533 | * |
||
| 534 | * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account |
||
| 535 | * |
||
| 536 | * @return array |
||
| 537 | */ |
||
| 538 | public function getAccountInfo(): array |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Revoke current access token. |
||
| 545 | * |
||
| 546 | * @link https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke |
||
| 547 | */ |
||
| 548 | public function revokeToken() |
||
| 552 | |||
| 553 | protected function normalizePath(string $path): string |
||
| 563 | |||
| 564 | protected function getEndpointUrl(string $subdomain, string $endpoint): string |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @param string $endpoint |
||
| 575 | * @param array $arguments |
||
| 576 | * @param string|resource|StreamInterface $body |
||
| 577 | * |
||
| 578 | * @return \Psr\Http\Message\ResponseInterface |
||
| 579 | * |
||
| 580 | * @throws \Exception |
||
| 581 | */ |
||
| 582 | public function contentEndpointRequest(string $endpoint, array $arguments, $body = ''): ResponseInterface |
||
| 601 | |||
| 602 | public function rpcEndpointRequest(string $endpoint, array $parameters = null): array |
||
| 620 | |||
| 621 | protected function determineException(ClientException $exception): Exception |
||
| 629 | |||
| 630 | /** |
||
| 631 | * @param $contents |
||
| 632 | * |
||
| 633 | * @return \GuzzleHttp\Psr7\PumpStream|\GuzzleHttp\Psr7\Stream |
||
| 634 | */ |
||
| 635 | protected function getStream($contents) |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Get the access token. |
||
| 654 | */ |
||
| 655 | public function getAccessToken(): string |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Set the access token. |
||
| 662 | */ |
||
| 663 | public function setAccessToken(string $accessToken): self |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Get the HTTP headers. |
||
| 672 | */ |
||
| 673 | protected function getHeaders(array $headers = []): array |
||
| 679 | } |
||
| 680 |
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.