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 |
||
| 17 | class LeadAlliance extends AbstractNetwork implements NetworkInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var object |
||
| 21 | */ |
||
| 22 | private $_network = null; |
||
| 23 | private $_username = ''; |
||
| 24 | private $_password = ''; |
||
| 25 | private $_apiClient = null; |
||
| 26 | protected $_tracking_parameter = 'subid'; |
||
| 27 | private $_idSite = ''; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @method __construct |
||
| 31 | */ |
||
| 32 | public function __construct(string $username, string $password, string $idSite = '') |
||
| 41 | |||
| 42 | public function login(string $username, string $password, string $idSite = ''): bool |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return bool |
||
| 68 | */ |
||
| 69 | public function checkLogin(): bool |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return array of Merchants |
||
| 76 | * @throws \Exception |
||
| 77 | */ |
||
| 78 | public function getMerchants(): array |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param null $merchantID |
||
| 87 | * @param int $page |
||
| 88 | * @param int $items_per_page |
||
| 89 | * @return DealsResultset array of Deal |
||
| 90 | * @throws \Exception |
||
| 91 | */ |
||
| 92 | public function getDeals($merchantID = NULL, int $page = 0, int $items_per_page = 100): DealsResultset |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param \DateTime $dateFrom |
||
| 99 | * @param \DateTime $dateTo |
||
| 100 | * @param array $arrMerchantID |
||
| 101 | * @return array of Transaction |
||
| 102 | * @throws \Exception |
||
| 103 | */ |
||
| 104 | View Code Duplication | public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()): array |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @param \DateTime $dateFrom |
||
| 147 | * @param \DateTime $dateTo |
||
| 148 | * @param int $merchantID |
||
| 149 | * @return array of Stat |
||
| 150 | */ |
||
| 151 | public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0): array |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * @param array $params |
||
| 159 | * @return ProductsResultset |
||
| 160 | * @throws \Exception |
||
| 161 | */ |
||
| 162 | public function getProducts(array $params = []): ProductsResultset |
||
| 166 | |||
| 167 | public function getTrackingParameter() |
||
| 171 | } |
||
| 172 |
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: