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 Account 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 Account, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Account extends MoipResource |
||
12 | { |
||
13 | /** |
||
14 | * Path accounts API. |
||
15 | * |
||
16 | * @const string |
||
17 | */ |
||
18 | const PATH = 'accounts'; |
||
19 | |||
20 | /** |
||
21 | * Standard country . |
||
22 | * |
||
23 | * @const string |
||
24 | */ |
||
25 | const ADDRESS_COUNTRY = 'BRA'; |
||
26 | |||
27 | /** |
||
28 | * Standard document type. |
||
29 | * |
||
30 | * @const string |
||
31 | */ |
||
32 | const TAX_DOCUMENT = 'CPF'; |
||
33 | |||
34 | /** |
||
35 | * Standard company document type. |
||
36 | * |
||
37 | * @const string |
||
38 | */ |
||
39 | const COMPANY_TAX_DOCUMENT = 'CNPJ'; |
||
40 | |||
41 | /** |
||
42 | * Default Account Type. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | const ACCOUNT_TYPE = 'MERCHANT'; |
||
47 | |||
48 | /** |
||
49 | * Initialize a new instance. |
||
50 | */ |
||
51 | public function initialize() |
||
60 | |||
61 | /** |
||
62 | * Add a new address to the account. |
||
63 | * |
||
64 | * @param string $street Street address. |
||
65 | * @param string $number Number address. |
||
66 | * @param string $district Neighborhood address. |
||
67 | * @param string $city City address. |
||
68 | * @param string $state State address. |
||
69 | * @param string $zip The zip code billing address. |
||
70 | * @param string $complement Address complement. |
||
71 | * @param string $country Country ISO-alpha3 format, BRA example. |
||
72 | * |
||
73 | * @return $this |
||
74 | */ |
||
75 | View Code Duplication | public function addAddress($street, $number, $district, $city, $state, $zip, $complement = null, $country = self::ADDRESS_COUNTRY) |
|
91 | |||
92 | /** |
||
93 | * Add alternative phone to an account. |
||
94 | * |
||
95 | * @param int $areaCode DDD telephone. |
||
96 | * @param int $number Telephone number. |
||
97 | * @param int $countryCode Country code. |
||
98 | * |
||
99 | * @return \Moip\Resource\Account |
||
100 | */ |
||
101 | public function addAlternativePhone($areaCode, $number, $countryCode = 55) |
||
112 | |||
113 | /** |
||
114 | * Create a new account. |
||
115 | * |
||
116 | * @return \stdClass |
||
117 | */ |
||
118 | public function create() |
||
122 | |||
123 | /** |
||
124 | * Find a account. |
||
125 | * |
||
126 | * @param string $moip_id |
||
127 | * |
||
128 | * @return stdClass |
||
129 | */ |
||
130 | public function get($moip_id) |
||
134 | |||
135 | /** |
||
136 | * Check if an account exists. |
||
137 | * |
||
138 | * @param string $tax_document |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | public function checkExistence($tax_document) |
||
156 | |||
157 | /** |
||
158 | * Get account id. |
||
159 | * |
||
160 | * @return string The buyer id. |
||
161 | */ |
||
162 | public function getId() |
||
166 | |||
167 | /** |
||
168 | * Get account access token. |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | public function getAccessToken() |
||
176 | |||
177 | /** |
||
178 | * Get account channel ID. |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | public function getChannelId() |
||
186 | |||
187 | /** |
||
188 | * Get account login. |
||
189 | * |
||
190 | * @return string The buyer login. |
||
191 | */ |
||
192 | public function getLogin() |
||
196 | |||
197 | /** |
||
198 | * Get account address. |
||
199 | * |
||
200 | * @return \stdClass Account's address. |
||
201 | */ |
||
202 | public function getAddress() |
||
206 | |||
207 | /** |
||
208 | * Get account fullname. |
||
209 | * |
||
210 | * @return string Account's full name. |
||
211 | */ |
||
212 | public function getFullname() |
||
216 | |||
217 | /** |
||
218 | * Get birth date from account. |
||
219 | * |
||
220 | * @return \DateTime|null Date of birth of the credit card holder. |
||
221 | */ |
||
222 | public function getBirthDate() |
||
226 | |||
227 | /** |
||
228 | * Get phone area code from account. |
||
229 | * |
||
230 | * @return int DDD telephone. |
||
231 | */ |
||
232 | public function getPhoneAreaCode() |
||
236 | |||
237 | /** |
||
238 | * Get phone country code from account. |
||
239 | * |
||
240 | * @return int Country code. |
||
241 | */ |
||
242 | public function getPhoneCountryCode() |
||
246 | |||
247 | /** |
||
248 | * Get phone number from account. |
||
249 | * |
||
250 | * @return int Telephone number. |
||
251 | */ |
||
252 | public function getPhoneNumber() |
||
256 | |||
257 | /** |
||
258 | * Get tax document type from account. |
||
259 | * |
||
260 | * @return string Type of value: CPF and CNPJ |
||
261 | */ |
||
262 | public function getTaxDocumentType() |
||
266 | |||
267 | /** |
||
268 | * Get tax document number from account. |
||
269 | * |
||
270 | * @return string Document Number. |
||
271 | */ |
||
272 | public function getTaxDocumentNumber() |
||
276 | |||
277 | /** |
||
278 | * Get identity document number from account. |
||
279 | * |
||
280 | * @return string |
||
281 | */ |
||
282 | public function getIdentityDocumentNumber() |
||
286 | |||
287 | /** |
||
288 | * Get identity document issuer from account. |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | public function getIdentityDocumentIssuer() |
||
296 | |||
297 | /** |
||
298 | * Get identity document issue date from account. |
||
299 | * |
||
300 | * @return \DateTime |
||
301 | */ |
||
302 | public function getIdentityDocumentIssueDate() |
||
306 | |||
307 | /** |
||
308 | * Get identity document type from account. |
||
309 | * |
||
310 | * @return string Type of value: RG |
||
311 | */ |
||
312 | public function getIdentityDocumentType() |
||
316 | |||
317 | /** |
||
318 | * Get alternative phones. |
||
319 | * |
||
320 | * @return array |
||
321 | */ |
||
322 | public function getAlternativePhones() |
||
326 | |||
327 | /** |
||
328 | * Get company data. |
||
329 | * |
||
330 | * @return array |
||
331 | */ |
||
332 | public function getCompany() |
||
336 | |||
337 | /** |
||
338 | * Get email address. |
||
339 | * |
||
340 | * @return string |
||
341 | */ |
||
342 | public function getEmailAddress() |
||
346 | |||
347 | /** |
||
348 | * Get email confirmed. |
||
349 | * |
||
350 | * @return bool |
||
351 | */ |
||
352 | public function getEmailConfirmed() |
||
356 | |||
357 | /** |
||
358 | * Get account type. |
||
359 | * |
||
360 | * @return string Document Number. |
||
361 | */ |
||
362 | public function getType() |
||
366 | |||
367 | /** |
||
368 | * Get business segment id. |
||
369 | * |
||
370 | * @return int id. |
||
371 | */ |
||
372 | public function getBusinessSegmentId() |
||
376 | |||
377 | /** |
||
378 | * Get business segment name. |
||
379 | * |
||
380 | * @return string name. |
||
381 | */ |
||
382 | public function getBusinessSegmentName() |
||
386 | |||
387 | /** |
||
388 | * Get business segment mcc. |
||
389 | * |
||
390 | * @return int mcc. |
||
391 | */ |
||
392 | public function getBusinessSegmentMcc() |
||
396 | |||
397 | /** |
||
398 | * Get transparent account (true/false). |
||
399 | * |
||
400 | * @return bool |
||
401 | */ |
||
402 | public function getTransparentAccount() |
||
406 | |||
407 | /** |
||
408 | * Get account created at. |
||
409 | * |
||
410 | * @return string |
||
411 | */ |
||
412 | public function getCreatedAt() |
||
416 | |||
417 | /** |
||
418 | * Get link to set the password of created account. |
||
419 | * |
||
420 | * @return string |
||
421 | */ |
||
422 | public function getPasswordLink() |
||
426 | |||
427 | /** |
||
428 | * Mount the seller structure from account. |
||
429 | * |
||
430 | * @param \stdClass $response |
||
431 | * |
||
432 | * @return \Moip\Resource\Account Account data |
||
433 | */ |
||
434 | protected function populate(stdClass $response) |
||
502 | |||
503 | /** |
||
504 | * Set e-mail from account. |
||
505 | * |
||
506 | * @param string $email Email account. |
||
507 | * |
||
508 | * @return \Moip\Resource\Account |
||
509 | */ |
||
510 | public function setEmail($email) |
||
516 | |||
517 | /** |
||
518 | * Set name from account. |
||
519 | * |
||
520 | * @param string $name Account's person name. |
||
521 | * |
||
522 | * @return \Moip\Resource\Account |
||
523 | */ |
||
524 | public function setName($name) |
||
530 | |||
531 | /** |
||
532 | * Set name from account. |
||
533 | * |
||
534 | * @param string $lastname Account's person name. |
||
535 | * |
||
536 | * @return \Moip\Resource\Account |
||
537 | */ |
||
538 | public function setLastName($lastname) |
||
544 | |||
545 | /** |
||
546 | * Set birth date from account. |
||
547 | * |
||
548 | * @param \DateTime|string $birthDate Date of birth of the credit card holder. |
||
549 | * |
||
550 | * @return \Moip\Resource\Account |
||
551 | */ |
||
552 | View Code Duplication | public function setBirthDate($birthDate) |
|
562 | |||
563 | /** |
||
564 | * Set tax document from account. |
||
565 | * |
||
566 | * @param string $number Document number. |
||
567 | * @param string $type Document type. |
||
568 | * |
||
569 | * @return \Moip\Resource\Account |
||
570 | */ |
||
571 | public function setTaxDocument($number, $type = self::TAX_DOCUMENT) |
||
572 | { |
||
573 | $this->data->person->taxDocument = new stdClass(); |
||
574 | $this->data->person->taxDocument->type = $type; |
||
575 | $this->data->person->taxDocument->number = $number; |
||
576 | |||
577 | return $this; |
||
578 | } |
||
579 | |||
580 | /** |
||
581 | * Set phone from account. |
||
582 | * |
||
583 | * @param int $areaCode DDD telephone. |
||
584 | * @param int $number Telephone number. |
||
585 | * @param int $countryCode Country code. |
||
586 | * |
||
587 | * @return \Moip\Resource\Account |
||
588 | */ |
||
589 | View Code Duplication | public function setPhone($areaCode, $number, $countryCode = 55) |
|
598 | |||
599 | /** |
||
600 | * Set identity document from account. |
||
601 | * |
||
602 | * @param string $number Número do documento. |
||
603 | * @param string $issuer Emissor do documento. |
||
604 | * @param $issueDate |
||
605 | * @param string $type Tipo do documento. Valores possíveis: RG. |
||
606 | * |
||
607 | * @return Account |
||
608 | */ |
||
609 | public function setIdentityDocument($number, $issuer, $issueDate, $type = 'RG') |
||
619 | |||
620 | /** |
||
621 | * Set person nationality. |
||
622 | * |
||
623 | * @param string $nationality Abbreviation for nationality (3 max length). |
||
624 | * |
||
625 | * @return $this |
||
626 | */ |
||
627 | public function setNationality($nationality = self::ADDRESS_COUNTRY) |
||
633 | |||
634 | /** |
||
635 | * Set person birth place. |
||
636 | * |
||
637 | * @param string $birthPlace Birth place (city). |
||
638 | * |
||
639 | * @return $this |
||
640 | */ |
||
641 | public function setBirthPlace($birthPlace) |
||
647 | |||
648 | /** |
||
649 | * Set parents name. |
||
650 | * |
||
651 | * @param string $motherName Mother name. |
||
652 | * @param string $fatherName Father name. |
||
653 | * |
||
654 | * @return $this |
||
655 | */ |
||
656 | public function setParentsName($motherName, $fatherName) |
||
664 | |||
665 | /** |
||
666 | * Set site. |
||
667 | * |
||
668 | * @param string $site URL from site. |
||
669 | * |
||
670 | * @return $this |
||
671 | */ |
||
672 | public function setSite($site) |
||
678 | |||
679 | /** |
||
680 | * Set transparent account. |
||
681 | * |
||
682 | * @param bool $transparentAccount Set true if you want create a transparent account. |
||
683 | * |
||
684 | * @return $this |
||
685 | */ |
||
686 | public function setTransparentAccount($transparentAccount) |
||
692 | |||
693 | /** |
||
694 | * Set business segment. |
||
695 | * |
||
696 | * @param int $segmentId business segment id. Possible values available at: https://documentao-moip.readme.io/v2.0/reference#tabela-de-categorias-de-estabelecimento . |
||
697 | * |
||
698 | * @return $this |
||
699 | */ |
||
700 | public function setBusinessSegment($segmentId) |
||
706 | |||
707 | /** |
||
708 | * Set company name. |
||
709 | * |
||
710 | * @param string $name Trading Name. |
||
711 | * @param string $businessName Company Name. |
||
712 | * |
||
713 | * @return $this |
||
714 | */ |
||
715 | public function setCompanyName($name, $businessName) |
||
723 | |||
724 | /** |
||
725 | * Initialize company node. |
||
726 | */ |
||
727 | private function initializeCompany() |
||
733 | |||
734 | /** |
||
735 | * Set company opening date. |
||
736 | * |
||
737 | * @param \DateTime|string $openingDate . |
||
738 | * |
||
739 | * @return $this |
||
740 | */ |
||
741 | public function setCompanyOpeningDate($openingDate) |
||
751 | |||
752 | /** |
||
753 | * Set company tax document. |
||
754 | * |
||
755 | * @param string $documentNumber . |
||
756 | * |
||
757 | * @return $this |
||
758 | */ |
||
759 | View Code Duplication | public function setCompanyTaxDocument($documentNumber) |
|
768 | |||
769 | /** |
||
770 | * Set company tax document. |
||
771 | * |
||
772 | * @param string $documentNumber . |
||
773 | * |
||
774 | * @return $this |
||
775 | */ |
||
776 | View Code Duplication | public function setCompanyMainActivity($cnae, $description) |
|
777 | { |
||
778 | $this->initializeCompany(); |
||
779 | $this->data->company->mainActivity = new stdClass(); |
||
780 | $this->data->company->mainActivity->cnae = $cnae; |
||
781 | $this->data->company->mainActivity->description = $description; |
||
782 | |||
783 | return $this; |
||
784 | } |
||
785 | |||
786 | /** |
||
787 | * Set address to company. |
||
788 | * |
||
789 | * @param string $street Street address. |
||
790 | * @param string $number Number address. |
||
791 | * @param string $district Neighborhood address. |
||
792 | * @param string $city City address. |
||
793 | * @param string $state State address. |
||
794 | * @param string $zip The zip code billing address. |
||
795 | * @param string $complement Address complement. |
||
796 | * @param string $country Country ISO-alpha3 format, BRA example. |
||
797 | * |
||
798 | * @return $this |
||
799 | */ |
||
800 | View Code Duplication | public function setCompanyAddress($street, $number, $district, $city, $state, $zip, $complement = null, $country = self::ADDRESS_COUNTRY) |
|
817 | |||
818 | /** |
||
819 | * Set company phone. |
||
820 | * |
||
821 | * @param int $areaCode DDD telephone. |
||
822 | * @param int $number Telephone number. |
||
823 | * @param int $countryCode Country code. |
||
824 | * |
||
825 | * @return \Moip\Resource\Account |
||
826 | */ |
||
827 | View Code Duplication | public function setCompanyPhone($areaCode, $number, $countryCode = 55) |
|
828 | { |
||
829 | $this->initializeCompany(); |
||
830 | $this->data->company->phone = new stdClass(); |
||
831 | $this->data->company->phone->countryCode = $countryCode; |
||
832 | $this->data->company->phone->areaCode = $areaCode; |
||
833 | $this->data->company->phone->number = $number; |
||
834 | |||
835 | return $this; |
||
836 | } |
||
837 | |||
838 | /** |
||
839 | * Set account type. Possible values: CONSUMER, MERCHANT. |
||
840 | * |
||
841 | * @param string $type |
||
842 | * |
||
843 | * @return \Moip\Resource\Account |
||
844 | */ |
||
845 | public function setType($type) |
||
851 | } |
||
852 |
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.