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 |
||
16 | class Client |
||
17 | { |
||
18 | const THUMBNAIL_FORMAT_JPEG = 'jpeg'; |
||
19 | const THUMBNAIL_FORMAT_PNG = 'png'; |
||
20 | |||
21 | const THUMBNAIL_SIZE_XS = 'w32h32'; |
||
22 | const THUMBNAIL_SIZE_S = 'w64h64'; |
||
23 | const THUMBNAIL_SIZE_M = 'w128h128'; |
||
24 | const THUMBNAIL_SIZE_L = 'w640h480'; |
||
25 | const THUMBNAIL_SIZE_XL = 'w1024h768'; |
||
26 | |||
27 | const MAX_CHUNK_SIZE = 1024 * 1024 * 150; |
||
28 | |||
29 | const UPLOAD_SESSION_START = 0; |
||
30 | const UPLOAD_SESSION_APPEND = 1; |
||
31 | |||
32 | /** @var string */ |
||
33 | protected $accessToken; |
||
34 | |||
35 | /** @var \GuzzleHttp\Client */ |
||
36 | protected $client; |
||
37 | |||
38 | /** @var int */ |
||
39 | protected $maxChunkSize; |
||
40 | |||
41 | /** @var int */ |
||
42 | protected $maxUploadChunkRetries; |
||
43 | |||
44 | /** |
||
45 | * @param string $accessToken |
||
46 | * @param GuzzleClient|null $client |
||
47 | * @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). |
||
48 | * @param int $maxUploadChunkRetries How many times to retry an upload session start or append after RequestException. |
||
49 | */ |
||
50 | public function __construct(string $accessToken, GuzzleClient $client = null, int $maxChunkSize = self::MAX_CHUNK_SIZE, int $maxUploadChunkRetries = 0) |
||
59 | |||
60 | /** |
||
61 | * Copy a file or folder to a different location in the user's Dropbox. |
||
62 | * |
||
63 | * If the source path is a folder all its contents will be copied. |
||
64 | * |
||
65 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy_v2 |
||
66 | */ |
||
67 | public function copy(string $fromPath, string $toPath): array |
||
68 | { |
||
69 | $parameters = [ |
||
70 | 'from_path' => $this->normalizePath($fromPath), |
||
71 | 'to_path' => $this->normalizePath($toPath), |
||
72 | ]; |
||
73 | |||
74 | return $this->rpcEndpointRequest('files/copy_v2', $parameters); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Create a folder at a given path. |
||
79 | * |
||
80 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder |
||
81 | */ |
||
82 | public function createFolder(string $path): array |
||
94 | |||
95 | /** |
||
96 | * Create a shared link with custom settings. |
||
97 | * |
||
98 | * If no settings are given then the default visibility is RequestedVisibility.public. |
||
99 | * The resolved visibility, though, may depend on other aspects such as team and |
||
100 | * shared folder settings). Only for paid users. |
||
101 | * |
||
102 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings |
||
103 | */ |
||
104 | public function createSharedLinkWithSettings(string $path, array $settings = []) |
||
116 | |||
117 | /** |
||
118 | * List shared links. |
||
119 | * |
||
120 | * For empty path returns a list of all shared links. For non-empty path |
||
121 | * returns a list of all shared links with access to the given path. |
||
122 | * |
||
123 | * If direct_only is set true, only direct links to the path will be returned, otherwise |
||
124 | * it may return link to the path itself and parent folders as described on docs. |
||
125 | * |
||
126 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links |
||
127 | */ |
||
128 | public function listSharedLinks(string $path = null, bool $direct_only = false, string $cursor = null): array |
||
140 | |||
141 | /** |
||
142 | * Delete the file or folder at a given path. |
||
143 | * |
||
144 | * If the path is a folder, all its contents will be deleted too. |
||
145 | * A successful response indicates that the file or folder was deleted. |
||
146 | * |
||
147 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete |
||
148 | */ |
||
149 | public function delete(string $path): array |
||
157 | |||
158 | /** |
||
159 | * Download a file from a user's Dropbox. |
||
160 | * |
||
161 | * @param string $path |
||
162 | * |
||
163 | * @return resource |
||
164 | * |
||
165 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download |
||
166 | */ |
||
167 | public function download(string $path) |
||
177 | |||
178 | /** |
||
179 | * Returns the metadata for a file or folder. |
||
180 | * |
||
181 | * Note: Metadata for the root folder is unsupported. |
||
182 | * |
||
183 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata |
||
184 | */ |
||
185 | public function getMetadata(string $path): array |
||
193 | |||
194 | /** |
||
195 | * Get a temporary link to stream content of a file. |
||
196 | * |
||
197 | * This link will expire in four hours and afterwards you will get 410 Gone. |
||
198 | * Content-Type of the link is determined automatically by the file's mime type. |
||
199 | * |
||
200 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link |
||
201 | */ |
||
202 | public function getTemporaryLink(string $path): string |
||
203 | { |
||
204 | $parameters = [ |
||
205 | 'path' => $this->normalizePath($path), |
||
206 | ]; |
||
207 | |||
208 | $body = $this->rpcEndpointRequest('files/get_temporary_link', $parameters); |
||
209 | |||
210 | return $body['link']; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Get a thumbnail for an image. |
||
215 | * |
||
216 | * This method currently supports files with the following file extensions: |
||
217 | * jpg, jpeg, png, tiff, tif, gif and bmp. |
||
218 | * |
||
219 | * Photos that are larger than 20MB in size won't be converted to a thumbnail. |
||
220 | * |
||
221 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail |
||
222 | */ |
||
223 | public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string |
||
235 | |||
236 | /** |
||
237 | * Starts returning the contents of a folder. |
||
238 | * |
||
239 | * If the result's ListFolderResult.has_more field is true, call |
||
240 | * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries. |
||
241 | * |
||
242 | * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls |
||
243 | * with same parameters are made simultaneously by same API app for same user. If your app implements |
||
244 | * retry logic, please hold off the retry until the previous request finishes. |
||
245 | * |
||
246 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder |
||
247 | */ |
||
248 | public function listFolder(string $path = '', bool $recursive = false): array |
||
249 | { |
||
250 | $parameters = [ |
||
251 | 'path' => $this->normalizePath($path), |
||
252 | 'recursive' => $recursive, |
||
253 | ]; |
||
254 | |||
255 | return $this->rpcEndpointRequest('files/list_folder', $parameters); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Once a cursor has been retrieved from list_folder, use this to paginate through all files and |
||
260 | * retrieve updates to the folder, following the same rules as documented for list_folder. |
||
261 | * |
||
262 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue |
||
263 | */ |
||
264 | public function listFolderContinue(string $cursor = ''): array |
||
268 | |||
269 | /** |
||
270 | * Move a file or folder to a different location in the user's Dropbox. |
||
271 | * |
||
272 | * If the source path is a folder all its contents will be moved. |
||
273 | * |
||
274 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move_v2 |
||
275 | */ |
||
276 | public function move(string $fromPath, string $toPath): array |
||
277 | { |
||
278 | $parameters = [ |
||
279 | 'from_path' => $this->normalizePath($fromPath), |
||
280 | 'to_path' => $this->normalizePath($toPath), |
||
281 | ]; |
||
282 | |||
283 | return $this->rpcEndpointRequest('files/move_v2', $parameters); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * The file should be uploaded in chunks if it size exceeds the 150 MB threshold |
||
288 | * or if the resource size could not be determined (eg. a popen() stream). |
||
289 | * |
||
290 | * @param string|resource $contents |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | protected function shouldUploadChunked($contents): bool |
||
308 | |||
309 | /** |
||
310 | * Check if the contents is a pipe stream (not seekable, no size defined). |
||
311 | * |
||
312 | * @param string|resource $contents |
||
313 | * |
||
314 | * @return bool |
||
315 | */ |
||
316 | protected function isPipe($contents): bool |
||
320 | |||
321 | /** |
||
322 | * Create a new file with the contents provided in the request. |
||
323 | * |
||
324 | * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start. |
||
325 | * |
||
326 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload |
||
327 | * |
||
328 | * @param string $path |
||
329 | * @param string|resource $contents |
||
330 | * @param string $mode |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | public function upload(string $path, $contents, $mode = 'add'): array |
||
353 | |||
354 | /** |
||
355 | * Upload file split in chunks. This allows uploading large files, since |
||
356 | * Dropbox API v2 limits the content size to 150MB. |
||
357 | * |
||
358 | * The chunk size will affect directly the memory usage, so be careful. |
||
359 | * Large chunks tends to speed up the upload, while smaller optimizes memory usage. |
||
360 | * |
||
361 | * @param string $path |
||
362 | * @param string|resource $contents |
||
363 | * @param string $mode |
||
364 | * @param int $chunkSize |
||
365 | * |
||
366 | * @return array |
||
367 | */ |
||
368 | public function uploadChunked(string $path, $contents, $mode = 'add', $chunkSize = null): array |
||
384 | |||
385 | /** |
||
386 | * @param int $type |
||
387 | * @param Psr7\Stream $stream |
||
388 | * @param int $chunkSize |
||
389 | * @param \Spatie\Dropbox\UploadSessionCursor|null $cursor |
||
390 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
391 | * @throws Exception |
||
392 | */ |
||
393 | protected function uploadChunk($type, &$stream, $chunkSize, $cursor = null): UploadSessionCursor |
||
424 | |||
425 | /** |
||
426 | * Upload sessions allow you to upload a single file in one or more requests, |
||
427 | * for example where the size of the file is greater than 150 MB. |
||
428 | * This call starts a new upload session with the given data. |
||
429 | * |
||
430 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start |
||
431 | * |
||
432 | * @param string|StreamInterface $contents |
||
433 | * @param bool $close |
||
434 | * |
||
435 | * @return UploadSessionCursor |
||
436 | */ |
||
437 | public function uploadSessionStart($contents, bool $close = false): UploadSessionCursor |
||
448 | |||
449 | /** |
||
450 | * Append more data to an upload session. |
||
451 | * When the parameter close is set, this call will close the session. |
||
452 | * A single request should not upload more than 150 MB. |
||
453 | * |
||
454 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2 |
||
455 | * |
||
456 | * @param string|StreamInterface $contents |
||
457 | * @param UploadSessionCursor $cursor |
||
458 | * @param bool $close |
||
459 | * |
||
460 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
461 | */ |
||
462 | public function uploadSessionAppend($contents, UploadSessionCursor $cursor, bool $close = false): UploadSessionCursor |
||
473 | |||
474 | /** |
||
475 | * Finish an upload session and save the uploaded data to the given file path. |
||
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-finish |
||
479 | * |
||
480 | * @param string|StreamInterface $contents |
||
481 | * @param \Spatie\Dropbox\UploadSessionCursor $cursor |
||
482 | * @param string $path |
||
483 | * @param string|array $mode |
||
484 | * @param bool $autorename |
||
485 | * @param bool $mute |
||
486 | * |
||
487 | * @return array |
||
488 | */ |
||
489 | public function uploadSessionFinish($contents, UploadSessionCursor $cursor, string $path, $mode = 'add', $autorename = false, $mute = false): array |
||
506 | |||
507 | /** |
||
508 | * Get Account Info for current authenticated user. |
||
509 | * |
||
510 | * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account |
||
511 | * |
||
512 | * @return array |
||
513 | */ |
||
514 | public function getAccountInfo(): array |
||
518 | |||
519 | /** |
||
520 | * Revoke current access token. |
||
521 | * |
||
522 | * @link https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke |
||
523 | */ |
||
524 | public function revokeToken() |
||
528 | |||
529 | protected function normalizePath(string $path): string |
||
539 | |||
540 | protected function getEndpointUrl(string $subdomain, string $endpoint): string |
||
548 | |||
549 | /** |
||
550 | * @param string $endpoint |
||
551 | * @param array $arguments |
||
552 | * @param string|resource|StreamInterface $body |
||
553 | * |
||
554 | * @return \Psr\Http\Message\ResponseInterface |
||
555 | * |
||
556 | * @throws \Exception |
||
557 | */ |
||
558 | public function contentEndpointRequest(string $endpoint, array $arguments, $body = ''): ResponseInterface |
||
577 | |||
578 | public function rpcEndpointRequest(string $endpoint, array $parameters = null): array |
||
596 | |||
597 | protected function determineException(ClientException $exception): Exception |
||
605 | |||
606 | /** |
||
607 | * @param $contents |
||
608 | * |
||
609 | * @return \GuzzleHttp\Psr7\PumpStream|\GuzzleHttp\Psr7\Stream |
||
610 | */ |
||
611 | protected function getStream($contents) |
||
627 | |||
628 | /** |
||
629 | * Get the access token. |
||
630 | */ |
||
631 | public function getAccessToken(): string |
||
635 | |||
636 | /** |
||
637 | * Set the access token. |
||
638 | */ |
||
639 | public function setAccessToken(string $accessToken): self |
||
645 | |||
646 | /** |
||
647 | * Get the HTTP headers. |
||
648 | */ |
||
649 | protected function getHeaders(array $headers = []): array |
||
655 | } |
||
656 |
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.