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 ShareASale 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 = 'afftrack'; |
||
| 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\ShareASale; |
||
| 36 | $this->_username = $username; |
||
| 37 | $this->_password = $password; |
||
| 38 | $this->_idSite = $idSite; |
||
| 39 | |||
| 40 | $this->login($this->_username, $this->_password, $idSite); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function login(string $username, string $password, string $idSite = ''): bool |
||
| 44 | { |
||
| 45 | |||
| 46 | $this->_logged = false; |
||
|
|
|||
| 47 | if (isNullOrEmpty($username) || isNullOrEmpty($password)) { |
||
| 48 | |||
| 49 | return false; |
||
| 50 | } |
||
| 51 | $this->_username = $username; |
||
| 52 | $this->_password = $password; |
||
| 53 | $this->_idSite = $idSite; |
||
| 54 | $credentials = array(); |
||
| 55 | $credentials["apiToken"] = $this->_username; |
||
| 56 | $credentials["apiSecret"] = $this->_password; |
||
| 57 | $credentials["affiliateId"] = $this->_idSite; |
||
| 58 | $this->_network->login($credentials); |
||
| 59 | |||
| 60 | if ($this->_network->checkConnection()) { |
||
| 61 | $this->_logged = true; |
||
| 62 | } |
||
| 63 | |||
| 64 | return $this->_logged; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | public function checkLogin(): bool |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return array of Merchants |
||
| 77 | * @throws \Exception |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function getMerchants(): array |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @param null $merchantID |
||
| 96 | * @param int $page |
||
| 97 | * @param int $items_per_page |
||
| 98 | * @return DealsResultset array of Deal |
||
| 99 | * @throws \Exception |
||
| 100 | */ |
||
| 101 | public function getDeals($merchantID = NULL, int $page = 0, int $items_per_page = 100): DealsResultset |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param \DateTime $dateFrom |
||
| 134 | * @param \DateTime $dateTo |
||
| 135 | * @param array $arrMerchantID |
||
| 136 | * @return array of Transaction |
||
| 137 | * @throws \Exception |
||
| 138 | */ |
||
| 139 | View Code Duplication | public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()): array |
|
| 140 | { |
||
| 141 | |||
| 142 | $arrResult = array(); |
||
| 143 | $transactionList = $this->_network->getTransactionList($arrMerchantID, $dateFrom, $dateTo); |
||
| 144 | foreach ($transactionList as $transaction) { |
||
| 145 | if (isset($transaction['commission']) && $transaction['commission'] < 0){ |
||
| 146 | continue; |
||
| 147 | } |
||
| 148 | $Transaction = Transaction::createInstance(); |
||
| 149 | if (isset($transaction['currency']) && !empty($transaction['currency'])) { |
||
| 150 | $Transaction->currency = $transaction['currency']; |
||
| 151 | } else { |
||
| 152 | $Transaction->currency = "EUR"; |
||
| 153 | } |
||
| 154 | $Transaction->status = $transaction['status']; |
||
| 155 | $Transaction->amount = $transaction['amount']; |
||
| 156 | array_key_exists_safe($transaction, 'custom_id') ? $Transaction->custom_ID = $transaction['custom_id'] : $Transaction->custom_ID = ''; |
||
| 157 | $Transaction->unique_ID = $transaction['unique_id']; |
||
| 158 | $Transaction->commission = $transaction['commission']; |
||
| 159 | $Transaction->date = $transaction['date']; |
||
| 160 | // Future use - Only few providers returns these dates values - <PN> - 2017-06-29 |
||
| 161 | if (isset($transaction['click_date']) && !empty($transaction['click_date'])) { |
||
| 162 | $Transaction->click_date = $transaction['click_date']; |
||
| 163 | } |
||
| 164 | if (isset($transaction['update_date']) && !empty($transaction['update_date'])) { |
||
| 165 | $Transaction->update_date = new \DateTime($transaction['update_date']); |
||
| 166 | } |
||
| 167 | $Transaction->merchant_ID = $transaction['merchantId']; |
||
| 168 | $Transaction->campaign_name = $transaction['campaign_name']; |
||
| 169 | $Transaction->approved = false; |
||
| 170 | if ($Transaction->status == \Oara\Utilities::STATUS_CONFIRMED) { |
||
| 171 | $Transaction->approved = true; |
||
| 172 | } |
||
| 173 | $arrResult[] = $Transaction; |
||
| 174 | } |
||
| 175 | |||
| 176 | return $arrResult; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param \DateTime $dateFrom |
||
| 181 | * @param \DateTime $dateTo |
||
| 182 | * @param int $merchantID |
||
| 183 | * @return array of Stat |
||
| 184 | */ |
||
| 185 | public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0): array |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * @param array $params |
||
| 193 | * @return ProductsResultset |
||
| 194 | * @throws \Exception |
||
| 195 | */ |
||
| 196 | public function getProducts(array $params = []): ProductsResultset |
||
| 200 | |||
| 201 | public function getTrackingParameter() |
||
| 205 | |||
| 206 | |||
| 207 | } |
||
| 208 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: