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 DataHandlerHook 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 DataHandlerHook, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class DataHandlerHook |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * Store indexed file before the Data Handler start "working". |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $beforeDataHandlerProcessFileIdentifiers = []; |
||
32 | |||
33 | /** |
||
34 | * Store indexed file after the Data Handler has done its job. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $afterDataHandlerProcessFileIdentifiers = []; |
||
39 | |||
40 | /** |
||
41 | * Internal key for the Cache Manager. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $registerKey = 'media-hook-elementsToKeepTrack'; |
||
46 | |||
47 | /** |
||
48 | * First procedures to launch before all operations in DataHandler. |
||
49 | * |
||
50 | * Feed variable $this->beforeDataHandlerProcessFileIdentifiers |
||
51 | * |
||
52 | * @param \TYPO3\CMS\Core\DataHandling\DataHandler $caller TCEMain Object |
||
53 | * @return void |
||
54 | */ |
||
55 | public function processDatamap_beforeStart(DataHandler $caller) |
||
87 | |||
88 | /** |
||
89 | * Last procedures to launch after all operations in DataHandler. |
||
90 | * |
||
91 | * Process field "number_of_references" which may require updates. |
||
92 | * |
||
93 | * @param \TYPO3\CMS\Core\DataHandling\DataHandler $caller TCEMain Object |
||
94 | * @return void |
||
95 | */ |
||
96 | public function processDatamap_afterAllOperations(DataHandler $caller) |
||
136 | |||
137 | /** |
||
138 | * @return void |
||
139 | */ |
||
140 | protected function initializeFileRegister() |
||
148 | |||
149 | /** |
||
150 | * @return void |
||
151 | */ |
||
152 | protected function registerFilesToKeepTrack() |
||
175 | |||
176 | /** |
||
177 | * @return array |
||
178 | */ |
||
179 | protected function getRegisteredFiles() |
||
184 | |||
185 | /** |
||
186 | * Look for file which are within the reference index. |
||
187 | * |
||
188 | * @return array |
||
189 | */ |
||
190 | protected function getFileToProcess() |
||
199 | |||
200 | /** |
||
201 | * @param array $fileIdentifiers |
||
202 | * @return void |
||
203 | */ |
||
204 | protected function addBeforeDataHandlerProcessFileIdentifiers(array $fileIdentifiers) |
||
208 | |||
209 | /** |
||
210 | * @param array $fileIdentifiers |
||
211 | * @return void |
||
212 | */ |
||
213 | protected function addAfterDataHandlerProcessFileIdentifiers(array $fileIdentifiers) |
||
217 | |||
218 | /** |
||
219 | * Look for file which are within the reference index. |
||
220 | * |
||
221 | * @param array $indexes |
||
222 | * @return array |
||
223 | */ |
||
224 | protected function lookForFiles(array $indexes) |
||
245 | |||
246 | /** |
||
247 | * @param array $index |
||
248 | * @return bool |
||
249 | */ |
||
250 | public function isFileReference(array $index) |
||
254 | |||
255 | /** |
||
256 | * @param array $index |
||
257 | * @return bool |
||
258 | */ |
||
259 | public function isSoftReferenceLink(array $index) |
||
263 | |||
264 | /** |
||
265 | * @param array $index |
||
266 | * @return bool |
||
267 | */ |
||
268 | public function isSoftReferenceImage(array $index) |
||
272 | |||
273 | /** |
||
274 | * Retrieve the File identifier. |
||
275 | * |
||
276 | * @param $fileReferenceIdentifier |
||
277 | * @return int |
||
278 | * @throws \Exception |
||
279 | */ |
||
280 | protected function findFileByFileReference($fileReferenceIdentifier) |
||
296 | |||
297 | /** |
||
298 | * @return \Fab\Media\Resource\FileReferenceService|object |
||
299 | */ |
||
300 | protected function getFileReferenceService() |
||
304 | |||
305 | /** |
||
306 | * Gets an instance of the memory cache. |
||
307 | * |
||
308 | * @return \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend |
||
309 | */ |
||
310 | protected function getMemoryCache() |
||
314 | |||
315 | /** |
||
316 | * Create and returns an instance of the CacheManager |
||
317 | * |
||
318 | * @return \TYPO3\CMS\Core\Cache\CacheManager|object |
||
319 | */ |
||
320 | protected function getCacheManager() |
||
324 | |||
325 | /** |
||
326 | * @return object|DataService |
||
327 | */ |
||
328 | protected function getDataService(): DataService |
||
332 | |||
333 | } |
||
334 |
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.