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:
1 | <?php |
||
12 | View Code Duplication | class PromotedAccount extends Resource |
|
|
|||
13 | { |
||
14 | const RESOURCE_COLLECTION = 'accounts/{account_id}/promoted_accounts'; |
||
15 | const RESOURCE = 'accounts/{account_id}/promoted_accounts/{id}'; |
||
16 | const RESOURCE_STATS = 'stats/accounts/{account_id}/promoted_accounts/{id}'; |
||
17 | |||
18 | /** Read Only */ |
||
19 | protected $id; |
||
20 | protected $approval_status; |
||
21 | protected $created_at; |
||
22 | protected $updated_at; |
||
23 | protected $deleted; |
||
24 | |||
25 | protected $properties = [ |
||
26 | PromotedAccountFields::LINE_ITEM_ID, |
||
27 | PromotedAccountFields::USER_ID, |
||
28 | ]; |
||
29 | |||
30 | /** Writable */ |
||
31 | protected $line_item_id; |
||
32 | protected $user_id; |
||
33 | |||
34 | /** |
||
35 | * @return mixed |
||
36 | */ |
||
37 | public function getId() |
||
41 | |||
42 | /** |
||
43 | * @return mixed |
||
44 | */ |
||
45 | public function getApprovalStatus() |
||
49 | |||
50 | /** |
||
51 | * @return mixed |
||
52 | */ |
||
53 | public function getCreatedAt() |
||
57 | |||
58 | /** |
||
59 | * @return mixed |
||
60 | */ |
||
61 | public function getUpdatedAt() |
||
65 | |||
66 | /** |
||
67 | * @return mixed |
||
68 | */ |
||
69 | public function getDeleted() |
||
73 | |||
74 | /** |
||
75 | * @return mixed |
||
76 | */ |
||
77 | public function getLineItemId() |
||
81 | |||
82 | /** |
||
83 | * @param mixed $line_item_id |
||
84 | */ |
||
85 | public function setLineItemId($line_item_id) |
||
89 | |||
90 | /** |
||
91 | * @return mixed |
||
92 | */ |
||
93 | public function getUserId() |
||
97 | |||
98 | /** |
||
99 | * @param mixed $user_id |
||
100 | */ |
||
101 | public function setUserId($user_id) |
||
105 | |||
106 | /** |
||
107 | * @param array $properties |
||
108 | */ |
||
109 | public function setProperties($properties) |
||
113 | } |
||
114 |
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.