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 |
||
9 | class SubscriptionPaymentRequest extends EcommercePaymentRequest |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * Set amount in cents, eg EUR 12.34 is written as 1234 |
||
14 | * For subscriptions an amount of 0 can be selected, however this feature must first be enabled by ogone for your account |
||
15 | */ |
||
16 | 6 | View Code Duplication | public function setAmount($amount) |
31 | |||
32 | /** |
||
33 | * Unique identifier of the subscription. The subscription id must be assigned dynamically. |
||
34 | * @author René de Kat <[email protected]> |
||
35 | * |
||
36 | * @param string $subscriptionId (maxlength 50) |
||
37 | */ |
||
38 | 3 | View Code Duplication | public function setSubscriptionId($subscriptionId) |
39 | { |
||
40 | 3 | if (strlen($subscriptionId) > 50) { |
|
41 | 1 | throw new InvalidArgumentException("Subscription id cannot be longer than 50 characters"); |
|
42 | } |
||
43 | 2 | if (preg_match('/[^a-zA-Z0-9_-]/', $subscriptionId)) { |
|
44 | 1 | throw new InvalidArgumentException("Subscription id cannot contain special characters"); |
|
45 | } |
||
46 | 1 | $this->parameters['subscription_id'] = $subscriptionId; |
|
47 | 1 | } |
|
48 | |||
49 | /** |
||
50 | * Amount of the subscription (can be different from the amount of the original transaction) |
||
51 | * multiplied by 100, since the format of the amount must not contain any decimals or other separators. |
||
52 | * |
||
53 | * @author René de Kat <[email protected]> |
||
54 | * |
||
55 | * @param integer $amount |
||
56 | */ |
||
57 | 5 | View Code Duplication | public function setSubscriptionAmount($amount) |
70 | |||
71 | /** |
||
72 | * Order description |
||
73 | * @author René de Kat <[email protected]> |
||
74 | * |
||
75 | * @param string $description (maxlength 100) |
||
76 | */ |
||
77 | 3 | View Code Duplication | public function setSubscriptionDescription($description) |
78 | { |
||
79 | 3 | if (strlen($description) > 100) { |
|
80 | 1 | throw new InvalidArgumentException("Subscription description cannot be longer than 100 characters"); |
|
81 | } |
||
82 | 2 | if (preg_match('/[^a-zA-Z0-9_ -]/', $description)) { |
|
83 | 1 | throw new InvalidArgumentException("Subscription description cannot contain special characters"); |
|
84 | } |
||
85 | 1 | $this->parameters['sub_com'] = $description; |
|
86 | 1 | } |
|
87 | |||
88 | /** |
||
89 | * OrderID for subscription payments |
||
90 | * @author René de Kat <[email protected]> |
||
91 | * |
||
92 | * @param string $orderId (maxlength 40) |
||
93 | */ |
||
94 | 3 | View Code Duplication | public function setSubscriptionOrderId($orderId) |
95 | { |
||
96 | 3 | if (strlen($orderId) > 40) { |
|
97 | 1 | throw new InvalidArgumentException("Subscription order id cannot be longer than 40 characters"); |
|
98 | } |
||
99 | 2 | if (preg_match('/[^a-zA-Z0-9_-]/', $orderId)) { |
|
100 | 1 | throw new InvalidArgumentException("Subscription order id cannot contain special characters"); |
|
101 | } |
||
102 | 1 | $this->parameters['sub_orderid'] = $orderId; |
|
103 | 1 | } |
|
104 | |||
105 | /** |
||
106 | * Set subscription payment interval |
||
107 | * @author René de Kat <[email protected]> |
||
108 | */ |
||
109 | 1 | public function setSubscriptionPeriod(SubscriptionPeriod $period) |
|
115 | |||
116 | |||
117 | /** |
||
118 | * Subscription start date |
||
119 | * @author René de Kat <[email protected]> |
||
120 | * |
||
121 | * @param DateTime $date Startdate of the subscription. |
||
122 | */ |
||
123 | 1 | public function setSubscriptionStartdate(DateTime $date) |
|
127 | |||
128 | /** |
||
129 | * Subscription end date |
||
130 | * @author René de Kat <[email protected]> |
||
131 | * |
||
132 | * @param DateTime $date Enddate of the subscription. |
||
133 | */ |
||
134 | 1 | public function setSubscriptionEnddate(DateTime $date) |
|
138 | |||
139 | /** |
||
140 | * Set subscription status |
||
141 | * @author René de Kat <[email protected]> |
||
142 | * |
||
143 | * @param integer $status 0 = inactive, 1 = active |
||
144 | */ |
||
145 | 2 | public function setSubscriptionStatus($status) |
|
152 | |||
153 | /** |
||
154 | * Set comment for merchant |
||
155 | * @author René de Kat <[email protected]> |
||
156 | * |
||
157 | * @param string $comment |
||
158 | */ |
||
159 | 3 | View Code Duplication | public function setSubscriptionComment($comment) |
160 | { |
||
161 | 3 | if (strlen($comment) > 200) { |
|
162 | 1 | throw new InvalidArgumentException("Subscription comment cannot be longer than 200 characters"); |
|
163 | } |
||
164 | 2 | if (preg_match('/[^a-zA-Z0-9_ -]/', $comment)) { |
|
165 | 1 | throw new InvalidArgumentException("Subscription comment cannot contain special characters"); |
|
166 | } |
||
167 | 1 | $this->parameters['sub_comment'] = $comment; |
|
168 | 1 | } |
|
169 | |||
170 | 2 | public function getRequiredFields() |
|
178 | } |
||
179 |
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.