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 | * DB app context. |
||
48 | */ |
||
49 | const CONTEXT_APP = 'app'; |
||
50 | |||
51 | /** |
||
52 | * DB user context. |
||
53 | */ |
||
54 | const CONTEXT_USER = 'user'; |
||
55 | |||
56 | /** |
||
57 | * Requested version of API |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | private $version = 'v1'; |
||
62 | |||
63 | /** |
||
64 | * API domain |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $serviceDomain = 'cloud-api.yandex.net'; |
||
69 | |||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | private $context; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | private $databaseId; |
||
79 | |||
80 | /** |
||
81 | * @return string |
||
82 | * @throws InvalidArgumentException |
||
83 | */ |
||
84 | 27 | public function getDatabaseId() |
|
92 | |||
93 | /** |
||
94 | * @param string $databaseId |
||
95 | */ |
||
96 | 26 | public function setDatabaseId($databaseId) |
|
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | * @throws InvalidArgumentException |
||
104 | */ |
||
105 | 29 | public function getContext() |
|
112 | |||
113 | /** |
||
114 | * @param string $context |
||
115 | * |
||
116 | * @throws InvalidArgumentException |
||
117 | */ |
||
118 | 29 | public function setContext($context) |
|
126 | |||
127 | /** |
||
128 | * @param string $token access token |
||
129 | * @param null $context |
||
130 | * @param null $databaseId |
||
131 | */ |
||
132 | 31 | public function __construct($token = '', $context = null, $databaseId = null) |
|
144 | |||
145 | /** |
||
146 | * @param null|string $context |
||
147 | * @param array $fields |
||
148 | * @param null $limit |
||
149 | * @param null $offset |
||
150 | * |
||
151 | * @return string |
||
152 | * @throws InvalidArgumentException |
||
153 | */ |
||
154 | 2 | protected function getDatabasesUrl($context = null, $fields = [], $limit = null, $offset = null) |
|
174 | |||
175 | /** |
||
176 | * @param null|string $databaseId |
||
177 | * @param null|string $context |
||
178 | * @param array $fields |
||
179 | * |
||
180 | * @return string |
||
181 | * @throws InvalidArgumentException |
||
182 | */ |
||
183 | 19 | protected function getDatabaseUrl($databaseId = null, $context = null, $fields = []) |
|
200 | |||
201 | /** |
||
202 | * @param null|string $databaseId |
||
203 | * @param null|string $context |
||
204 | * @param null $collectionId |
||
205 | * @param array $fields |
||
206 | * |
||
207 | * @return string |
||
208 | * @throws InvalidArgumentException |
||
209 | */ |
||
210 | 2 | protected function getDatabaseSnapshotUrl($databaseId = null, $context = null, $collectionId = null, $fields = []) |
|
230 | |||
231 | /** |
||
232 | * @param null|string $databaseId |
||
233 | * @param null|string $context |
||
234 | * @param array $fields |
||
235 | * @param int $baseRevision |
||
236 | * @param int $limit |
||
237 | * |
||
238 | * @return string |
||
239 | * @throws InvalidArgumentException |
||
240 | */ |
||
241 | 4 | protected function getDatabaseDeltasUrl( |
|
268 | |||
269 | /** |
||
270 | * Sends a request |
||
271 | * |
||
272 | * @param string $method HTTP method |
||
273 | * @param UriInterface|string $uri URI object or string. |
||
274 | * @param array $options Request options to apply. |
||
275 | * |
||
276 | * @return Response|\Psr\Http\Message\ResponseInterface |
||
277 | * @throws DataSyncException |
||
278 | * @throws ForbiddenException |
||
279 | * @throws IncorrectDataFormatException |
||
280 | * @throws IncorrectRevisionNumberException |
||
281 | * @throws InvalidArgumentException |
||
282 | * @throws MaxDatabasesCountException |
||
283 | * @throws NotFoundException |
||
284 | * @throws RevisionOnServerOverCurrentException |
||
285 | * @throws RevisionTooOldException |
||
286 | * @throws TooManyRequestsException |
||
287 | * @throws UnauthorizedException |
||
288 | * @throws UnavailableResourceException |
||
289 | */ |
||
290 | 14 | protected function sendRequest($method, $uri, array $options = []) |
|
334 | |||
335 | /** |
||
336 | * @param null|string $context |
||
337 | * @param array $fields |
||
338 | * @param null $limit |
||
339 | * @param null $offset |
||
340 | * |
||
341 | * @return DatabasesResponse |
||
342 | * @throws DataSyncException |
||
343 | * @throws ForbiddenException |
||
344 | * @throws IncorrectDataFormatException |
||
345 | * @throws InvalidArgumentException |
||
346 | * @throws MaxDatabasesCountException |
||
347 | * @throws NotFoundException |
||
348 | * @throws TooManyRequestsException |
||
349 | * @throws UnauthorizedException |
||
350 | * @throws UnavailableResourceException |
||
351 | */ |
||
352 | 2 | public function getDatabases($context = null, $fields = [], $limit = null, $offset = null) |
|
366 | |||
367 | /** |
||
368 | * @param null|string $databaseId |
||
369 | * @param null|string $context |
||
370 | * @param array $fields |
||
371 | * |
||
372 | * @return Database |
||
373 | * @throws DataSyncException |
||
374 | * @throws ForbiddenException |
||
375 | * @throws IncorrectDataFormatException |
||
376 | * @throws InvalidArgumentException |
||
377 | * @throws MaxDatabasesCountException |
||
378 | * @throws NotFoundException |
||
379 | * @throws TooManyRequestsException |
||
380 | * @throws UnauthorizedException |
||
381 | * @throws UnavailableResourceException |
||
382 | */ |
||
383 | 1 | View Code Duplication | public function createDatabase($databaseId = null, $context = null, $fields = []) |
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 | 16 | View Code Duplication | public function getDatabase($databaseId = null, $context = null, $fields = []) |
416 | |||
417 | /** |
||
418 | * @param null|string $databaseId |
||
419 | * @param null|string $context |
||
420 | * |
||
421 | * @return bool |
||
422 | * |
||
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 | 1 | public function deleteDatabase($databaseId = null, $context = null) |
|
438 | |||
439 | /** |
||
440 | * @param string $title |
||
441 | * @param null $databaseId |
||
442 | * @param null $context |
||
443 | * @param array $fields |
||
444 | * |
||
445 | * @return Database |
||
446 | * @throws DataSyncException |
||
447 | * @throws ForbiddenException |
||
448 | * @throws IncorrectDataFormatException |
||
449 | * @throws InvalidArgumentException |
||
450 | * @throws MaxDatabasesCountException |
||
451 | * @throws NotFoundException |
||
452 | * @throws TooManyRequestsException |
||
453 | * @throws UnauthorizedException |
||
454 | * @throws UnavailableResourceException |
||
455 | */ |
||
456 | 1 | public function updateDatabaseTitle($title, $databaseId = null, $context = null, $fields = []) |
|
470 | |||
471 | /** |
||
472 | * @param null|string $databaseId |
||
473 | * @param null|string $context |
||
474 | * @param null $collectionId |
||
475 | * @param array $fields |
||
476 | * |
||
477 | * @return DatabaseSnapshotResponse |
||
478 | * @throws DataSyncException |
||
479 | * @throws ForbiddenException |
||
480 | * @throws IncorrectDataFormatException |
||
481 | * @throws InvalidArgumentException |
||
482 | * @throws MaxDatabasesCountException |
||
483 | * @throws NotFoundException |
||
484 | * @throws TooManyRequestsException |
||
485 | * @throws UnauthorizedException |
||
486 | * @throws UnavailableResourceException |
||
487 | */ |
||
488 | 2 | View Code Duplication | public function getDatabaseSnapshot($databaseId = null, $context = null, $collectionId = null, $fields = []) |
498 | |||
499 | /** |
||
500 | * @param array $data |
||
501 | * @param int $revision |
||
502 | * @param null|string $databaseId |
||
503 | * @param null|string $context |
||
504 | * @param array $fields |
||
505 | * |
||
506 | * @return array |
||
507 | * @throws DataSyncException |
||
508 | * @throws ForbiddenException |
||
509 | * @throws IncorrectDataFormatException |
||
510 | * @throws IncorrectRevisionNumberException |
||
511 | * @throws InvalidArgumentException |
||
512 | * @throws MaxDatabasesCountException |
||
513 | * @throws NotFoundException |
||
514 | * @throws RevisionOnServerOverCurrentException |
||
515 | * @throws RevisionTooOldException |
||
516 | * @throws TooManyRequestsException |
||
517 | * @throws UnauthorizedException |
||
518 | * @throws UnavailableResourceException |
||
519 | * |
||
520 | * @see https://tech.yandex.ru/datasync/http/doc/tasks/add-changes-docpage/ |
||
521 | */ |
||
522 | 2 | public function saveDelta($data, $revision = 0, $databaseId = null, $context = null, $fields = []) |
|
545 | |||
546 | /** |
||
547 | * @param int $baseRevision |
||
548 | * @param null|string $databaseId |
||
549 | * @param null|string $context |
||
550 | * @param array $fields |
||
551 | * @param null|int $limit |
||
552 | * |
||
553 | * @return DatabaseDeltasResponse |
||
554 | * @throws DataSyncException |
||
555 | * @throws ForbiddenException |
||
556 | * @throws IncorrectDataFormatException |
||
557 | * @throws IncorrectRevisionNumberException |
||
558 | * @throws InvalidArgumentException |
||
559 | * @throws MaxDatabasesCountException |
||
560 | * @throws NotFoundException |
||
561 | * @throws RevisionOnServerOverCurrentException |
||
562 | * @throws RevisionTooOldException |
||
563 | * @throws TooManyRequestsException |
||
564 | * @throws UnauthorizedException |
||
565 | * @throws UnavailableResourceException |
||
566 | */ |
||
567 | 2 | View Code Duplication | public function getDelta($baseRevision = 0, $databaseId = null, $context = null, $fields = [], $limit = null) |
577 | } |
||
578 |
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: