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 AffiliateFuture 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 = 'tracking'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @method __construct |
||
| 33 | */ |
||
| 34 | public function __construct(string $username, string $password) |
||
| 35 | { |
||
| 36 | $this->_network = new \Oara\Network\Publisher\AffiliateFuture(); |
||
| 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["user"] = $this->_username; |
||
| 53 | $credentials["password"] = $this->_password; |
||
| 54 | $this->_network->login( $credentials ); |
||
| 55 | if ($this->_network->checkConnection()) { |
||
| 56 | $this->_logged = true; |
||
| 57 | } |
||
| 58 | return $this->_logged; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return bool |
||
| 63 | */ |
||
| 64 | public function checkLogin() : bool |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return array of Merchants |
||
| 71 | */ |
||
| 72 | public function getMerchants() : array |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param int|null $merchantID |
||
| 92 | * @param int $page |
||
| 93 | * @param int $items_per_page |
||
| 94 | * |
||
| 95 | * @return DealsResultset |
||
| 96 | */ |
||
| 97 | public function getDeals($merchantID,int $page=0,int $items_per_page=10) : DealsResultset |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param \DateTime $dateFrom |
||
| 105 | * @param \DateTime $dateTo |
||
| 106 | * @param int $merchantID |
||
|
|
|||
| 107 | * @return array of Transaction |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param \DateTime $dateFrom |
||
| 154 | * @param \DateTime $dateTo |
||
| 155 | * @param int $merchantID |
||
| 156 | * @return array of Stat |
||
| 157 | */ |
||
| 158 | public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array |
||
| 162 | |||
| 163 | |||
| 164 | /** |
||
| 165 | * @param array $params |
||
| 166 | * |
||
| 167 | * @return ProductsResultset |
||
| 168 | */ |
||
| 169 | public function getProducts(array $params = []): ProductsResultset |
||
| 174 | |||
| 175 | public function getTrackingParameter(){ |
||
| 178 | } |
||
| 179 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.