Complex classes like AliyunOssAdapter 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 AliyunOssAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class AliyunOssAdapter extends AbstractAdapter implements CanOverwriteFiles |
||
22 | { |
||
23 | /** |
||
24 | * @var OssClient |
||
25 | */ |
||
26 | protected $ossClient; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $bucket; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $options = []; |
||
37 | |||
38 | /** |
||
39 | * Constructor. |
||
40 | * |
||
41 | * @param OssClient $ossClient |
||
42 | * @param string $bucket |
||
43 | * @param string $prefix |
||
44 | 86 | * @param array $options |
|
45 | */ |
||
46 | 86 | public function __construct(OssClient $ossClient, $bucket, $prefix = '', array $options = []) |
|
47 | 86 | { |
|
48 | 86 | $this->ossClient = $ossClient; |
|
49 | 86 | $this->bucket = $bucket; |
|
50 | 86 | $this->setPathPrefix($prefix); |
|
51 | $this->options = $options; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Get the OssClient bucket. |
||
56 | * |
||
57 | 4 | * @return string |
|
58 | */ |
||
59 | 4 | public function getBucket() |
|
60 | { |
||
61 | return $this->bucket; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Set the OssClient bucket. |
||
66 | * |
||
67 | 2 | * @param string $bucket |
|
68 | */ |
||
69 | 2 | public function setBucket($bucket) |
|
70 | 2 | { |
|
71 | $this->bucket = $bucket; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Get the OssClient instance. |
||
76 | * |
||
77 | 2 | * @return OssClient |
|
78 | */ |
||
79 | 2 | public function getClient() |
|
80 | { |
||
81 | return $this->ossClient; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Write a new file. |
||
86 | * |
||
87 | * @param string $path |
||
88 | * @param string $contents |
||
89 | * @param Config $config Config object |
||
90 | * |
||
91 | 4 | * @return array |
|
92 | */ |
||
93 | 4 | public function write($path, $contents, Config $config) |
|
94 | { |
||
95 | return $this->upload($path, $contents, $config); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Update a file. |
||
100 | * |
||
101 | * @param string $path |
||
102 | * @param string $contents |
||
103 | * @param Config $config Config object |
||
104 | * |
||
105 | 2 | * @return array |
|
106 | */ |
||
107 | 2 | public function update($path, $contents, Config $config) |
|
108 | { |
||
109 | return $this->upload($path, $contents, $config); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Write a new file using a stream. |
||
114 | * |
||
115 | * @param string $path |
||
116 | * @param resource $resource |
||
117 | * @param Config $config Config object |
||
118 | * |
||
119 | 2 | * @return array|false false on failure file meta data on success |
|
120 | */ |
||
121 | 2 | public function writeStream($path, $resource, Config $config) |
|
122 | { |
||
123 | return $this->upload($path, $resource, $config); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Update a file using a stream. |
||
128 | * |
||
129 | * @param string $path |
||
130 | * @param resource $resource |
||
131 | * @param Config $config Config object |
||
132 | * |
||
133 | 2 | * @return array|false false on failure file meta data on success |
|
134 | */ |
||
135 | 2 | public function updateStream($path, $resource, Config $config) |
|
136 | { |
||
137 | return $this->upload($path, $resource, $config); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Rename a file. |
||
142 | * |
||
143 | * @param string $path |
||
144 | * @param string $newpath |
||
145 | * |
||
146 | 4 | * @return bool |
|
147 | */ |
||
148 | 4 | public function rename($path, $newpath) |
|
149 | 2 | { |
|
150 | if (!$this->copy($path, $newpath)) { |
||
151 | return false; |
||
152 | 2 | } |
|
153 | |||
154 | return $this->delete($path); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Copy a file. |
||
159 | * |
||
160 | * @param string $path |
||
161 | * @param string $newpath |
||
162 | * |
||
163 | 8 | * @return bool |
|
164 | */ |
||
165 | public function copy($path, $newpath) |
||
166 | 8 | { |
|
167 | 8 | try { |
|
168 | 8 | $this->ossClient->copyObject( |
|
169 | 8 | $this->bucket, |
|
170 | 8 | $this->applyPathPrefix($path), |
|
171 | 8 | $this->bucket, |
|
172 | 8 | $this->applyPathPrefix($newpath), |
|
173 | 8 | $this->options |
|
174 | 4 | ); |
|
175 | } catch (OssException $e) { |
||
176 | return false; |
||
177 | 4 | } |
|
178 | |||
179 | return true; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Delete a file. |
||
184 | * |
||
185 | * @param string $path |
||
186 | * |
||
187 | 4 | * @return bool |
|
188 | */ |
||
189 | 4 | public function delete($path) |
|
190 | { |
||
191 | 4 | $object = $this->applyPathPrefix($path); |
|
192 | |||
193 | 4 | $this->ossClient->deleteObject($this->bucket, $object, $this->options); |
|
194 | |||
195 | return !$this->has($path); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Delete a directory. |
||
200 | * |
||
201 | * @param string $dirname |
||
202 | * |
||
203 | 2 | * @return bool |
|
204 | */ |
||
205 | 2 | public function deleteDir($dirname) |
|
206 | { |
||
207 | return false; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Create a directory. |
||
212 | * |
||
213 | * @param string $dirname directory name |
||
214 | * @param Config $config |
||
215 | * |
||
216 | 4 | * @return array|false |
|
217 | */ |
||
218 | public function createDir($dirname, Config $config) |
||
219 | 4 | { |
|
220 | 4 | try { |
|
221 | $key = $this->applyPathPrefix($dirname); |
||
222 | 4 | $options = $this->getOptionsFromConfig($config); |
|
223 | 4 | ||
224 | 2 | $response = $this->ossClient->createObjectDir($this->bucket, rtrim($key, '/'), $options); |
|
|
|||
225 | } catch (OssException $e) { |
||
226 | return false; |
||
227 | 2 | } |
|
228 | |||
229 | return $this->normalizeResponse($response, $key); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Set the visibility for a file. |
||
234 | * |
||
235 | * @param string $path |
||
236 | * @param string $visibility |
||
237 | * |
||
238 | 6 | * @return array|false file meta data |
|
239 | */ |
||
240 | public function setVisibility($path, $visibility) |
||
241 | 6 | { |
|
242 | 6 | try { |
|
243 | 6 | $this->ossClient->putObjectAcl( |
|
244 | 6 | $this->bucket, |
|
245 | 6 | $this->applyPathPrefix($path), |
|
246 | 6 | in_array($visibility, OssClient::$OSS_ACL_TYPES) ? $visibility : 'default' |
|
247 | 2 | ); |
|
248 | } catch (OssException $e) { |
||
249 | return false; |
||
250 | 4 | } |
|
251 | |||
252 | return compact('path', 'visibility'); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Check whether a file exists. |
||
257 | * |
||
258 | * @param string $path |
||
259 | * |
||
260 | 12 | * @return bool |
|
261 | */ |
||
262 | 12 | public function has($path) |
|
263 | { |
||
264 | 12 | $location = $this->applyPathPrefix($path); |
|
265 | 2 | ||
266 | if ($this->ossClient->doesObjectExist($this->bucket, $location, $this->options)) { |
||
267 | return true; |
||
268 | 10 | } |
|
269 | |||
270 | return $this->doesDirectoryExist($location); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Read a file. |
||
275 | * |
||
276 | * @param string $path |
||
277 | * |
||
278 | 6 | * @return array|false |
|
279 | */ |
||
280 | public function read($path) |
||
281 | 6 | { |
|
282 | try { |
||
283 | 2 | $response = $this->ossClient->getObject($this->bucket, $this->applyPathPrefix($path)); |
|
284 | 2 | ||
285 | 2 | return $this->normalizeResponse([ |
|
286 | 4 | 'contents' => $response, |
|
287 | 4 | ], $path); |
|
288 | } catch (OssException $e) { |
||
289 | return false; |
||
290 | } |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Read a file as a stream. |
||
295 | * |
||
296 | * @param string $path |
||
297 | * |
||
298 | 4 | * @return array|false |
|
299 | */ |
||
300 | public function readStream($path) |
||
301 | 4 | { |
|
302 | 4 | try { |
|
303 | 2 | $object = $this->applyPathPrefix($path); |
|
304 | $url = $this->ossClient->signUrl($this->bucket, $object, 60, OssClient::OSS_HTTP_GET, $this->options); |
||
305 | 2 | $handle = fopen($url, 'r'); |
|
306 | 2 | ||
307 | 2 | return $this->normalizeResponse([ |
|
308 | 2 | 'stream' => $handle, |
|
309 | 2 | ], $path); |
|
310 | } catch (OssException $e) { |
||
311 | return false; |
||
312 | } |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * List contents of a directory. |
||
317 | * |
||
318 | * @param string $directory |
||
319 | * @param bool $recursive |
||
320 | * |
||
321 | 6 | * @return array|false |
|
322 | */ |
||
323 | 6 | public function listContents($directory = '', $recursive = false) |
|
324 | { |
||
325 | $prefix = $this->applyPathPrefix(rtrim($directory, '/').'/'); |
||
326 | 6 | ||
327 | 6 | $options = [ |
|
328 | 6 | OssClient::OSS_PREFIX => ltrim($prefix, '/'), |
|
329 | 6 | OssClient::OSS_MARKER => '', |
|
330 | OssClient::OSS_MAX_KEYS => 100, |
||
331 | 6 | ]; |
|
332 | 2 | ||
333 | 2 | if ($recursive) { |
|
334 | 4 | $options[OssClient::OSS_DELIMITER] = ''; |
|
335 | } else { |
||
336 | $options[OssClient::OSS_DELIMITER] = '/'; |
||
337 | } |
||
338 | 6 | ||
339 | 6 | $listing = $this->retrieveListing($options); |
|
340 | 2 | ||
341 | $normalizer = [$this, 'normalizeResponse']; |
||
342 | $normalized = array_map($normalizer, $listing); |
||
343 | 4 | ||
344 | 4 | return Util::emulateDirectories($normalized); |
|
345 | } |
||
346 | 4 | ||
347 | /** |
||
348 | * @param array $options |
||
349 | * |
||
350 | * @return array |
||
351 | * |
||
352 | * @throws OssException |
||
353 | */ |
||
354 | protected function retrieveListing(array $options) |
||
355 | 6 | { |
|
356 | $result = $this->ossClient->listObjects($this->bucket, $options); |
||
357 | 6 | $listing = []; |
|
358 | 4 | ||
359 | foreach ($result->getObjectList() as $object) { |
||
360 | 4 | $listing[] = [ |
|
361 | 4 | 'key' => $object->getKey(), |
|
362 | 4 | 'last_modified' => $object->getLastModified(), |
|
363 | 4 | 'etag' => $object->getETag(), |
|
364 | 4 | 'type' => $object->getType(), |
|
365 | 4 | 'size' => $object->getSize(), |
|
366 | 4 | 'storage_class' => $object->getStorageClass(), |
|
367 | 4 | ]; |
|
368 | } |
||
369 | 4 | ||
370 | foreach ($result->getPrefixList() as $object) { |
||
371 | 4 | $listing[] = [ |
|
372 | 4 | 'prefix' => $object->getPrefix(), |
|
373 | 4 | ]; |
|
374 | } |
||
375 | 4 | ||
376 | return $listing; |
||
377 | 4 | } |
|
378 | |||
379 | /** |
||
380 | * Get all the meta data of a file or directory. |
||
381 | * |
||
382 | * @param string $path |
||
383 | * |
||
384 | * @return array|false |
||
385 | */ |
||
386 | public function getMetadata($path) |
||
387 | 12 | { |
|
388 | $response = $this->getObjectMeta($path); |
||
389 | 12 | ||
390 | if (empty($response)) { |
||
391 | 10 | return false; |
|
392 | 10 | } |
|
393 | |||
394 | return $this->normalizeResponse($response, $path); |
||
395 | 8 | } |
|
396 | |||
397 | /** |
||
398 | * Get the size of a file. |
||
399 | * |
||
400 | * @param string $path |
||
401 | * |
||
402 | * @return array|false |
||
403 | */ |
||
404 | public function getSize($path) |
||
405 | 2 | { |
|
406 | if ($metaData = $this->getMetadata($path)) { |
||
407 | 2 | return $this->normalizeResponse([ |
|
408 | 2 | 'size' => $metaData['content-length'], |
|
409 | 2 | ], $path); |
|
410 | 2 | } |
|
411 | |||
412 | return false; |
||
413 | 2 | } |
|
414 | |||
415 | /** |
||
416 | * Get the mimetype of a file. |
||
417 | * |
||
418 | * @param string $path |
||
419 | * |
||
420 | * @return array|false |
||
421 | */ |
||
422 | public function getMimetype($path) |
||
423 | 2 | { |
|
424 | if ($metaData = $this->getMetadata($path)) { |
||
425 | 2 | $contentType = array_key_exists('content_type', |
|
426 | 2 | $metaData) ? $metaData['content_type'] : $metaData['info']['content_type']; |
|
427 | 2 | ||
428 | return [ |
||
429 | 'mimetype' => $contentType, |
||
430 | 2 | ]; |
|
431 | 2 | } |
|
432 | |||
433 | return false; |
||
434 | 2 | } |
|
435 | |||
436 | /** |
||
437 | * Get the last modified time of a file as a timestamp. |
||
438 | * |
||
439 | * @param string $path |
||
440 | * |
||
441 | * @return array|false |
||
442 | */ |
||
443 | public function getTimestamp($path) |
||
444 | 2 | { |
|
445 | if ($metaData = $this->getMetadata($path)) { |
||
446 | 2 | return $this->normalizeResponse([ |
|
447 | 2 | 'timestamp' => strtotime($metaData['last-modified']), |
|
448 | 2 | ], $path); |
|
449 | 2 | } |
|
450 | |||
451 | return false; |
||
452 | 2 | } |
|
453 | |||
454 | /** |
||
455 | * Get the visibility of a file. |
||
456 | * |
||
457 | * @param string $path |
||
458 | * |
||
459 | * @return array|false |
||
460 | */ |
||
461 | public function getVisibility($path) |
||
462 | 6 | { |
|
463 | try { |
||
464 | return [ |
||
465 | 'visibility' => $this->ossClient->getObjectAcl($this->bucket, $this->applyPathPrefix($path)), |
||
466 | 6 | ]; |
|
467 | 4 | } catch (OssException $e) { |
|
468 | 2 | return false; |
|
469 | 2 | } |
|
470 | } |
||
471 | |||
472 | /** |
||
473 | * {@inheritdoc} |
||
474 | */ |
||
475 | public function applyPathPrefix($path) |
||
476 | 74 | { |
|
477 | return ltrim(parent::applyPathPrefix($path), '/'); |
||
478 | 74 | } |
|
479 | |||
480 | /** |
||
481 | * {@inheritdoc} |
||
482 | */ |
||
483 | public function setPathPrefix($prefix) |
||
484 | 86 | { |
|
485 | $prefix = ltrim($prefix, '/'); |
||
486 | 86 | ||
487 | parent::setPathPrefix($prefix); |
||
488 | 86 | } |
|
489 | 86 | ||
490 | /** |
||
491 | * Get the object meta. |
||
492 | * |
||
493 | * @param $path |
||
494 | * |
||
495 | * @return array|bool |
||
496 | */ |
||
497 | protected function getObjectMeta($path) |
||
498 | 12 | { |
|
499 | try { |
||
500 | return $this->ossClient->getObjectMeta($this->bucket, $this->applyPathPrefix($path)); |
||
501 | 12 | } catch (OssException $e) { |
|
502 | 12 | if (404 === $e->getHTTPStatus()) { |
|
503 | 12 | return false; |
|
504 | 10 | } |
|
505 | |||
506 | throw $e; |
||
507 | 2 | } |
|
508 | } |
||
509 | |||
510 | /** |
||
511 | * Upload an object. |
||
512 | * |
||
513 | * @param string $path |
||
514 | * @param string|resource $body |
||
515 | * @param Config $config |
||
516 | * |
||
517 | * @return array|false |
||
518 | * |
||
519 | * @throws \InvalidArgumentException |
||
520 | */ |
||
521 | 10 | protected function upload($path, $body, Config $config) |
|
539 | |||
540 | /** |
||
541 | * Get options from the config. |
||
542 | * |
||
543 | * @param Config $config |
||
544 | * @param array $keys |
||
545 | * |
||
546 | * @return array |
||
547 | */ |
||
548 | 14 | protected function getOptionsFromConfig(Config $config, $keys = []) |
|
560 | |||
561 | /** |
||
562 | * Normalize the object result array. |
||
563 | * |
||
564 | * @param array $response |
||
565 | * @param null $path |
||
566 | * |
||
567 | * @return array |
||
568 | */ |
||
569 | 26 | protected function normalizeResponse(array $response, $path = null) |
|
590 | |||
591 | /** |
||
592 | * @param $location |
||
593 | * |
||
594 | * @return bool |
||
595 | */ |
||
596 | 10 | protected function doesDirectoryExist($location) |
|
615 | } |
||
616 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.