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 Partnerize extends AbstractNetwork implements NetworkInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var object |
||
23 | */ |
||
24 | private $_network = null; |
||
25 | private $_apiClient = null; |
||
26 | private $_username = ''; |
||
27 | private $_password = ''; |
||
28 | private $_logged = false; |
||
29 | protected $_tracking_parameter = 'pubref'; |
||
30 | |||
31 | /** |
||
32 | * @method __construct |
||
33 | */ |
||
34 | public function __construct(string $username, string $password) |
||
35 | { |
||
36 | $this->_network = new \Oara\Network\Publisher\PerformanceHorizon(); |
||
37 | $this->_username = $username; |
||
38 | $this->_password = $password; |
||
39 | $this->_apiClient = null; |
||
40 | $this->login( $this->_username, $this->_password ); |
||
41 | } |
||
42 | |||
43 | public function login(string $username, string $password): bool |
||
44 | { |
||
45 | $this->_logged = false; |
||
46 | if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) { |
||
47 | return false; |
||
48 | } |
||
49 | $this->_username = $username; |
||
50 | $this->_password = $password; |
||
51 | $credentials = array(); |
||
52 | $credentials["accountid"] = $this->_username; |
||
53 | $credentials["apipassword"] = $this->_password; |
||
54 | $this->_network->login( $credentials ); |
||
55 | if ($this->_network->checkConnection()) { |
||
56 | $this->_logged = true; |
||
57 | |||
58 | } |
||
59 | |||
60 | return $this->_logged; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return bool |
||
65 | */ |
||
66 | public function checkLogin() : bool |
||
70 | |||
71 | /** |
||
72 | * @return array of Merchants |
||
73 | */ |
||
74 | View Code Duplication | public function getMerchants() : array |
|
90 | |||
91 | /** |
||
92 | * @param int|null $merchantID |
||
93 | * @param int $page |
||
94 | * @param int $items_per_page |
||
95 | * |
||
96 | * @return DealsResultset |
||
97 | */ |
||
98 | public function getDeals($merchantID,int $page=0,int $items_per_page=10) : DealsResultset |
||
103 | |||
104 | /** |
||
105 | * @param \DateTime $dateFrom |
||
106 | * @param \DateTime $dateTo |
||
107 | * @param int $merchantID |
||
108 | * @return array of Transaction |
||
109 | */ |
||
110 | View Code Duplication | public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array |
|
111 | { |
||
112 | $arrResult = array(); |
||
113 | |||
114 | try { |
||
115 | // Added timezone parameter |
||
116 | $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo, 'UTC'); |
||
117 | |||
118 | if (is_array($transactionList)) { |
||
119 | foreach ($transactionList as $transaction) { |
||
120 | try { |
||
121 | |||
122 | $myTransaction = Transaction::createInstance(); |
||
123 | |||
124 | $myTransaction->merchant_ID = $transaction['merchantId']; |
||
125 | $myTransaction->date = $transaction['date']; |
||
126 | if (!empty($transaction['date'])) { |
||
127 | $date = new \DateTime($transaction['date'], new \DateTimeZone('UTC')); |
||
128 | $myTransaction->date = $date; |
||
129 | } |
||
130 | $myTransaction->unique_ID = $transaction['unique_id']; |
||
131 | if (isset($transaction['custom_id'])) { |
||
132 | $myTransaction->custom_ID = $transaction['custom_id']; |
||
133 | } |
||
134 | $myTransaction->status = $transaction['status']; |
||
135 | $myTransaction->amount = \Oara\Utilities::parseDouble($transaction['amount']); |
||
136 | $myTransaction->commission = \Oara\Utilities::parseDouble($transaction['commission']); |
||
137 | $myTransaction->currency = $transaction['currency']; |
||
138 | $arrResult[] = $myTransaction; |
||
139 | } catch (\Exception $e) { |
||
140 | echo "<br><br>Transaction Error Partnerize, id: " . $myTransaction['unique_id'] . " msg: ".$e->getMessage()."<br><br>"; |
||
141 | var_dump($e->getTraceAsString()); |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | } catch (\Exception $e) { |
||
146 | echo "<br><br>Generic Error Partnerize: ".$e->getMessage()."<br><br>"; |
||
147 | var_dump($e->getTraceAsString()); |
||
148 | throw new \Exception($e); |
||
149 | } |
||
150 | return $arrResult; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param \DateTime $dateFrom |
||
155 | * @param \DateTime $dateTo |
||
156 | * @param int $merchantID |
||
157 | * @return array of Stat |
||
158 | */ |
||
159 | public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array |
||
163 | |||
164 | |||
165 | /** |
||
166 | * @param array $params |
||
167 | * |
||
168 | * @return ProductsResultset |
||
169 | */ |
||
170 | public function getProducts(array $params = []): ProductsResultset |
||
175 | |||
176 | public function getTrackingParameter(){ |
||
179 | } |
||
180 |
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.