Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DiskClient 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 DiskClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class DiskClient extends AbstractServiceClient |
||
| 32 | { |
||
| 33 | const DECODE_TYPE_DEFAULT = self::DECODE_TYPE_XML; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $version = 'v1'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $serviceDomain = 'webdav.yandex.ru'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param string $version |
||
| 47 | * |
||
| 48 | * @return self |
||
| 49 | */ |
||
| 50 | 1 | public function setVersion($version) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | 2 | public function getVersion() |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @inheritdoc |
||
| 67 | */ |
||
| 68 | 2 | public function getServiceUrl($resource = '') |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @param $path |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | 1 | public function getRequestUrl($path) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @param string $token access token |
||
| 84 | */ |
||
| 85 | 25 | public function __construct($token = '') |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Sends a request |
||
| 92 | * |
||
| 93 | * @param string $method HTTP method |
||
| 94 | * @param string|UriInterface $uri URI object or string. |
||
| 95 | * @param array $options Request options to apply. |
||
| 96 | * |
||
| 97 | * @throws \Exception|\GuzzleHttp\Exception\ClientException |
||
| 98 | * @return Response |
||
| 99 | */ |
||
| 100 | 20 | protected function sendRequest($method, $uri, array $options = []) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $path |
||
| 120 | * @return bool |
||
| 121 | * |
||
| 122 | * @see https://tech.yandex.com/disk/doc/dg/reference/mkcol-docpage/ |
||
| 123 | */ |
||
| 124 | 1 | public function createDirectory($path = '') |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $path |
||
| 131 | * @param null $offset |
||
| 132 | * @param null $amount |
||
| 133 | * @return array |
||
| 134 | * |
||
| 135 | * @see https://tech.yandex.com/disk/doc/dg/reference/propfind_contains-request-docpage/ |
||
| 136 | */ |
||
| 137 | 2 | public function directoryContents($path = '/', $offset = null, $amount = null) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @return array |
||
| 176 | * |
||
| 177 | * @see https://tech.yandex.com/disk/doc/dg/reference/propfind_space-request-docpage/ |
||
| 178 | */ |
||
| 179 | 1 | public function diskSpaceInfo() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $path |
||
| 206 | * @param string $property |
||
| 207 | * @param string $value |
||
| 208 | * @param string $namespace |
||
| 209 | * @return bool |
||
| 210 | * |
||
| 211 | * @see https://tech.yandex.com/disk/doc/dg/reference/proppatch-docpage/ |
||
| 212 | */ |
||
| 213 | 3 | public function setProperty($path = '', $property = '', $value = '', $namespace = 'default:namespace') |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @param string $path |
||
| 244 | * @param string $property |
||
| 245 | * @param string $namespace |
||
| 246 | * @return string|false |
||
| 247 | * |
||
| 248 | * @see https://tech.yandex.com/disk/doc/dg/reference/propfind_property-request-docpage/ |
||
| 249 | */ |
||
| 250 | 3 | public function getProperty($path = '', $property = '', $namespace = 'default:namespace') |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @return string |
||
| 282 | * @see https://tech.yandex.com/disk/doc/dg/reference/userinfo-docpage/ |
||
| 283 | */ |
||
| 284 | 1 | public function getLogin() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $path |
||
| 297 | * @return array |
||
| 298 | * |
||
| 299 | * @see https://tech.yandex.com/disk/doc/dg/reference/get-docpage/ |
||
| 300 | */ |
||
| 301 | 1 | public function getFile($path = '') |
|
| 302 | { |
||
| 303 | 1 | $response = $this->sendRequest('GET', $path); |
|
| 304 | |||
| 305 | 1 | $result = []; |
|
| 306 | 1 | View Code Duplication | foreach ($response->getHeaders() as $key => $value) { |
| 307 | 1 | $result['headers'][strtolower($key)] = $value[0]; |
|
| 308 | 1 | } |
|
| 309 | 1 | $result['body'] = $response->getBody(); |
|
| 310 | 1 | return $result; |
|
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $path |
||
| 315 | * @param string $destination |
||
| 316 | * @param string $name |
||
| 317 | * @return string|false |
||
| 318 | * |
||
| 319 | * @see https://tech.yandex.com/disk/doc/dg/reference/get-docpage/ |
||
| 320 | */ |
||
| 321 | 2 | public function downloadFile($path = '', $destination = '', $name = '') |
|
| 343 | |||
| 344 | /** |
||
| 345 | * @param string $path |
||
| 346 | * @param array $file |
||
| 347 | * @param array $extraHeaders |
||
| 348 | * @return Response |
||
| 349 | * |
||
| 350 | * @see https://tech.yandex.com/disk/doc/dg/reference/put-docpage/ |
||
| 351 | */ |
||
| 352 | 1 | public function uploadFile($path = '', $file = null, $extraHeaders = null) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * @param string $path |
||
| 380 | * @param array $file |
||
| 381 | * @param array $extraHeaders |
||
| 382 | * |
||
| 383 | * @return Response |
||
| 384 | * |
||
| 385 | 1 | * @see https://tech.yandex.com/disk/doc/dg/reference/put-docpage/ |
|
| 386 | * @throws \Exception|ClientException |
||
| 387 | 1 | */ |
|
| 388 | 1 | public function uploadRemoteFile($path = '', $file = null, $extraHeaders = null) |
|
| 417 | 1 | ||
| 418 | 1 | /** |
|
| 419 | * @param $path |
||
| 420 | * @param $size |
||
| 421 | * @return array |
||
| 422 | 1 | * |
|
| 423 | 1 | * @see https://tech.yandex.com/disk/doc/dg/reference/preview-docpage/ |
|
| 424 | 1 | */ |
|
| 425 | public function getImagePreview($path, $size) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param string $target |
||
| 449 | * @param string $destination |
||
| 450 | * @return bool |
||
| 451 | * |
||
| 452 | * @see https://tech.yandex.com/disk/doc/dg/reference/copy-docpage/ |
||
| 453 | 1 | */ |
|
| 454 | public function copy($target = '', $destination = '') |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param string $path |
||
| 469 | 1 | * @param string $destination |
|
| 470 | * @return bool |
||
| 471 | 1 | * |
|
| 472 | 1 | * @see https://tech.yandex.com/disk/doc/dg/reference/move-docpage/ |
|
| 473 | 1 | */ |
|
| 474 | public function move($path = '', $destination = '') |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @param string $path |
||
| 489 | * @return bool |
||
| 490 | * |
||
| 491 | * @see https://tech.yandex.com/disk/doc/dg/reference/delete-docpage/ |
||
| 492 | */ |
||
| 493 | public function delete($path = '') |
||
| 497 | |||
| 498 | 1 | /** |
|
| 499 | * @param string $path |
||
| 500 | 1 | * @return string |
|
| 501 | 1 | * |
|
| 502 | 1 | * @see https://tech.yandex.com/disk/doc/dg/reference/publish-docpage/#publish-s_1 |
|
| 503 | * @see https://tech.yandex.com/disk/doc/dg/reference/publish-docpage/#publish-s |
||
| 504 | */ |
||
| 505 | 1 | public function startPublishing($path = '') |
|
| 527 | 1 | ||
| 528 | /** |
||
| 529 | 1 | * @param string $path |
|
| 530 | * @return void |
||
| 531 | 1 | * |
|
| 532 | 1 | * @see @https://tech.yandex.com/disk/doc/dg/reference/publish-docpage/#unpublish |
|
| 533 | */ |
||
| 534 | 1 | public function stopPublishing($path = '') |
|
| 551 | |||
| 552 | /** |
||
| 553 | * @param string $path |
||
| 554 | * @return string|bool |
||
| 555 | * |
||
| 556 | * @see https://tech.yandex.com/disk/doc/dg/reference/publish-docpage/#unpublish_1 |
||
| 557 | */ |
||
| 558 | public function checkPublishing($path = '') |
||
| 579 | } |
||
| 580 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.