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) |
||
52 | { |
||
53 | $this->accessToken = $accessToken; |
||
54 | |||
55 | $this->client = $client ?? new GuzzleClient(['handler' => GuzzleFactory::handler()]); |
||
56 | |||
57 | $this->maxChunkSize = ($maxChunkSize < self::MAX_CHUNK_SIZE ? ($maxChunkSize > 1 ? $maxChunkSize : 1) : self::MAX_CHUNK_SIZE); |
||
58 | $this->maxUploadChunkRetries = $maxUploadChunkRetries; |
||
59 | } |
||
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 |
||
69 | { |
||
70 | $parameters = [ |
||
71 | 'from_path' => $this->normalizePath($fromPath), |
||
72 | 'to_path' => $this->normalizePath($toPath), |
||
73 | ]; |
||
74 | |||
75 | return $this->rpcEndpointRequest('files/copy_v2', $parameters); |
||
76 | } |
||
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 |
||
84 | { |
||
85 | $parameters = [ |
||
86 | 'path' => $this->normalizePath($path), |
||
87 | ]; |
||
88 | |||
89 | $object = $this->rpcEndpointRequest('files/create_folder', $parameters); |
||
90 | |||
91 | $object['.tag'] = 'folder'; |
||
92 | |||
93 | return $object; |
||
94 | } |
||
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 = []) |
||
106 | { |
||
107 | $parameters = [ |
||
108 | 'path' => $this->normalizePath($path), |
||
109 | ]; |
||
110 | |||
111 | if (count($settings)) { |
||
112 | $parameters = array_merge(compact('settings'), $parameters); |
||
113 | } |
||
114 | |||
115 | return $this->rpcEndpointRequest('sharing/create_shared_link_with_settings', $parameters); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * List shared links. |
||
120 | * |
||
121 | * For empty path returns a list of all shared links. For non-empty path |
||
122 | * returns a list of all shared links with access to the given path. |
||
123 | * |
||
124 | * If direct_only is set true, only direct links to the path will be returned, otherwise |
||
125 | * it may return link to the path itself and parent folders as described on docs. |
||
126 | * |
||
127 | * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links |
||
128 | */ |
||
129 | public function listSharedLinks(string $path = null, bool $direct_only = false, string $cursor = null): array |
||
141 | |||
142 | /** |
||
143 | * Delete the file or folder at a given path. |
||
144 | * |
||
145 | * If the path is a folder, all its contents will be deleted too. |
||
146 | * A successful response indicates that the file or folder was deleted. |
||
147 | * |
||
148 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete |
||
149 | */ |
||
150 | public function delete(string $path): array |
||
158 | |||
159 | /** |
||
160 | * Download a file from a user's Dropbox. |
||
161 | * |
||
162 | * @param string $path |
||
163 | * |
||
164 | * @return resource |
||
165 | * |
||
166 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download |
||
167 | */ |
||
168 | public function download(string $path) |
||
178 | |||
179 | /** |
||
180 | * Returns the metadata for a file or folder. |
||
181 | * |
||
182 | * Note: Metadata for the root folder is unsupported. |
||
183 | * |
||
184 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata |
||
185 | */ |
||
186 | public function getMetadata(string $path): array |
||
194 | |||
195 | /** |
||
196 | * Get a temporary link to stream content of a file. |
||
197 | * |
||
198 | * This link will expire in four hours and afterwards you will get 410 Gone. |
||
199 | * Content-Type of the link is determined automatically by the file's mime type. |
||
200 | * |
||
201 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link |
||
202 | */ |
||
203 | public function getTemporaryLink(string $path): string |
||
204 | { |
||
205 | $parameters = [ |
||
206 | 'path' => $this->normalizePath($path), |
||
207 | ]; |
||
208 | |||
209 | $body = $this->rpcEndpointRequest('files/get_temporary_link', $parameters); |
||
210 | |||
211 | return $body['link']; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Get a thumbnail for an image. |
||
216 | * |
||
217 | * This method currently supports files with the following file extensions: |
||
218 | * jpg, jpeg, png, tiff, tif, gif and bmp. |
||
219 | * |
||
220 | * Photos that are larger than 20MB in size won't be converted to a thumbnail. |
||
221 | * |
||
222 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail |
||
223 | */ |
||
224 | public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string |
||
236 | |||
237 | /** |
||
238 | * Starts returning the contents of a folder. |
||
239 | * |
||
240 | * If the result's ListFolderResult.has_more field is true, call |
||
241 | * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries. |
||
242 | * |
||
243 | * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls |
||
244 | * with same parameters are made simultaneously by same API app for same user. If your app implements |
||
245 | * retry logic, please hold off the retry until the previous request finishes. |
||
246 | * |
||
247 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder |
||
248 | */ |
||
249 | public function listFolder(string $path = '', bool $recursive = false): array |
||
250 | { |
||
251 | $parameters = [ |
||
252 | 'path' => $this->normalizePath($path), |
||
253 | 'recursive' => $recursive, |
||
254 | ]; |
||
255 | |||
256 | return $this->rpcEndpointRequest('files/list_folder', $parameters); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Once a cursor has been retrieved from list_folder, use this to paginate through all files and |
||
261 | * retrieve updates to the folder, following the same rules as documented for list_folder. |
||
262 | * |
||
263 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue |
||
264 | */ |
||
265 | public function listFolderContinue(string $cursor = ''): array |
||
269 | |||
270 | /** |
||
271 | * Move a file or folder to a different location in the user's Dropbox. |
||
272 | * |
||
273 | * If the source path is a folder all its contents will be moved. |
||
274 | * |
||
275 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move_v2 |
||
276 | */ |
||
277 | public function move(string $fromPath, string $toPath): array |
||
278 | { |
||
279 | $parameters = [ |
||
280 | 'from_path' => $this->normalizePath($fromPath), |
||
281 | 'to_path' => $this->normalizePath($toPath), |
||
282 | ]; |
||
283 | |||
284 | return $this->rpcEndpointRequest('files/move_v2', $parameters); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * The file should be uploaded in chunks if it size exceeds the 150 MB threshold |
||
289 | * or if the resource size could not be determined (eg. a popen() stream). |
||
290 | * |
||
291 | * @param string|resource $contents |
||
292 | * |
||
293 | * @return bool |
||
294 | */ |
||
295 | protected function shouldUploadChunked($contents): bool |
||
309 | |||
310 | /** |
||
311 | * Check if the contents is a pipe stream (not seekable, no size defined). |
||
312 | * |
||
313 | * @param string|resource $contents |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | protected function isPipe($contents): bool |
||
321 | |||
322 | /** |
||
323 | * Create a new file with the contents provided in the request. |
||
324 | * |
||
325 | * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start. |
||
326 | * |
||
327 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload |
||
328 | * |
||
329 | * @param string $path |
||
330 | * @param string|resource $contents |
||
331 | * @param string $mode |
||
332 | * |
||
333 | * @return array |
||
334 | */ |
||
335 | public function upload(string $path, $contents, $mode = 'add'): array |
||
354 | |||
355 | /** |
||
356 | * Upload file split in chunks. This allows uploading large files, since |
||
357 | * Dropbox API v2 limits the content size to 150MB. |
||
358 | * |
||
359 | * The chunk size will affect directly the memory usage, so be careful. |
||
360 | * Large chunks tends to speed up the upload, while smaller optimizes memory usage. |
||
361 | * |
||
362 | * @param string $path |
||
363 | * @param string|resource $contents |
||
364 | * @param string $mode |
||
365 | * @param int $chunkSize |
||
366 | * |
||
367 | * @return array |
||
368 | */ |
||
369 | public function uploadChunked(string $path, $contents, $mode = 'add', $chunkSize = null): array |
||
385 | |||
386 | /** |
||
387 | * @param int $type |
||
388 | * @param Psr7\Stream $stream |
||
389 | * @param int $chunkSize |
||
390 | * @param \Spatie\Dropbox\UploadSessionCursor|null $cursor |
||
391 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
392 | * @throws Exception |
||
393 | */ |
||
394 | protected function uploadChunk($type, &$stream, $chunkSize, $cursor = null): UploadSessionCursor |
||
425 | |||
426 | /** |
||
427 | * Upload sessions allow you to upload a single file in one or more requests, |
||
428 | * for example where the size of the file is greater than 150 MB. |
||
429 | * This call starts a new upload session with the given data. |
||
430 | * |
||
431 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start |
||
432 | * |
||
433 | * @param string|StreamInterface $contents |
||
434 | * @param bool $close |
||
435 | * |
||
436 | * @return UploadSessionCursor |
||
437 | */ |
||
438 | public function uploadSessionStart($contents, bool $close = false): UploadSessionCursor |
||
449 | |||
450 | /** |
||
451 | * Append more data to an upload session. |
||
452 | * When the parameter close is set, this call will close the session. |
||
453 | * A single request should not upload more than 150 MB. |
||
454 | * |
||
455 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2 |
||
456 | * |
||
457 | * @param string|StreamInterface $contents |
||
458 | * @param UploadSessionCursor $cursor |
||
459 | * @param bool $close |
||
460 | * |
||
461 | * @return \Spatie\Dropbox\UploadSessionCursor |
||
462 | */ |
||
463 | public function uploadSessionAppend($contents, UploadSessionCursor $cursor, bool $close = false): UploadSessionCursor |
||
474 | |||
475 | /** |
||
476 | * Finish an upload session and save the uploaded data to the given file path. |
||
477 | * A single request should not upload more than 150 MB. |
||
478 | * |
||
479 | * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish |
||
480 | * |
||
481 | * @param string|StreamInterface $contents |
||
482 | * @param \Spatie\Dropbox\UploadSessionCursor $cursor |
||
483 | * @param string $path |
||
484 | * @param string|array $mode |
||
485 | * @param bool $autorename |
||
486 | * @param bool $mute |
||
487 | * |
||
488 | * @return array |
||
489 | */ |
||
490 | public function uploadSessionFinish($contents, UploadSessionCursor $cursor, string $path, $mode = 'add', $autorename = false, $mute = false): array |
||
507 | |||
508 | /** |
||
509 | * Get Account Info for current authenticated user. |
||
510 | * |
||
511 | * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account |
||
512 | * |
||
513 | * @return array |
||
514 | */ |
||
515 | public function getAccountInfo(): array |
||
519 | |||
520 | /** |
||
521 | * Revoke current access token. |
||
522 | * |
||
523 | * @link https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke |
||
524 | */ |
||
525 | public function revokeToken() |
||
529 | |||
530 | protected function normalizePath(string $path): string |
||
540 | |||
541 | protected function getEndpointUrl(string $subdomain, string $endpoint): string |
||
542 | { |
||
543 | if (count($parts = explode('::', $endpoint)) === 2) { |
||
544 | [$subdomain, $endpoint] = $parts; |
||
545 | } |
||
546 | |||
547 | return "https://{$subdomain}.dropboxapi.com/2/{$endpoint}"; |
||
548 | } |
||
549 | |||
550 | /** |
||
551 | * @param string $endpoint |
||
552 | * @param array $arguments |
||
553 | * @param string|resource|StreamInterface $body |
||
554 | * |
||
555 | * @return \Psr\Http\Message\ResponseInterface |
||
556 | * |
||
557 | * @throws \Exception |
||
558 | */ |
||
559 | public function contentEndpointRequest(string $endpoint, array $arguments, $body = ''): ResponseInterface |
||
578 | |||
579 | public function rpcEndpointRequest(string $endpoint, array $parameters = null): array |
||
597 | |||
598 | protected function determineException(ClientException $exception): Exception |
||
606 | |||
607 | /** |
||
608 | * @param $contents |
||
609 | * |
||
610 | * @return \GuzzleHttp\Psr7\PumpStream|\GuzzleHttp\Psr7\Stream |
||
611 | */ |
||
612 | protected function getStream($contents) |
||
628 | |||
629 | /** |
||
630 | * Get the access token. |
||
631 | */ |
||
632 | public function getAccessToken(): string |
||
636 | |||
637 | /** |
||
638 | * Set the access token. |
||
639 | */ |
||
640 | public function setAccessToken(string $accessToken): self |
||
646 | |||
647 | /** |
||
648 | * Get the HTTP headers. |
||
649 | */ |
||
650 | protected function getHeaders(array $headers = []): array |
||
656 | } |
||
657 |
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.