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 |
||
18 | class LinkShare extends AbstractNetwork implements NetworkInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var object |
||
22 | */ |
||
23 | private $_network = null; |
||
24 | private $_username = ''; |
||
25 | private $_password = ''; |
||
26 | private $_apiClient = null; |
||
27 | protected $_tracking_parameter = ''; |
||
28 | private $_idSite = ''; |
||
29 | |||
30 | /** |
||
31 | * @method __construct |
||
32 | */ |
||
33 | public function __construct(string $username, string $password, string $idSite = '') |
||
34 | { |
||
35 | $this->_network = new \Oara\Network\Publisher\LinkShare; |
||
36 | $this->_username = $username; |
||
37 | $this->_password = $password; |
||
38 | $this->_idSite = $idSite; |
||
39 | |||
40 | $this->login( $this->_username, $this->_password, $idSite ); |
||
41 | } |
||
42 | |||
43 | View Code Duplication | public function login(string $username, string $password,string $idSite=''): bool{ |
|
|
|||
44 | $this->_logged = false; |
||
45 | if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) { |
||
46 | |||
47 | return false; |
||
48 | } |
||
49 | $this->_username = $username; |
||
50 | $this->_password = $password; |
||
51 | $credentials = array(); |
||
52 | $credentials["user"] = $this->_username; |
||
53 | $credentials["password"] = $this->_password; |
||
54 | $credentials["idSite"] = $idSite; |
||
55 | $this->_network->login($credentials); |
||
56 | if ($this->_network->checkConnection()) { |
||
57 | $this->_logged = true; |
||
58 | } |
||
59 | |||
60 | return $this->_logged; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return bool |
||
65 | */ |
||
66 | public function checkLogin() : bool |
||
67 | { |
||
68 | return $this->_logged; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @return array of Merchants |
||
73 | */ |
||
74 | View Code Duplication | public function getMerchants() : array |
|
75 | { |
||
76 | $arrResult = array(); |
||
77 | $merchantList = $this->_network->getMerchantList(); |
||
78 | foreach($merchantList as $merchant) { |
||
79 | $Merchant = Merchant::createInstance(); |
||
80 | $Merchant->merchant_ID = $merchant['cid']; |
||
81 | $Merchant->name = $merchant['name']; |
||
82 | $Merchant->status = $merchant['status']; |
||
83 | $Merchant->termination_date = $merchant['termination_date']; |
||
84 | $Merchant->url = $merchant['url']; |
||
85 | $arrResult[] = $Merchant; |
||
86 | } |
||
87 | return $arrResult; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param int $merchantID |
||
92 | * @return array of Deal |
||
93 | */ |
||
94 | public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset |
||
127 | |||
128 | /** |
||
129 | * @param \DateTime $dateFrom |
||
130 | * @param \DateTime $dateTo |
||
131 | * @param int $merchantID |
||
132 | * @return array of Transaction |
||
133 | */ |
||
134 | public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array |
||
135 | { |
||
136 | |||
137 | $arrResult = array(); |
||
138 | $this->_network->_nid = $_ENV['LINKSHARE_NETWORK']; |
||
139 | $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo); |
||
140 | foreach($transactionList as $transaction) { |
||
141 | $Transaction = Transaction::createInstance(); |
||
142 | if (isset($transaction['currency']) && !empty($transaction['currency'])) { |
||
143 | $Transaction->currency = $transaction['currency']; |
||
144 | } else { |
||
145 | $Transaction->currency = "EUR"; |
||
146 | } |
||
147 | $Transaction->status = $transaction['status']; |
||
148 | $Transaction->amount = $transaction['amount']; |
||
149 | array_key_exists_safe( $transaction,'custom_id' ) ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = ''; |
||
150 | $Transaction->title = ''; |
||
151 | $Transaction->unique_ID = $transaction['unique_id']; |
||
152 | $Transaction->commission = $transaction['commission']; |
||
153 | $date = new \DateTime($transaction['date']); |
||
154 | $Transaction->date = $date; // $date->format('Y-m-d H:i:s'); |
||
155 | // Future use - Only few providers returns these dates values - <PN> - 2017-06-29 |
||
156 | if (isset($transaction['click_date']) && !empty($transaction['click_date'])) { |
||
157 | $Transaction->click_date = new \DateTime($transaction['click_date']); |
||
158 | } |
||
159 | if (isset($transaction['update_date']) && !empty($transaction['update_date'])) { |
||
160 | $Transaction->update_date = new \DateTime($transaction['update_date']); |
||
161 | } |
||
162 | $Transaction->merchant_ID = $transaction['merchantId']; |
||
163 | $Transaction->campaign_name = $transaction['merchantName']; |
||
164 | $Transaction->IP = $transaction['IP']; |
||
165 | $Transaction->approved = false; |
||
166 | if ($Transaction->status==\Oara\Utilities::STATUS_CONFIRMED){ |
||
167 | $Transaction->approved = true; |
||
168 | } |
||
169 | $arrResult[] = $Transaction; |
||
170 | } |
||
171 | |||
172 | return $arrResult; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param \DateTime $dateFrom |
||
177 | * @param \DateTime $dateTo |
||
178 | * @param int $merchantID |
||
179 | * @return array of Stat |
||
180 | */ |
||
181 | public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array |
||
199 | |||
200 | |||
201 | /** |
||
202 | * @param array $params |
||
203 | * |
||
204 | * @return ProductsResultset |
||
205 | */ |
||
206 | public function getProducts(array $params = []): ProductsResultset |
||
211 | |||
212 | public function getTrackingParameter(){ |
||
215 | } |
||
216 |
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.