Complex classes like AwsS3Adapter 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 AwsS3Adapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class AwsS3Adapter extends AbstractAdapter implements CanOverwriteFiles |
||
16 | { |
||
17 | const PUBLIC_GRANT_URI = 'http://acs.amazonaws.com/groups/global/AllUsers'; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected static $resultMap = [ |
||
23 | 'Body' => 'contents', |
||
24 | 'ContentLength' => 'size', |
||
25 | 'ContentType' => 'mimetype', |
||
26 | 'Size' => 'size', |
||
27 | 'Metadata' => 'metadata', |
||
28 | 'StorageClass' => 'storageclass', |
||
29 | 'ETag' => 'etag', |
||
30 | 'VersionId' => 'versionid' |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected static $metaOptions = [ |
||
37 | 'ACL', |
||
38 | 'CacheControl', |
||
39 | 'ContentDisposition', |
||
40 | 'ContentEncoding', |
||
41 | 'ContentLength', |
||
42 | 'ContentType', |
||
43 | 'Expires', |
||
44 | 'GrantFullControl', |
||
45 | 'GrantRead', |
||
46 | 'GrantReadACP', |
||
47 | 'GrantWriteACP', |
||
48 | 'Metadata', |
||
49 | 'RequestPayer', |
||
50 | 'SSECustomerAlgorithm', |
||
51 | 'SSECustomerKey', |
||
52 | 'SSECustomerKeyMD5', |
||
53 | 'SSEKMSKeyId', |
||
54 | 'ServerSideEncryption', |
||
55 | 'StorageClass', |
||
56 | 'Tagging', |
||
57 | 'WebsiteRedirectLocation', |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * @var S3Client |
||
62 | */ |
||
63 | protected $s3Client; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $bucket; |
||
69 | |||
70 | /** |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $options = []; |
||
74 | |||
75 | /** |
||
76 | * Constructor. |
||
77 | * |
||
78 | * @param S3Client $client |
||
79 | * @param string $bucket |
||
80 | * @param string $prefix |
||
81 | * @param array $options |
||
82 | 72 | */ |
|
83 | public function __construct(S3Client $client, $bucket, $prefix = '', array $options = []) |
||
90 | |||
91 | /** |
||
92 | * Get the S3Client bucket. |
||
93 | * |
||
94 | * @return string |
||
95 | 4 | */ |
|
96 | public function getBucket() |
||
100 | |||
101 | /** |
||
102 | * Set the S3Client bucket. |
||
103 | * |
||
104 | * @return AwsS3Adapter |
||
105 | 2 | */ |
|
106 | public function setBucket($bucket) |
||
111 | |||
112 | /** |
||
113 | * Get the S3Client instance. |
||
114 | * |
||
115 | 2 | * @return S3Client |
|
116 | */ |
||
117 | 2 | public function getClient() |
|
121 | |||
122 | /** |
||
123 | * Set the S3Client. |
||
124 | * |
||
125 | * @return AwsS3Adapter |
||
126 | */ |
||
127 | public function setClient($s3Client) |
||
132 | |||
133 | /** |
||
134 | * Set the S3Client options. |
||
135 | * |
||
136 | * @return AwsS3Adapter |
||
137 | */ |
||
138 | public function setOptions($options) |
||
143 | 2 | ||
144 | /** |
||
145 | 2 | * Write a new file. |
|
146 | * |
||
147 | * @param string $path |
||
148 | * @param string $contents |
||
149 | * @param Config $config Config object |
||
150 | * |
||
151 | * @return false|array false on failure file meta data on success |
||
152 | */ |
||
153 | public function write($path, $contents, Config $config) |
||
157 | |||
158 | 4 | /** |
|
159 | 2 | * Update a file. |
|
160 | * |
||
161 | * @param string $path |
||
162 | 2 | * @param string $contents |
|
163 | * @param Config $config Config object |
||
164 | * |
||
165 | * @return false|array false on failure file meta data on success |
||
166 | */ |
||
167 | public function update($path, $contents, Config $config) |
||
171 | |||
172 | 4 | /** |
|
173 | * Rename a file. |
||
174 | 4 | * |
|
175 | * @param string $path |
||
176 | 4 | * @param string $newpath |
|
177 | 4 | * |
|
178 | * @return bool |
||
179 | 4 | */ |
|
180 | 4 | public function rename($path, $newpath) |
|
188 | |||
189 | /** |
||
190 | * Delete a file. |
||
191 | * |
||
192 | * @param string $path |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | 4 | public function delete($path) |
|
212 | |||
213 | /** |
||
214 | * Delete a directory. |
||
215 | * |
||
216 | 2 | * @param string $dirname |
|
217 | * |
||
218 | 2 | * @return bool |
|
219 | */ |
||
220 | public function deleteDir($dirname) |
||
231 | |||
232 | 12 | /** |
|
233 | 2 | * Create a directory. |
|
234 | * |
||
235 | * @param string $dirname directory name |
||
236 | 10 | * @param Config $config |
|
237 | * |
||
238 | * @return bool|array |
||
239 | */ |
||
240 | public function createDir($dirname, Config $config) |
||
244 | |||
245 | /** |
||
246 | 4 | * Check whether a file exists. |
|
247 | * |
||
248 | 4 | * @param string $path |
|
249 | * |
||
250 | 4 | * @return bool |
|
251 | 2 | */ |
|
252 | 1 | public function has($path) |
|
262 | |||
263 | /** |
||
264 | * Read a file. |
||
265 | 2 | * |
|
266 | * @param string $path |
||
267 | 2 | * |
|
268 | 2 | * @return false|array |
|
269 | */ |
||
270 | 2 | public function read($path) |
|
280 | |||
281 | /** |
||
282 | * List contents of a directory. |
||
283 | * |
||
284 | * @param string $directory |
||
285 | * @param bool $recursive |
||
286 | 2 | * |
|
287 | * @return array |
||
288 | 2 | */ |
|
289 | 2 | public function listContents($directory = '', $recursive = false) |
|
304 | |||
305 | 12 | /** |
|
306 | * @param array $options |
||
307 | 12 | * |
|
308 | 12 | * @return array |
|
309 | */ |
||
310 | 12 | protected function retrievePaginatedListing(array $options) |
|
321 | 4 | ||
322 | 2 | /** |
|
323 | * Get all the meta data of a file or directory. |
||
324 | * |
||
325 | 2 | * @param string $path |
|
326 | * |
||
327 | * @return false|array |
||
328 | 8 | */ |
|
329 | public function getMetadata($path) |
||
354 | |||
355 | /** |
||
356 | * Get all the meta data of a file or directory. |
||
357 | * |
||
358 | * @param string $path |
||
359 | * |
||
360 | * @return false|array |
||
361 | */ |
||
362 | 2 | public function getSize($path) |
|
366 | |||
367 | /** |
||
368 | * Get the mimetype of a file. |
||
369 | * |
||
370 | * @param string $path |
||
371 | * |
||
372 | * @return false|array |
||
373 | */ |
||
374 | public function getMimetype($path) |
||
378 | 2 | ||
379 | /** |
||
380 | * Get the timestamp of a file. |
||
381 | * |
||
382 | * @param string $path |
||
383 | * |
||
384 | * @return false|array |
||
385 | */ |
||
386 | public function getTimestamp($path) |
||
390 | 2 | ||
391 | /** |
||
392 | 2 | * Write a new file using a stream. |
|
393 | * |
||
394 | * @param string $path |
||
395 | * @param resource $resource |
||
396 | * @param Config $config Config object |
||
397 | * |
||
398 | * @return array|false false on failure file meta data on success |
||
399 | */ |
||
400 | public function writeStream($path, $resource, Config $config) |
||
404 | |||
405 | 8 | /** |
|
406 | 8 | * Update a file using a stream. |
|
407 | * |
||
408 | 8 | * @param string $path |
|
409 | 8 | * @param resource $resource |
|
410 | 8 | * @param Config $config Config object |
|
411 | 8 | * |
|
412 | 8 | * @return array|false false on failure file meta data on success |
|
413 | 8 | */ |
|
414 | 4 | public function updateStream($path, $resource, Config $config) |
|
418 | 6 | ||
419 | 4 | /** |
|
420 | * Copy a file. |
||
421 | * |
||
422 | 4 | * @param string $path |
|
423 | * @param string $newpath |
||
424 | * |
||
425 | * @return bool |
||
426 | */ |
||
427 | public function copy($path, $newpath) |
||
448 | |||
449 | /** |
||
450 | * Read a file as a stream. |
||
451 | 8 | * |
|
452 | * @param string $path |
||
453 | * |
||
454 | 8 | * @return array|false |
|
455 | 8 | */ |
|
456 | 4 | public function readStream($path) |
|
467 | 5 | ||
468 | 2 | /** |
|
469 | * Read an object and normalize the response. |
||
470 | * |
||
471 | 6 | * @param $path |
|
472 | * |
||
473 | * @return array|bool |
||
474 | */ |
||
475 | protected function readObject($path) |
||
497 | |||
498 | /** |
||
499 | 4 | * Set the visibility for a file. |
|
500 | * |
||
501 | * @param string $path |
||
502 | * @param string $visibility |
||
503 | * |
||
504 | * @return array|false file meta data |
||
505 | */ |
||
506 | public function setVisibility($path, $visibility) |
||
525 | 72 | ||
526 | /** |
||
527 | 72 | * Get the visibility of a file. |
|
528 | * |
||
529 | 72 | * @param string $path |
|
530 | * |
||
531 | * @return array|false |
||
532 | */ |
||
533 | public function getVisibility($path) |
||
537 | |||
538 | /** |
||
539 | 12 | * {@inheritdoc} |
|
540 | */ |
||
541 | 12 | public function applyPathPrefix($path) |
|
545 | 12 | ||
546 | /** |
||
547 | 6 | * {@inheritdoc} |
|
548 | */ |
||
549 | 12 | public function setPathPrefix($prefix) |
|
555 | 2 | ||
556 | 2 | /** |
|
557 | 1 | * Get the object acl presented as a visibility. |
|
558 | 2 | * |
|
559 | 2 | * @param string $path |
|
560 | * |
||
561 | 6 | * @return string |
|
562 | */ |
||
563 | 12 | protected function getRawVisibility($path) |
|
589 | 10 | ||
590 | /** |
||
591 | * Upload an object. |
||
592 | * |
||
593 | 10 | * @param $path |
|
594 | * @param $body |
||
595 | 10 | * @param Config $config |
|
596 | * |
||
597 | * @return array |
||
598 | */ |
||
599 | protected function upload($path, $body, Config $config) |
||
621 | 4 | ||
622 | /** |
||
623 | 10 | * Get options from the config. |
|
624 | 10 | * |
|
625 | 10 | * @param Config $config |
|
626 | * |
||
627 | 8 | * @return array |
|
628 | 5 | */ |
|
629 | protected function getOptionsFromConfig(Config $config) |
||
656 | 2 | ||
657 | /** |
||
658 | 2 | * Normalize the object result array. |
|
659 | * |
||
660 | * @param array $response |
||
661 | 22 | * @param string $path |
|
662 | * |
||
663 | * @return array |
||
664 | */ |
||
665 | protected function normalizeResponse(array $response, $path = null) |
||
687 | 4 | ||
688 | 2 | /** |
|
689 | * @param $location |
||
690 | * |
||
691 | 2 | * @return bool |
|
692 | */ |
||
693 | protected function doesDirectoryExist($location) |
||
718 | } |
||
719 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.