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 Connect 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 Connect, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Connect implements Authentication, JsonSerializable |
||
24 | { |
||
25 | /** |
||
26 | * @const string |
||
27 | */ |
||
28 | const ENDPOINT_SANDBOX = 'https://connect-sandbox.moip.com.br'; |
||
29 | |||
30 | /** |
||
31 | * @const string |
||
32 | */ |
||
33 | const ENDPOINT_PRODUCTION = 'https://connect.moip.com.br'; |
||
34 | |||
35 | /** |
||
36 | * @const string |
||
37 | */ |
||
38 | const OAUTH_AUTHORIZE = '/oauth/authorize'; |
||
39 | |||
40 | /** |
||
41 | * @const string |
||
42 | */ |
||
43 | const OAUTH_TOKEN = '/oauth/token'; |
||
44 | |||
45 | /** |
||
46 | * Type of request desired. Possible values: AUTHORIZATION_CODE. |
||
47 | * |
||
48 | * @const string |
||
49 | */ |
||
50 | const GRANT_TYPE = 'authorization_code'; |
||
51 | |||
52 | /** |
||
53 | * Define the type of response to be obtained. Possible values: CODE. |
||
54 | * |
||
55 | * @const string |
||
56 | */ |
||
57 | const RESPONSE_TYPE = 'code'; |
||
58 | |||
59 | /** |
||
60 | * Permission for creation and consultation of ORDERS, PAYMENTS, MULTI ORDERS, MULTI PAYMENTS, CUSTOMERS and consultation of LAUNCHES. |
||
61 | * |
||
62 | * @const string |
||
63 | */ |
||
64 | const RECEIVE_FUNDS = 'RECEIVE_FUNDS'; |
||
65 | |||
66 | /** |
||
67 | * Permission to create and consult reimbursements of ORDERS, PAYMENTS. |
||
68 | * |
||
69 | * @const string |
||
70 | */ |
||
71 | const REFUND = 'REFUND'; |
||
72 | |||
73 | /** |
||
74 | * Permission to consult ACCOUNTS registration information. |
||
75 | * |
||
76 | * @const string |
||
77 | */ |
||
78 | const MANAGE_ACCOUNT_INFO = 'MANAGE_ACCOUNT_INFO'; |
||
79 | |||
80 | /** |
||
81 | * Permission to query balance through the ACCOUNTS endpoint. |
||
82 | * |
||
83 | * @const string |
||
84 | */ |
||
85 | const RETRIEVE_FINANCIAL_INFO = 'RETRIEVE_FINANCIAL_INFO'; |
||
86 | |||
87 | /** |
||
88 | * Permission for bank transfers or for Moip accounts through the TRANSFERS endpoint. |
||
89 | * |
||
90 | * @const string |
||
91 | */ |
||
92 | const TRANSFER_FUNDS = 'TRANSFER_FUNDS'; |
||
93 | |||
94 | /** |
||
95 | * Permission to create, change, and delete notification preferences through the PREFERENCES endpoint. |
||
96 | * |
||
97 | * @const string |
||
98 | */ |
||
99 | const DEFINE_PREFERENCES = 'DEFINE_PREFERENCES'; |
||
100 | |||
101 | /** |
||
102 | * List all scopes. |
||
103 | * |
||
104 | * @const array |
||
105 | */ |
||
106 | const SCOPE_ALL = [ |
||
107 | self::RECEIVE_FUNDS, |
||
108 | self::REFUND, |
||
109 | self::MANAGE_ACCOUNT_INFO, |
||
110 | self::RETRIEVE_FINANCIAL_INFO, |
||
111 | self::TRANSFER_FUNDS, |
||
112 | self::DEFINE_PREFERENCES, |
||
113 | ]; |
||
114 | |||
115 | /** |
||
116 | * Unique identifier of the application that will be carried out the request. |
||
117 | * |
||
118 | * @var string |
||
119 | */ |
||
120 | private $client_id; |
||
121 | |||
122 | /** |
||
123 | * Classic non-standard authentication and access token for integration with generic SDKs. |
||
124 | * |
||
125 | * @var string |
||
126 | */ |
||
127 | private $client_secret; |
||
128 | |||
129 | /** |
||
130 | * Client Redirect URI. |
||
131 | * |
||
132 | * @var string |
||
133 | */ |
||
134 | private $redirect_uri; |
||
135 | |||
136 | /** |
||
137 | * Endpoint. |
||
138 | * |
||
139 | * @var string |
||
140 | */ |
||
141 | private $endpoint; |
||
142 | |||
143 | /** |
||
144 | * Permissions that you want (Possible values depending on the feature.). |
||
145 | * |
||
146 | * @var array |
||
147 | */ |
||
148 | private $scope = []; |
||
149 | |||
150 | /** |
||
151 | * Validation code to retrieve the access token. |
||
152 | * |
||
153 | * @var string |
||
154 | */ |
||
155 | private $code; |
||
156 | |||
157 | /** |
||
158 | * Connect constructor. |
||
159 | * |
||
160 | * @param string $redirect_uri |
||
161 | * @param string $client_id |
||
162 | * @param array|bool $scope |
||
163 | * @param string $endpoint |
||
164 | */ |
||
165 | public function __construct($redirect_uri = '', $client_id = '', $scope = true, $endpoint = self::ENDPOINT_PRODUCTION) |
||
178 | |||
179 | /** |
||
180 | * Creates a new Request_Session with all the default values. |
||
181 | * A Session is created at construction. |
||
182 | * |
||
183 | * @param float $timeout How long should we wait for a response?(seconds with a millisecond precision, default: 30, example: 0.01). |
||
184 | * @param float $connect_timeout How long should we wait while trying to connect? (seconds with a millisecond precision, default: 10, example: 0.01) |
||
185 | * |
||
186 | * @return \Requests_Session |
||
187 | */ |
||
188 | public function createNewSession($timeout = 30.0, $connect_timeout = 30.0) |
||
205 | |||
206 | /** |
||
207 | * URI of oauth. |
||
208 | * |
||
209 | * @param $endpoint |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | public function getAuthUrl($endpoint = null) |
||
227 | |||
228 | /** |
||
229 | * With the permission granted, you will receive a code that will allow you to retrieve the authentication accessToken and process requests involving another user. |
||
230 | * |
||
231 | * @return mixed |
||
232 | */ |
||
233 | public function authorize() |
||
259 | |||
260 | /** |
||
261 | * @param bool $scope |
||
262 | * |
||
263 | * @return $this |
||
264 | */ |
||
265 | public function setScodeAll($scope) |
||
285 | |||
286 | /** |
||
287 | * Permission for creation and consultation of ORDERS, PAYMENTS, MULTI ORDERS, MULTI PAYMENTS, CUSTOMERS and consultation of LAUNCHES. |
||
288 | * |
||
289 | * @param bool $receive_funds |
||
290 | * |
||
291 | * @throws \Moip\Exceptions\InvalidArgumentException |
||
292 | * |
||
293 | * @return \Moip\Auth\Connect $this |
||
294 | */ |
||
295 | View Code Duplication | public function setReceiveFunds($receive_funds) |
|
307 | |||
308 | /** |
||
309 | * Permission to create and consult reimbursements ofORDERS, PAYMENTS. |
||
310 | * |
||
311 | * @param bool $refund |
||
312 | * |
||
313 | * @throws \Moip\Exceptions\InvalidArgumentException |
||
314 | * |
||
315 | * @return \Moip\Auth\Connect $this |
||
316 | */ |
||
317 | View Code Duplication | public function setRefund($refund) |
|
329 | |||
330 | /** |
||
331 | * Permission to consult ACCOUNTS registration information. |
||
332 | * |
||
333 | * @param bool $manage_account_info |
||
334 | * |
||
335 | * @throws \Moip\Exceptions\InvalidArgumentException |
||
336 | * |
||
337 | * @return \Moip\Auth\Connect $this |
||
338 | */ |
||
339 | View Code Duplication | public function setManageAccountInfo($manage_account_info) |
|
351 | |||
352 | /** |
||
353 | * Permission to query balance through the ACCOUNTS endpoint. |
||
354 | * |
||
355 | * @param bool $retrieve_financial_info |
||
356 | * |
||
357 | * @throws \Moip\Exceptions\InvalidArgumentException |
||
358 | * |
||
359 | * @return \Moip\Auth\Connect $this |
||
360 | */ |
||
361 | View Code Duplication | public function setRetrieveFinancialInfo($retrieve_financial_info) |
|
373 | |||
374 | /** |
||
375 | * Permission for bank transfers or for Moip accounts through the TRANSFERS endpoint. |
||
376 | * |
||
377 | * @param bool $transfer_funds |
||
378 | * |
||
379 | * @throws \Moip\Exceptions\InvalidArgumentException |
||
380 | * |
||
381 | * @return \Moip\Auth\Connect $this |
||
382 | */ |
||
383 | View Code Duplication | public function setTransferFunds($transfer_funds) |
|
395 | |||
396 | /** |
||
397 | * Permission to create, change, and delete notification preferences through the PREFERENCES endpoint. |
||
398 | * |
||
399 | * @param bool $define_preferences |
||
400 | * |
||
401 | * @throws \Moip\Exceptions\InvalidArgumentException |
||
402 | * |
||
403 | * @return $this |
||
404 | */ |
||
405 | View Code Duplication | public function setDefinePreferences($define_preferences) |
|
417 | |||
418 | /** |
||
419 | * Unique identifier of the application that will be carried out the request. |
||
420 | * |
||
421 | * @return mixed |
||
422 | */ |
||
423 | public function getClientId() |
||
427 | |||
428 | /** |
||
429 | * Unique identifier of the application that will be carried out the request. |
||
430 | * |
||
431 | * @param mixed $client_id |
||
432 | * |
||
433 | * @return \Moip\Auth\Connect |
||
434 | */ |
||
435 | public function setClientId($client_id) |
||
441 | |||
442 | /** |
||
443 | * Client Redirect URI. |
||
444 | * |
||
445 | * @return mixed |
||
446 | */ |
||
447 | public function getRedirectUri() |
||
451 | |||
452 | /** |
||
453 | * Client Redirect URI. |
||
454 | * |
||
455 | * @param mixed $redirect_uri |
||
456 | * |
||
457 | * @return \Moip\Auth\Connect |
||
458 | */ |
||
459 | public function setRedirectUri($redirect_uri) |
||
465 | |||
466 | /** |
||
467 | * Permissions that you want (Possible values depending on the feature.). |
||
468 | * |
||
469 | * @return mixed |
||
470 | */ |
||
471 | public function getScope() |
||
475 | |||
476 | /** |
||
477 | * Permissions that you want (Possible values depending on the feature.). |
||
478 | * |
||
479 | * @param array|string $scope |
||
480 | * |
||
481 | * @return \Moip\Auth\Connect |
||
482 | */ |
||
483 | public function setScope($scope) |
||
497 | |||
498 | /** |
||
499 | * @param string $endpoint |
||
500 | * |
||
501 | * @return \Moip\Auth\Connect |
||
502 | */ |
||
503 | public function setEndpoint(string $endpoint) |
||
513 | |||
514 | /** |
||
515 | * @param mixed $client_secret |
||
516 | * |
||
517 | * @return \Moip\Auth\Connect |
||
518 | */ |
||
519 | public function setClientSecret($client_secret) |
||
525 | |||
526 | /** |
||
527 | * @return mixed |
||
528 | */ |
||
529 | public function getClientSecret() |
||
533 | |||
534 | /** |
||
535 | * @param string $code |
||
536 | * |
||
537 | * @return \Moip\Auth\Connect |
||
538 | */ |
||
539 | public function setCode(string $code) |
||
545 | |||
546 | /** |
||
547 | * @return string |
||
548 | */ |
||
549 | public function getCode() |
||
553 | |||
554 | /** |
||
555 | * Register hooks as needed. |
||
556 | * |
||
557 | * This method is called in {@see Requests::request} when the user has set |
||
558 | * an instance as the 'auth' option. Use this callback to register all the |
||
559 | * hooks you'll need. |
||
560 | * |
||
561 | * @see Requests_Hooks::register |
||
562 | * |
||
563 | * @param Requests_Hooks $hooks Hook system |
||
564 | */ |
||
565 | public function register(Requests_Hooks &$hooks) |
||
569 | |||
570 | /** |
||
571 | * Specify data which should be serialized to JSON. |
||
572 | * |
||
573 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
574 | * |
||
575 | * @return mixed data which can be serialized by <b>json_encode</b>, |
||
576 | * which is a value of any type other than a resource. |
||
577 | * |
||
578 | * @since 5.4.0 |
||
579 | */ |
||
580 | public function jsonSerialize() |
||
584 | } |
||
585 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.