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 |
||
43 | class DataSyncClient extends AbstractServiceClient |
||
44 | { |
||
45 | /** |
||
46 | * DB app context. |
||
47 | */ |
||
48 | const CONTEXT_APP = 'app'; |
||
49 | |||
50 | /** |
||
51 | * DB user context. |
||
52 | */ |
||
53 | const CONTEXT_USER = 'user'; |
||
54 | |||
55 | /** |
||
56 | * Requested version of API |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | private $version = 'v1'; |
||
61 | |||
62 | /** |
||
63 | * API domain |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $serviceDomain = 'cloud-api.yandex.net'; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | private $context; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | private $databaseId; |
||
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | * @throws InvalidArgumentException |
||
82 | */ |
||
83 | 27 | public function getDatabaseId() |
|
91 | |||
92 | /** |
||
93 | * @param string $databaseId |
||
94 | */ |
||
95 | 26 | public function setDatabaseId($databaseId) |
|
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | * @throws InvalidArgumentException |
||
103 | */ |
||
104 | 29 | public function getContext() |
|
111 | |||
112 | /** |
||
113 | * @param string $context |
||
114 | * |
||
115 | * @throws InvalidArgumentException |
||
116 | */ |
||
117 | 29 | public function setContext($context) |
|
125 | |||
126 | /** |
||
127 | * @param string $token access token |
||
128 | * @param null $context |
||
129 | * @param null $databaseId |
||
130 | */ |
||
131 | 31 | public function __construct($token = '', $context = null, $databaseId = null) |
|
143 | |||
144 | /** |
||
145 | * @param null|string $context |
||
146 | * @param array $fields |
||
147 | * @param null $limit |
||
148 | * @param null $offset |
||
149 | * |
||
150 | * @return string |
||
151 | * @throws InvalidArgumentException |
||
152 | */ |
||
153 | 2 | protected function getDatabasesUrl($context = null, $fields = [], $limit = null, $offset = null) |
|
173 | |||
174 | /** |
||
175 | * @param null|string $databaseId |
||
176 | * @param null|string $context |
||
177 | * @param array $fields |
||
178 | * |
||
179 | * @return string |
||
180 | * @throws InvalidArgumentException |
||
181 | */ |
||
182 | 19 | protected function getDatabaseUrl($databaseId = null, $context = null, $fields = []) |
|
199 | |||
200 | /** |
||
201 | * @param null|string $databaseId |
||
202 | * @param null|string $context |
||
203 | * @param null $collectionId |
||
204 | * @param array $fields |
||
205 | * |
||
206 | * @return string |
||
207 | * @throws InvalidArgumentException |
||
208 | */ |
||
209 | 2 | protected function getDatabaseSnapshotUrl($databaseId = null, $context = null, $collectionId = null, $fields = []) |
|
229 | |||
230 | /** |
||
231 | * @param null|string $databaseId |
||
232 | * @param null|string $context |
||
233 | * @param array $fields |
||
234 | * @param int $baseRevision |
||
235 | * @param int $limit |
||
236 | * |
||
237 | * @return string |
||
238 | * @throws InvalidArgumentException |
||
239 | */ |
||
240 | 4 | protected function getDatabaseDeltasUrl( |
|
267 | |||
268 | /** |
||
269 | * Sends a request |
||
270 | * |
||
271 | * @param string $method HTTP method |
||
272 | * @param string $uri URI object or string. |
||
273 | * @param array $options Request options to apply. |
||
274 | * |
||
275 | * @return Response|\Psr\Http\Message\ResponseInterface |
||
276 | * @throws DataSyncException |
||
277 | * @throws ForbiddenException |
||
278 | * @throws IncorrectDataFormatException |
||
279 | * @throws IncorrectRevisionNumberException |
||
280 | * @throws InvalidArgumentException |
||
281 | * @throws MaxDatabasesCountException |
||
282 | * @throws NotFoundException |
||
283 | * @throws RevisionOnServerOverCurrentException |
||
284 | * @throws RevisionTooOldException |
||
285 | * @throws TooManyRequestsException |
||
286 | * @throws UnauthorizedException |
||
287 | * @throws UnavailableResourceException |
||
288 | */ |
||
289 | 14 | protected function sendRequest($method, $uri, array $options = []) |
|
333 | |||
334 | /** |
||
335 | * @param null|string $context |
||
336 | * @param array $fields |
||
337 | * @param null $limit |
||
338 | * @param null $offset |
||
339 | * |
||
340 | * @return DatabasesResponse |
||
341 | * @throws DataSyncException |
||
342 | * @throws ForbiddenException |
||
343 | * @throws IncorrectDataFormatException |
||
344 | * @throws InvalidArgumentException |
||
345 | * @throws MaxDatabasesCountException |
||
346 | * @throws NotFoundException |
||
347 | * @throws TooManyRequestsException |
||
348 | * @throws UnauthorizedException |
||
349 | * @throws UnavailableResourceException |
||
350 | */ |
||
351 | 2 | public function getDatabases($context = null, $fields = [], $limit = null, $offset = null) |
|
365 | |||
366 | /** |
||
367 | * @param null|string $databaseId |
||
368 | * @param null|string $context |
||
369 | * @param array $fields |
||
370 | * |
||
371 | * @return Database |
||
372 | * @throws DataSyncException |
||
373 | * @throws ForbiddenException |
||
374 | * @throws IncorrectDataFormatException |
||
375 | * @throws InvalidArgumentException |
||
376 | * @throws MaxDatabasesCountException |
||
377 | * @throws NotFoundException |
||
378 | * @throws TooManyRequestsException |
||
379 | * @throws UnauthorizedException |
||
380 | * @throws UnavailableResourceException |
||
381 | */ |
||
382 | 1 | View Code Duplication | public function createDatabase($databaseId = null, $context = null, $fields = []) |
390 | |||
391 | /** |
||
392 | * @param null|string $databaseId |
||
393 | * @param null|string $context |
||
394 | * @param array $fields |
||
395 | * |
||
396 | * @return Database |
||
397 | * @throws DataSyncException |
||
398 | * @throws ForbiddenException |
||
399 | * @throws IncorrectDataFormatException |
||
400 | * @throws InvalidArgumentException |
||
401 | * @throws MaxDatabasesCountException |
||
402 | * @throws NotFoundException |
||
403 | * @throws TooManyRequestsException |
||
404 | * @throws UnauthorizedException |
||
405 | * @throws UnavailableResourceException |
||
406 | */ |
||
407 | 16 | View Code Duplication | public function getDatabase($databaseId = null, $context = null, $fields = []) |
415 | |||
416 | /** |
||
417 | * @param null|string $databaseId |
||
418 | * @param null|string $context |
||
419 | * |
||
420 | * @return bool |
||
421 | * |
||
422 | * @throws DataSyncException |
||
423 | * @throws ForbiddenException |
||
424 | * @throws IncorrectDataFormatException |
||
425 | * @throws InvalidArgumentException |
||
426 | * @throws MaxDatabasesCountException |
||
427 | * @throws NotFoundException |
||
428 | * @throws TooManyRequestsException |
||
429 | * @throws UnauthorizedException |
||
430 | * @throws UnavailableResourceException |
||
431 | */ |
||
432 | 1 | public function deleteDatabase($databaseId = null, $context = null) |
|
437 | |||
438 | /** |
||
439 | * @param string $title |
||
440 | * @param null $databaseId |
||
441 | * @param null $context |
||
442 | * @param array $fields |
||
443 | * |
||
444 | * @return Database |
||
445 | * @throws DataSyncException |
||
446 | * @throws ForbiddenException |
||
447 | * @throws IncorrectDataFormatException |
||
448 | * @throws InvalidArgumentException |
||
449 | * @throws MaxDatabasesCountException |
||
450 | * @throws NotFoundException |
||
451 | * @throws TooManyRequestsException |
||
452 | * @throws UnauthorizedException |
||
453 | * @throws UnavailableResourceException |
||
454 | */ |
||
455 | 1 | public function updateDatabaseTitle($title, $databaseId = null, $context = null, $fields = []) |
|
469 | |||
470 | /** |
||
471 | * @param null|string $databaseId |
||
472 | * @param null|string $context |
||
473 | * @param null $collectionId |
||
474 | * @param array $fields |
||
475 | * |
||
476 | * @return DatabaseSnapshotResponse |
||
477 | * @throws DataSyncException |
||
478 | * @throws ForbiddenException |
||
479 | * @throws IncorrectDataFormatException |
||
480 | * @throws InvalidArgumentException |
||
481 | * @throws MaxDatabasesCountException |
||
482 | * @throws NotFoundException |
||
483 | * @throws TooManyRequestsException |
||
484 | * @throws UnauthorizedException |
||
485 | * @throws UnavailableResourceException |
||
486 | */ |
||
487 | 2 | View Code Duplication | public function getDatabaseSnapshot($databaseId = null, $context = null, $collectionId = null, $fields = []) |
497 | |||
498 | /** |
||
499 | * @param array $data |
||
500 | * @param int $revision |
||
501 | * @param null|string $databaseId |
||
502 | * @param null|string $context |
||
503 | * @param array $fields |
||
504 | * |
||
505 | * @return array |
||
506 | * @throws DataSyncException |
||
507 | * @throws ForbiddenException |
||
508 | * @throws IncorrectDataFormatException |
||
509 | * @throws IncorrectRevisionNumberException |
||
510 | * @throws InvalidArgumentException |
||
511 | * @throws MaxDatabasesCountException |
||
512 | * @throws NotFoundException |
||
513 | * @throws RevisionOnServerOverCurrentException |
||
514 | * @throws RevisionTooOldException |
||
515 | * @throws TooManyRequestsException |
||
516 | * @throws UnauthorizedException |
||
517 | * @throws UnavailableResourceException |
||
518 | * |
||
519 | * @see https://tech.yandex.ru/datasync/http/doc/tasks/add-changes-docpage/ |
||
520 | */ |
||
521 | 2 | public function saveDelta($data, $revision = 0, $databaseId = null, $context = null, $fields = []) |
|
544 | |||
545 | /** |
||
546 | * @param int $baseRevision |
||
547 | * @param null|string $databaseId |
||
548 | * @param null|string $context |
||
549 | * @param array $fields |
||
550 | * @param null|int $limit |
||
551 | * |
||
552 | * @return DatabaseDeltasResponse |
||
553 | * @throws DataSyncException |
||
554 | * @throws ForbiddenException |
||
555 | * @throws IncorrectDataFormatException |
||
556 | * @throws IncorrectRevisionNumberException |
||
557 | * @throws InvalidArgumentException |
||
558 | * @throws MaxDatabasesCountException |
||
559 | * @throws NotFoundException |
||
560 | * @throws RevisionOnServerOverCurrentException |
||
561 | * @throws RevisionTooOldException |
||
562 | * @throws TooManyRequestsException |
||
563 | * @throws UnauthorizedException |
||
564 | * @throws UnavailableResourceException |
||
565 | */ |
||
566 | 2 | View Code Duplication | public function getDelta($baseRevision = 0, $databaseId = null, $context = null, $fields = [], $limit = null) |
576 | } |
||
577 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: