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 |
||
19 | class LiteSpeed { |
||
20 | |||
21 | public $login = ''; |
||
22 | public $password = ''; |
||
23 | public $usePost = TRUE; |
||
24 | public $url = 'https://store.litespeedtech.com/reseller/LiteSpeed_eService.php'; |
||
25 | public $version = '1.1'; |
||
26 | public $params = []; |
||
27 | public $response = []; |
||
28 | public $validProducts = ['LSWS', 'LSLB']; |
||
29 | public $validCpu = ['1', '2', '4', '8', 'V', 'U']; |
||
30 | public $validPeriod = ['monthly', 'yearly', 'owned']; |
||
31 | public $validPayment = ['credit', 'creditcard']; |
||
32 | public $rawResponse; |
||
33 | |||
34 | /** |
||
35 | * @param $login |
||
36 | * @param $password |
||
37 | */ |
||
38 | public function __construct($login, $password) { |
||
44 | |||
45 | public function resetParams() { |
||
51 | |||
52 | /** |
||
53 | * @return mixed |
||
54 | */ |
||
55 | public function ping() { |
||
58 | |||
59 | /** |
||
60 | * LiteSpeed Order License Return |
||
61 | * * If any errors occur during this process, <result> will contain “error” and <message> |
||
62 | * will contain a detailed description: |
||
63 | * <LiteSpeed_eService> |
||
64 | * <action>Order</action> |
||
65 | * <result>error</result> |
||
66 | * <message>Invalid cpu!</message> |
||
67 | * </LiteSpeed_eService> |
||
68 | * If the transaction cannot be completed, <result> will be “incomplete”. For example, if |
||
69 | * payment method is “credit”, but there is not enough credit in your account, or if payment method |
||
70 | * is “creditcard”, but the charge cannot go through, then the transaction will not be completed |
||
71 | * and <result> will display “incomplete”. <license_id> and <invoice> will be provided. You will |
||
72 | * need to login to LiteSpeed’s online store and pay the invoice to finish the order. |
||
73 | * If payment method is “credit”, but not enough credit is available to your account: |
||
74 | * <LiteSpeed_eService> |
||
75 | * <action>Order</action> |
||
76 | * <license_id>6066</license_id> |
||
77 | * <license_type>WS_L_V</license_type> |
||
78 | * <invoice_id>12466</invoice_id> |
||
79 | * <result>incomplete</result> |
||
80 | * <message>need to pay invoice first</message> |
||
81 | * </LiteSpeed_eService> |
||
82 | * If payment method is “creditcard”, but the attempted credit card payment failed: |
||
83 | * <LiteSpeed_eService> |
||
84 | * <action>Order</action> |
||
85 | * <license_id>9329</license_id> |
||
86 | * <license_type>WS_L_V</license_type> |
||
87 | * <invoice_id>20568</invoice_id> |
||
88 | * <result>incomplete</result> |
||
89 | * <message>need to pay invoice first, credit card payment failed</message> |
||
90 | * </LiteSpeed_eService> |
||
91 | * If the transaction is successful, which should happen for the majority of cases, you will get a |
||
92 | * serial number back. You can parse the message to get the serial number and create your own script |
||
93 | * for installation. You will still receive the same confirmation email and serial number emails as |
||
94 | * if you ordered online. There will be no <invoice_id> if the charge was paid with credit. |
||
95 | * <LiteSpeed_eService> |
||
96 | * <action>Order</action> |
||
97 | * < license_id>6067</ license_id> |
||
98 | * <license_type>WS_L_V</license_type> |
||
99 | * <invoice_id>12466</invoice_id> |
||
100 | * < license_serial>gv06-kXsU-SHBr-pL4N</license_serial> |
||
101 | * <result>success</result> |
||
102 | * <message>new order automatically accepted</message> |
||
103 | * </LiteSpeed_eService> |
||
104 | * |
||
105 | */ |
||
106 | |||
107 | /** |
||
108 | * Order a LiteSpeed License |
||
109 | * |
||
110 | * @param mixed $product Product type. Available values: “LSWS” or “LSLB”. |
||
111 | * @param mixed $cpu What kind of license. Available values: “1”: 1-CPU license, “2”: 2-CPU license, “4”: 4-CPU license, “8”: 8-CPU license, “V”: VPS license, “U”: Ultra-VPS license (Available LSWS 4.2.2 and above.), If <order_product> is “LSLB”, <order_cpu> is not required. |
||
112 | * @param mixed $period Renewal period. Available values: “monthly”, “yearly”, “owned”. |
||
113 | * @param mixed $payment Payment method. Available values: “credit”: Use account credit. User can utilize “Add funds” function to pre-deposit money, which will show up as account credit. “creditcard”: Use credit card to pay. The credit card is pre-defined in the account. If there is available credit in the account, credit will be applied first, even when the payment method is set to “creditcard”. |
||
114 | * @param mixed $cvv (optional) Credit card security code. Try not to set this field. Only if your bank requires this (meaning that the transaction will fail without it) should you then supply this field. CVV code is not stored in the system, so if you need to set it, you have to set this field every time. Other information from your credit card will be taken from your user account. |
||
115 | * @param mixed $promocode (optional) Promotional code. If you have a pre-assigned promotional code registered to your account, then you can set it here. Promotional codes are exclusive to each client. If your account is entitled to discounts at the invoice level, you do not need a promotional code. |
||
116 | * @return array array with the output result. see above for description of output. |
||
117 | * array ( |
||
118 | * 'LiteSpeed_eService' => array ( |
||
119 | * 'action' => 'Order', |
||
120 | * 'license_id' => '36514', |
||
121 | * 'license_type' => 'WS_L_1', |
||
122 | * 'invoice_id' => '86300', |
||
123 | * 'result' => 'incomplete', |
||
124 | * 'message' => 'Invoice 86300 not paid. ', |
||
125 | * ), |
||
126 | * ) |
||
127 | */ |
||
128 | public function order($product, $cpu = FALSE, $period = 'monthly', $payment = 'credit', $cvv = FALSE, $promocode = FALSE) { |
||
155 | |||
156 | /** |
||
157 | * @param bool $serial |
||
158 | * @param bool $ipAddress |
||
159 | * @param string $now |
||
160 | * @param bool $reason |
||
161 | * @return mixed |
||
162 | */ |
||
163 | public function cancel($serial = FALSE, $ipAddress = FALSE, $now = 'Y', $reason = FALSE) { |
||
170 | |||
171 | /** |
||
172 | * @param $serial |
||
173 | * @param $ipAddress |
||
174 | * @return mixed |
||
175 | */ |
||
176 | public function release($serial, $ipAddress) { |
||
181 | |||
182 | /** |
||
183 | * Suspend a license. This is a tool to temporarily suspend a particular user's license in special cases, |
||
184 | * like nonpayment or policy violation. The web server checks in with the license server at least once |
||
185 | * every 24 hours. It will shut down when it sees the license has been suspended. As a consequence, your |
||
186 | * client's web site will go down. Please note, though, that this license will continue to appear on |
||
187 | * your invoices. Once the issue is resolved, you can use an “unsuspend” action to reactivate the license; |
||
188 | * or you can request cancellation to permanently cancel it. Only requesting cancellation will take the |
||
189 | * license off your future invoices. |
||
190 | * |
||
191 | * @param mixed $serial optional (if you specify IP , but this is preferred) serial of the license |
||
192 | * @param mixed $ipAddress optional (if you specify serial) ip of the license, specifying bothserial and ip gives extra validation |
||
193 | * @param mixed $reason optional reason for suspend/unsuspend |
||
194 | * @return mixed |
||
195 | */ |
||
196 | View Code Duplication | public function suspend($serial = FALSE, $ipAddress = FALSE, $reason = FALSE) { |
|
208 | |||
209 | /** |
||
210 | * Unsuspend a license. |
||
211 | * |
||
212 | * @param mixed $serial optional (if you specify IP , but this is preferred) serial of the license |
||
213 | * @param mixed $ipAddress optional (if you specify serial) ip of the license, specifying bothserial and ip gives extra validation |
||
214 | * @param mixed $reason optional reason for suspend/unsuspend |
||
215 | * @return mixed |
||
216 | */ |
||
217 | View Code Duplication | public function unsuspend($serial = FALSE, $ipAddress = FALSE, $reason = FALSE) { |
|
229 | |||
230 | /** |
||
231 | * @param bool $serial |
||
232 | * @param bool $ipAddress |
||
233 | * @param $cpu |
||
234 | * @param string $payment |
||
235 | * @param bool $cvv |
||
236 | * @return array|mixed |
||
237 | */ |
||
238 | public function upgrade($serial = FALSE, $ipAddress = FALSE, $cpu, $payment = 'credit', $cvv = FALSE) { |
||
258 | |||
259 | /** |
||
260 | * @param $field |
||
261 | * @return mixed |
||
262 | */ |
||
263 | public function query($field) { |
||
284 | |||
285 | /** |
||
286 | * sets whether or not to use POST for the request or GET (false) |
||
287 | * |
||
288 | * @param mixed $post TRUE for POST , FALSE for GET requests * |
||
289 | */ |
||
290 | public function usePost($post = TRUE) { |
||
293 | |||
294 | /** |
||
295 | * performs a request to LiteSpeed |
||
296 | * |
||
297 | * @param string $action Can be one of Ping, Order, Cancel, ReleaseLicense, Suspend, Unsuspend, Upgrade, or Query |
||
298 | * @return mixed |
||
299 | */ |
||
300 | public function req($action) { |
||
344 | |||
345 | /** |
||
346 | * displays the response |
||
347 | * |
||
348 | * @param mixed $response the response from an a function/api command |
||
349 | * @return void |
||
350 | */ |
||
351 | public function displayResponse($response) { |
||
356 | |||
357 | } |
||
358 |
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.