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 NetworkManager 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 NetworkManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class NetworkManager |
||
15 | { |
||
16 | /** |
||
17 | * Network/api |
||
18 | * @var NetworkInterface |
||
19 | */ |
||
20 | protected $networks = []; |
||
21 | protected $avaliable_networks = []; |
||
22 | protected $loggerService = null; |
||
23 | |||
24 | /** |
||
25 | * Register the dependencies |
||
26 | */ |
||
27 | public function __construct() |
||
31 | |||
32 | public function logCapture() |
||
43 | |||
44 | /** |
||
45 | * Flush the captured log messages |
||
46 | */ |
||
47 | public function logFlush() |
||
71 | |||
72 | /** |
||
73 | * Inject an external Log service compatible with Psr\Log interface |
||
74 | * @param Psr\Log\LoggerInterface $loggerService |
||
75 | */ |
||
76 | public function setLogger(\Psr\Log\LoggerInterface $loggerService) |
||
80 | |||
81 | /** |
||
82 | * Add a network / api to use |
||
83 | * |
||
84 | * @param NetworkInterface $network |
||
85 | * @param string $network_alias |
||
86 | */ |
||
87 | public function addNetwork(NetworkInterface $network, $network_alias = '') |
||
99 | |||
100 | /** |
||
101 | * Get a network |
||
102 | * @return NetworkInterface|null |
||
103 | */ |
||
104 | public function getNetwork($network_alias) |
||
111 | |||
112 | /** |
||
113 | * @return array |
||
114 | */ |
||
115 | protected function loadAvailableNetworks(){ |
||
125 | |||
126 | public function getAvailableNetworks():array { |
||
129 | |||
130 | /** |
||
131 | * @param $network_alias |
||
132 | * @param string $username |
||
133 | * @param string $password |
||
134 | * @param string $id_site |
||
135 | * @param string $country |
||
136 | * @return bool |
||
137 | */ |
||
138 | public function hasNetwork($network_alias, $username = '', $password = '', string $id_site = '', string $country = '') { |
||
145 | |||
146 | /** |
||
147 | * @param $network_alias |
||
148 | * @param int $merchantID |
||
149 | * |
||
150 | * @return \Padosoft\AffiliateNetwork\DealsResultset |
||
151 | */ |
||
152 | public function getDeals($network_alias, int $merchantID = 0, int $page = 0, int $items_per_page = 0 ): DealsResultset |
||
165 | |||
166 | /** |
||
167 | * @param \DateTime $dateFrom |
||
168 | * @param \DateTime $dateTo |
||
169 | * @param int $merchantID |
||
|
|||
170 | * |
||
171 | * @return array of Transaction |
||
172 | */ |
||
173 | public function getSales(string $network_alias, \DateTime $dateFrom, \DateTime $dateTo, array $arrMerchantID = array()): array |
||
183 | |||
184 | /** |
||
185 | * @param \DateTime $dateFrom |
||
186 | * @param \DateTime $dateTo |
||
187 | * @param int $merchantID |
||
188 | * |
||
189 | * @return array of Stat |
||
190 | */ |
||
191 | public function getStats(string $network_alias, \DateTime $dateFrom, \DateTime $dateTo, int $merchantID = 0): array |
||
198 | |||
199 | View Code Duplication | public function getMerchants(string $network_alias) : array{ |
|
209 | |||
210 | View Code Duplication | public function checkLogin(string $network_alias): bool{ |
|
219 | |||
220 | public function login(string $network_alias, string $username, string $password, string $id_site = '', string $country = ''): bool{ |
||
232 | |||
233 | public function getTrackingParameter(string $network_alias):string { |
||
239 | } |
||
240 |
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
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.