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 ZohoDatabasePusher 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 ZohoDatabasePusher, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class ZohoDatabasePusher |
||
18 | { |
||
19 | /** |
||
20 | * @var Connection |
||
21 | */ |
||
22 | private $connection; |
||
23 | |||
24 | /** |
||
25 | * @var LoggerInterface |
||
26 | */ |
||
27 | private $logger; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $prefix; |
||
33 | |||
34 | /** |
||
35 | * @param Connection $connection |
||
36 | * @param int $apiLimitInsertUpdateDelete |
||
37 | * @param string $prefix |
||
38 | * @param LoggerInterface $logger |
||
39 | */ |
||
40 | public function __construct(Connection $connection, $apiLimitInsertUpdateDelete = 100, $prefix = 'zoho_', LoggerInterface $logger = null) |
||
54 | |||
55 | /** |
||
56 | * @var int |
||
57 | */ |
||
58 | private $apiLimitInsertUpdateDelete; |
||
59 | |||
60 | /** |
||
61 | * @param AbstractZohoDao $zohoDao |
||
62 | * @param bool $update |
||
63 | * @return int |
||
64 | */ |
||
65 | private function countElementInTable(AbstractZohoDao $zohoDao, $update = false) |
||
74 | |||
75 | /** |
||
76 | * Insert or Update rows. |
||
77 | * |
||
78 | * @param AbstractZohoDao $zohoDao |
||
79 | */ |
||
80 | public function pushDataToZoho(AbstractZohoDao $zohoDao, $update = false) |
||
137 | |||
138 | /** |
||
139 | * @param AbstractZohoDao $zohoDao |
||
140 | * @param ZohoBeanInterface[] $zohoBeans |
||
141 | * @param string[] $rowsDeleted |
||
142 | * @param bool $update |
||
143 | */ |
||
144 | private function sendDataToZohoCleanLocal(AbstractZohoDao $zohoDao, array $zohoBeans,$rowsDeleted, $update = false) |
||
178 | |||
179 | /** |
||
180 | * Insert data to bean in order to insert zoho records. |
||
181 | * |
||
182 | * @param AbstractZohoDao $dao |
||
183 | * @param ZohoBeanInterface $zohoBean |
||
184 | * @param array $row |
||
185 | */ |
||
186 | View Code Duplication | private function insertDataZohoBean(AbstractZohoDao $dao, ZohoBeanInterface $zohoBean, array $row) |
|
200 | |||
201 | /** |
||
202 | * Insert data to bean in order to update zoho records. |
||
203 | * |
||
204 | * @param ZohoBeanInterface $zohoBean |
||
205 | * @param array $fieldsMatching |
||
206 | * @param type $columnName |
||
207 | * @param type $valueDb |
||
208 | */ |
||
209 | View Code Duplication | private function updateDataZohoBean(AbstractZohoDao $dao, ZohoBeanInterface $zohoBean, $columnName, $valueDb) |
|
221 | |||
222 | /** |
||
223 | * Change the value to the good format. |
||
224 | * |
||
225 | * @param string $type |
||
226 | * @param mixed $value |
||
227 | * |
||
228 | * @return mixed |
||
229 | */ |
||
230 | private function formatValueToBeans($type, $value) |
||
257 | |||
258 | /** |
||
259 | * Run deleted rows to Zoho : local_delete. |
||
260 | * |
||
261 | * @param AbstractZohoDao $zohoDao |
||
262 | */ |
||
263 | public function pushDeletedRows(AbstractZohoDao $zohoDao) |
||
282 | |||
283 | /** |
||
284 | * Run inserted rows to Zoho : local_insert. |
||
285 | * |
||
286 | * @param AbstractZohoDao $zohoDao |
||
287 | */ |
||
288 | public function pushInsertedRows(AbstractZohoDao $zohoDao) |
||
292 | |||
293 | /** |
||
294 | * Run updated rows to Zoho : local_update. |
||
295 | * |
||
296 | * @param AbstractZohoDao $zohoDao |
||
297 | */ |
||
298 | public function pushUpdatedRows(AbstractZohoDao $zohoDao) |
||
302 | |||
303 | /** |
||
304 | * Push data from db to Zoho. |
||
305 | * |
||
306 | * @param AbstractZohoDao $zohoDao |
||
307 | */ |
||
308 | public function pushToZoho(AbstractZohoDao $zohoDao) |
||
317 | } |
||
318 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: