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 VarnishPurger 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 VarnishPurger, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class VarnishPurger implements VarnishPurgerInterface |
||
32 | { |
||
33 | /** |
||
34 | * @var VarnishUrlRegeneratorInterface |
||
35 | */ |
||
36 | protected $varnishUrlRegenerator; |
||
37 | |||
38 | /** |
||
39 | * @var VarnishUrlPurgerInterface |
||
40 | */ |
||
41 | protected $varnishUrlPurger; |
||
42 | |||
43 | /** |
||
44 | * @var LockInterface |
||
45 | */ |
||
46 | protected $lockHandler; |
||
47 | |||
48 | /** |
||
49 | * @var ScopeConfigInterface |
||
50 | */ |
||
51 | protected $scopeConfig; |
||
52 | |||
53 | /** |
||
54 | * @var ProductUrlProviderInterface |
||
55 | */ |
||
56 | protected $productUrlProvider; |
||
57 | |||
58 | /** |
||
59 | * @var CategoryUrlProviderInterface |
||
60 | */ |
||
61 | protected $categoryUrlProvider; |
||
62 | |||
63 | /** |
||
64 | * @var PurgingConfigProviderInterface |
||
65 | */ |
||
66 | protected $purgingConfigProvider; |
||
67 | |||
68 | /** |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $purgeBaseUrls; |
||
72 | |||
73 | /** |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $regenBaseUrl; |
||
77 | |||
78 | /** |
||
79 | * @var int |
||
80 | */ |
||
81 | protected $storeViewId; |
||
82 | |||
83 | /** |
||
84 | * VarnishPurger constructor. |
||
85 | * @param VarnishUrlRegeneratorFactory $varnishUrlRegeneratorFactory |
||
86 | * @param VarnishUrlPurgerFactory $varnishUrlPurgerFactory |
||
87 | * @param LockInterface $lockHandler |
||
88 | * @param ScopeConfigInterface $scopeConfig |
||
89 | * @param ProductUrlProviderInterface $productUrlProvider |
||
90 | * @param CategoryUrlProviderInterface $categoryUrlProvider |
||
91 | * @param PurgingConfigProviderInterface $purgingConfigProvider |
||
92 | */ |
||
93 | public function __construct( |
||
113 | |||
114 | /** |
||
115 | * Purge * |
||
116 | * Regen homepage, categories, products |
||
117 | * @return void |
||
118 | */ |
||
119 | View Code Duplication | public function purgeWildcard(): void |
|
130 | |||
131 | /** |
||
132 | * Purge * without any regeneration |
||
133 | * Pass through lock |
||
134 | * @return void |
||
135 | */ |
||
136 | public function purgeWildcardWithoutRegen(): void |
||
141 | |||
142 | /** |
||
143 | * Purge homepage, categories, products |
||
144 | * Regen homepage, categories, products |
||
145 | * @return void |
||
146 | */ |
||
147 | View Code Duplication | public function purgeAll(): void |
|
158 | |||
159 | /** |
||
160 | * Purge homepage, categories |
||
161 | * Regen homepage, categories |
||
162 | * @return void |
||
163 | */ |
||
164 | View Code Duplication | public function purgeGeneral(): void |
|
174 | |||
175 | /** |
||
176 | * Purge homepage |
||
177 | * Regen homepage |
||
178 | * @return void |
||
179 | */ |
||
180 | View Code Duplication | public function purgeHomepage(): void |
|
189 | |||
190 | /** |
||
191 | * @return void |
||
192 | */ |
||
193 | public function purgeAndRegenerateProducts(): void |
||
201 | |||
202 | /** |
||
203 | * @param string $url |
||
204 | * @return void |
||
205 | */ |
||
206 | public function purgeAndRegenerateUrl(string $url): void |
||
213 | |||
214 | /** |
||
215 | * @param ProductInterface $product |
||
216 | * @return void |
||
217 | */ |
||
218 | public function purgeProduct(ProductInterface $product): void |
||
227 | |||
228 | /** |
||
229 | * @param int $storeViewId |
||
230 | */ |
||
231 | public function setStoreViewId(int $storeViewId) |
||
235 | |||
236 | /** |
||
237 | * @return bool |
||
238 | */ |
||
239 | public function isLocked(): bool |
||
243 | |||
244 | /** |
||
245 | * @return string |
||
246 | */ |
||
247 | public function getLockMessage(): string |
||
251 | |||
252 | /** |
||
253 | * @param $relativeUrl |
||
254 | * @param bool $autoRegenerate |
||
255 | * @return void |
||
256 | */ |
||
257 | private function addUrlToPurge($relativeUrl, $autoRegenerate = false): void |
||
267 | |||
268 | /** |
||
269 | * @param $relativeUrl |
||
270 | * @return void |
||
271 | */ |
||
272 | private function addUrlToRegenerate($relativeUrl): void |
||
277 | |||
278 | /** |
||
279 | * @return void |
||
280 | */ |
||
281 | private function regenerateCategories(): void |
||
288 | |||
289 | /** |
||
290 | * @return void |
||
291 | */ |
||
292 | private function processCategoriesPurgeAndRegenerate(): void |
||
299 | |||
300 | /** |
||
301 | * @return void |
||
302 | */ |
||
303 | private function processProductsRegenerate(): void |
||
310 | |||
311 | /** |
||
312 | * @return void |
||
313 | */ |
||
314 | private function processProductsPurgeAndRegenerate(): void |
||
321 | |||
322 | /** |
||
323 | * @return array |
||
324 | */ |
||
325 | private function getAllProductsUrls(): array |
||
329 | |||
330 | /** |
||
331 | * @param $productId |
||
332 | * @return array |
||
333 | */ |
||
334 | private function getProductUrls($productId): array |
||
338 | |||
339 | /** |
||
340 | * @return array |
||
341 | */ |
||
342 | private function getCategories(): array |
||
346 | |||
347 | /** |
||
348 | * @return void |
||
349 | */ |
||
350 | private function lock(): void |
||
354 | |||
355 | /** |
||
356 | * @return void |
||
357 | */ |
||
358 | private function unlock(): void |
||
362 | |||
363 | /** |
||
364 | * @return void |
||
365 | */ |
||
366 | private function setPurgeBaseUrls(): void |
||
385 | |||
386 | /** |
||
387 | * @return void |
||
388 | */ |
||
389 | private function setRegenBaseUrl(): void |
||
401 | |||
402 | /** |
||
403 | * @return array |
||
404 | */ |
||
405 | private function getPurgeBaseUrls() |
||
412 | |||
413 | /** |
||
414 | * @return string |
||
415 | */ |
||
416 | private function getRegenBaseUrl() |
||
423 | } |
||
424 |
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.