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 Admitad 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\Admitad; |
||
| 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{ |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return bool |
||
| 66 | */ |
||
| 67 | public function checkLogin() : bool |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return array of Merchants |
||
| 74 | * @throws \Exception |
||
| 75 | */ |
||
| 76 | public function getMerchants() : array |
||
| 77 | { |
||
| 78 | $arrResult = array(); |
||
| 79 | $merchantList = $this->_network->getMerchantList(); |
||
| 80 | foreach($merchantList as $merchant) { |
||
| 81 | $Merchant = Merchant::createInstance(); |
||
| 82 | $Merchant->merchant_ID = $merchant['cid']; |
||
| 83 | $Merchant->name = $merchant['name']; |
||
| 84 | $Merchant->status = $merchant['status']; |
||
| 85 | $Merchant->url = $merchant['url']; |
||
| 86 | if (!empty($merchant['launch_date'])) { |
||
| 87 | $date = new \DateTime($merchant['launch_date']); |
||
| 88 | $Merchant->launch_date = $date; |
||
| 89 | } |
||
| 90 | $arrResult[] = $Merchant; |
||
| 91 | } |
||
| 92 | return $arrResult; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param null $merchantID |
||
| 97 | * @param int $page |
||
| 98 | * @param int $items_per_page |
||
| 99 | * @return DealsResultset array of Deal |
||
| 100 | * @throws \Exception |
||
| 101 | */ |
||
| 102 | public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=100 ): DealsResultset |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param \DateTime $dateFrom |
||
| 138 | * @param \DateTime $dateTo |
||
| 139 | * @param array $arrMerchantID |
||
| 140 | * @return array of Transaction |
||
| 141 | * @throws \Exception |
||
| 142 | */ |
||
| 143 | View Code Duplication | public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @param \DateTime $dateFrom |
||
| 185 | * @param \DateTime $dateTo |
||
| 186 | * @param int $merchantID |
||
| 187 | * @return array of Stat |
||
| 188 | */ |
||
| 189 | public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * @param array $params |
||
| 211 | * @return ProductsResultset |
||
| 212 | * @throws \Exception |
||
| 213 | */ |
||
| 214 | public function getProducts(array $params = []): ProductsResultset |
||
| 219 | |||
| 220 | public function getTrackingParameter(){ |
||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | } |
||
| 227 |
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: