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 DataSyncClient 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 DataSyncClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class DataSyncClient extends AbstractServiceClient |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * Requested version of API |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $version = 'v1'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * API domain |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $serviceDomain = 'cloud-api.yandex.net'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * DB app context. |
||
| 62 | */ |
||
| 63 | const CONTEXT_APP = 'app'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * DB user context. |
||
| 67 | */ |
||
| 68 | const CONTEXT_USER = 'user'; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | private $context; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | private $databaseId; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | 1 | public function getDatabaseId() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $databaseId |
||
| 90 | */ |
||
| 91 | 26 | public function setDatabaseId($databaseId) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | 7 | public function getContext() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $context |
||
| 106 | * |
||
| 107 | * @throws InvalidArgumentException |
||
| 108 | */ |
||
| 109 | 32 | public function setContext($context) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $token access token |
||
| 120 | * @param null $context |
||
| 121 | * @param null $databaseId |
||
| 122 | */ |
||
| 123 | 36 | public function __construct($token = '', $context = null, $databaseId = null) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @param null|string $context |
||
| 138 | * @param array $fields |
||
| 139 | * @param null $limit |
||
| 140 | * @param null $offset |
||
| 141 | * |
||
| 142 | * @return string |
||
| 143 | * @throws InvalidArgumentException |
||
| 144 | */ |
||
| 145 | 3 | public function getDatabasesUrl($context = null, $fields = [], $limit = null, $offset = null) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @param null|string $databaseId |
||
| 175 | * @param null|string $context |
||
| 176 | * @param array $fields |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | * @throws InvalidArgumentException |
||
| 180 | */ |
||
| 181 | 21 | public function getDatabaseUrl($databaseId = null, $context = null, $fields = []) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param null|string $databaseId |
||
| 209 | * @param null|string $context |
||
| 210 | * @param null $collectionId |
||
| 211 | * @param array $fields |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | * @throws InvalidArgumentException |
||
| 215 | */ |
||
| 216 | 4 | public function getDatabaseSnapshotUrl($databaseId = null, $context = null, $collectionId = null, $fields = []) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @param null|string $databaseId |
||
| 248 | * @param null|string $context |
||
| 249 | * @param array $fields |
||
| 250 | * @param int $baseRevision |
||
| 251 | * @param int $limit |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | * @throws InvalidArgumentException |
||
| 255 | */ |
||
| 256 | 6 | public function getDatabaseDeltasUrl( |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Sends a request |
||
| 296 | * |
||
| 297 | * @param string $method HTTP method |
||
| 298 | * @param UriInterface|string $uri URI object or string. |
||
| 299 | * @param array $options Request options to apply. |
||
| 300 | * |
||
| 301 | * @return Response|\Psr\Http\Message\ResponseInterface |
||
| 302 | * @throws DataSyncException |
||
| 303 | * @throws ForbiddenException |
||
| 304 | * @throws IncorrectDataFormatException |
||
| 305 | * @throws IncorrectRevisionNumberException |
||
| 306 | * @throws InvalidArgumentException |
||
| 307 | * @throws MaxDatabasesCountException |
||
| 308 | * @throws NotFoundException |
||
| 309 | * @throws RevisionOnServerOverCurrentException |
||
| 310 | * @throws RevisionTooOldException |
||
| 311 | * @throws TooManyRequestsException |
||
| 312 | * @throws UnauthorizedException |
||
| 313 | * @throws UnavailableResourceException |
||
| 314 | */ |
||
| 315 | 24 | protected function sendRequest($method, $uri, array $options = []) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * @param null|string $context |
||
| 362 | * @param array $fields |
||
| 363 | * @param null $limit |
||
| 364 | * @param null $offset |
||
| 365 | * |
||
| 366 | * @return DatabasesResponse |
||
| 367 | * @throws DataSyncException |
||
| 368 | * @throws ForbiddenException |
||
| 369 | * @throws IncorrectDataFormatException |
||
| 370 | * @throws InvalidArgumentException |
||
| 371 | * @throws MaxDatabasesCountException |
||
| 372 | * @throws NotFoundException |
||
| 373 | * @throws TooManyRequestsException |
||
| 374 | * @throws UnauthorizedException |
||
| 375 | * @throws UnavailableResourceException |
||
| 376 | */ |
||
| 377 | 2 | public function getDatabases($context = null, $fields = [], $limit = null, $offset = null) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * @param null|string $databaseId |
||
| 394 | * @param null|string $context |
||
| 395 | * @param array $fields |
||
| 396 | * |
||
| 397 | * @return Database |
||
| 398 | * @throws DataSyncException |
||
| 399 | * @throws ForbiddenException |
||
| 400 | * @throws IncorrectDataFormatException |
||
| 401 | * @throws InvalidArgumentException |
||
| 402 | * @throws MaxDatabasesCountException |
||
| 403 | * @throws NotFoundException |
||
| 404 | * @throws TooManyRequestsException |
||
| 405 | * @throws UnauthorizedException |
||
| 406 | * @throws UnavailableResourceException |
||
| 407 | */ |
||
| 408 | 1 | View Code Duplication | public function createDatabase($databaseId = null, $context = null, $fields = []) |
| 416 | |||
| 417 | /** |
||
| 418 | * @param null|string $databaseId |
||
| 419 | * @param null|string $context |
||
| 420 | * @param array $fields |
||
| 421 | * |
||
| 422 | * @return Database |
||
| 423 | * @throws DataSyncException |
||
| 424 | * @throws ForbiddenException |
||
| 425 | * @throws IncorrectDataFormatException |
||
| 426 | * @throws InvalidArgumentException |
||
| 427 | * @throws MaxDatabasesCountException |
||
| 428 | * @throws NotFoundException |
||
| 429 | * @throws TooManyRequestsException |
||
| 430 | * @throws UnauthorizedException |
||
| 431 | * @throws UnavailableResourceException |
||
| 432 | */ |
||
| 433 | 16 | View Code Duplication | public function getDatabase($databaseId = null, $context = null, $fields = []) |
| 441 | |||
| 442 | /** |
||
| 443 | * @param null|string $databaseId |
||
| 444 | * @param null|string $context |
||
| 445 | * |
||
| 446 | * @return bool |
||
| 447 | * |
||
| 448 | * @throws DataSyncException |
||
| 449 | * @throws ForbiddenException |
||
| 450 | * @throws IncorrectDataFormatException |
||
| 451 | * @throws InvalidArgumentException |
||
| 452 | * @throws MaxDatabasesCountException |
||
| 453 | * @throws NotFoundException |
||
| 454 | * @throws TooManyRequestsException |
||
| 455 | * @throws UnauthorizedException |
||
| 456 | * @throws UnavailableResourceException |
||
| 457 | */ |
||
| 458 | 1 | public function deleteDatabase($databaseId = null, $context = null) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * @param string $title |
||
| 466 | * @param null $databaseId |
||
| 467 | * @param null $context |
||
| 468 | * @param array $fields |
||
| 469 | * |
||
| 470 | * @return Database |
||
| 471 | * @throws DataSyncException |
||
| 472 | * @throws ForbiddenException |
||
| 473 | * @throws IncorrectDataFormatException |
||
| 474 | * @throws InvalidArgumentException |
||
| 475 | * @throws MaxDatabasesCountException |
||
| 476 | * @throws NotFoundException |
||
| 477 | * @throws TooManyRequestsException |
||
| 478 | * @throws UnauthorizedException |
||
| 479 | * @throws UnavailableResourceException |
||
| 480 | */ |
||
| 481 | 1 | public function updateDatabaseTitle($title, $databaseId = null, $context = null, $fields = []) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * @param null|string $databaseId |
||
| 498 | * @param null|string $context |
||
| 499 | * @param null $collectionId |
||
| 500 | * @param array $fields |
||
| 501 | * |
||
| 502 | * @return DatabaseSnapshotResponse |
||
| 503 | * @throws DataSyncException |
||
| 504 | * @throws ForbiddenException |
||
| 505 | * @throws IncorrectDataFormatException |
||
| 506 | * @throws InvalidArgumentException |
||
| 507 | * @throws MaxDatabasesCountException |
||
| 508 | * @throws NotFoundException |
||
| 509 | * @throws TooManyRequestsException |
||
| 510 | * @throws UnauthorizedException |
||
| 511 | * @throws UnavailableResourceException |
||
| 512 | */ |
||
| 513 | 2 | View Code Duplication | public function getDatabaseSnapshot($databaseId = null, $context = null, $collectionId = null, $fields = []) |
| 523 | |||
| 524 | /** |
||
| 525 | * @param array $data |
||
| 526 | * @param int $revision |
||
| 527 | * @param null|string $databaseId |
||
| 528 | * @param null|string $context |
||
| 529 | * @param array $fields |
||
| 530 | * |
||
| 531 | * @return array |
||
| 532 | * @throws DataSyncException |
||
| 533 | * @throws ForbiddenException |
||
| 534 | * @throws IncorrectDataFormatException |
||
| 535 | * @throws IncorrectRevisionNumberException |
||
| 536 | * @throws InvalidArgumentException |
||
| 537 | * @throws MaxDatabasesCountException |
||
| 538 | * @throws NotFoundException |
||
| 539 | * @throws RevisionOnServerOverCurrentException |
||
| 540 | * @throws RevisionTooOldException |
||
| 541 | * @throws TooManyRequestsException |
||
| 542 | * @throws UnauthorizedException |
||
| 543 | * @throws UnavailableResourceException |
||
| 544 | * |
||
| 545 | * @see https://tech.yandex.ru/datasync/http/doc/tasks/add-changes-docpage/ |
||
| 546 | */ |
||
| 547 | 2 | public function saveDelta($data, $revision = 0, $databaseId = null, $context = null, $fields = []) |
|
| 570 | |||
| 571 | /** |
||
| 572 | * @param int $baseRevision |
||
| 573 | * @param null|string $databaseId |
||
| 574 | * @param null|string $context |
||
| 575 | * @param array $fields |
||
| 576 | * @param null|int $limit |
||
| 577 | * |
||
| 578 | * @return DatabaseDeltasResponse |
||
| 579 | * @throws DataSyncException |
||
| 580 | * @throws ForbiddenException |
||
| 581 | * @throws IncorrectDataFormatException |
||
| 582 | * @throws IncorrectRevisionNumberException |
||
| 583 | * @throws InvalidArgumentException |
||
| 584 | * @throws MaxDatabasesCountException |
||
| 585 | * @throws NotFoundException |
||
| 586 | * @throws RevisionOnServerOverCurrentException |
||
| 587 | * @throws RevisionTooOldException |
||
| 588 | * @throws TooManyRequestsException |
||
| 589 | * @throws UnauthorizedException |
||
| 590 | * @throws UnavailableResourceException |
||
| 591 | */ |
||
| 592 | 2 | View Code Duplication | public function getDelta($baseRevision = 0, $databaseId = null, $context = null, $fields = [], $limit = null) |
| 602 | } |
||
| 603 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: