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:
Complex classes like NetAffiliation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NetAffiliation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class NetAffiliation extends AbstractNetwork implements NetworkInterface |
||
23 | { |
||
24 | /** |
||
25 | * @var object |
||
26 | */ |
||
27 | private $_network = null; |
||
28 | private $_username = ''; |
||
29 | private $_password = ''; |
||
30 | private $_idSite = ''; |
||
31 | private $_logged = false; |
||
32 | protected $_tracking_parameter = 'argsite'; |
||
33 | protected $_merchants = array(); // To avoid repeated calls to getMerchants() |
||
34 | |||
35 | /** |
||
36 | * @method __construct |
||
37 | */ |
||
38 | public function __construct(string $username, string $password,string $idSite='') |
||
48 | |||
49 | public function addAllowedSite($idSite){ |
||
54 | |||
55 | View Code Duplication | public function login(string $username, string $password,string $idSite=''): bool |
|
|
|||
56 | { |
||
57 | if (trim($idSite)!=''){ |
||
58 | $this->addAllowedSite($idSite); |
||
59 | } |
||
60 | $this->_logged = false; |
||
61 | if (isNullOrEmpty( $username ) || isNullOrEmpty( $password )) { |
||
62 | |||
63 | return false; |
||
64 | } |
||
65 | $this->_username = $username; |
||
66 | $this->_password = $password; |
||
67 | $credentials = array(); |
||
68 | $credentials["user"] = $this->_username; |
||
69 | $credentials["password"] = $this->_password; |
||
70 | $credentials["apiPassword"] = $this->_password; |
||
71 | $this->_network->login( $credentials ); |
||
72 | //$this->_apiClient = $this->_network->getApiClient(); |
||
73 | if ($this->_network->checkConnection()) { |
||
74 | $this->_logged = true; |
||
75 | |||
76 | } |
||
77 | |||
78 | return $this->_logged; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return bool |
||
83 | */ |
||
84 | public function checkLogin() : bool |
||
88 | |||
89 | /** |
||
90 | * @return array of Merchants |
||
91 | */ |
||
92 | public function getMerchants() : array |
||
117 | |||
118 | /** |
||
119 | * @param int|null $merchantID |
||
120 | * @param int $page |
||
121 | * @param int $items_per_page |
||
122 | * |
||
123 | * @return DealsResultset |
||
124 | */ |
||
125 | public function getDeals($merchantID=NULL,int $page=0,int $items_per_page=10 ): DealsResultset |
||
170 | |||
171 | /** |
||
172 | * @param \DateTime $dateFrom |
||
173 | * @param \DateTime $dateTo |
||
174 | * @param int $merchantID |
||
175 | * @return array of Transaction |
||
176 | */ |
||
177 | public function getSales(\DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()) : array |
||
234 | |||
235 | /** |
||
236 | * @param \DateTime $dateFrom |
||
237 | * @param \DateTime $dateTo |
||
238 | * @param int $merchantID |
||
239 | * @return array of Stat |
||
240 | */ |
||
241 | public function getStats(\DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0) : array |
||
258 | |||
259 | |||
260 | /** |
||
261 | * @param array $params |
||
262 | * |
||
263 | * @return ProductsResultset |
||
264 | */ |
||
265 | public function getProducts(array $params = []): ProductsResultset |
||
270 | |||
271 | public function getTrackingParameter(){ |
||
274 | |||
275 | } |
||
276 |
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.