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:
1 | <?php |
||
27 | class CacheCleaner |
||
28 | { |
||
29 | /** |
||
30 | * @var VarnishUrlRegeneratorInterface |
||
31 | */ |
||
32 | protected $varnishUrlRegenerator; |
||
33 | |||
34 | /** |
||
35 | * @var VarnishUrlPurgerInterface |
||
36 | */ |
||
37 | protected $varnishUrlPurger; |
||
38 | |||
39 | /** |
||
40 | * @var LockInterface |
||
41 | */ |
||
42 | protected $lockHandler; |
||
43 | |||
44 | /** |
||
45 | * @var ScopeConfigInterface |
||
46 | */ |
||
47 | protected $scopeConfig; |
||
48 | |||
49 | /** |
||
50 | * @var ProductUrlProviderInterface |
||
51 | */ |
||
52 | protected $productUrlProvider; |
||
53 | |||
54 | /** |
||
55 | * @var CategoryUrlProviderInterface |
||
56 | */ |
||
57 | protected $categoryUrlProvider; |
||
58 | |||
59 | /** |
||
60 | * @var PurgingConfigProviderInterface |
||
61 | */ |
||
62 | protected $purgingConfigProvider; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $purgeBaseUrl; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $regenBaseUrl; |
||
73 | |||
74 | /** |
||
75 | * @var int |
||
76 | */ |
||
77 | protected $storeViewId; |
||
78 | |||
79 | /** |
||
80 | * @var bool |
||
81 | */ |
||
82 | public $verifyPeer = true; |
||
83 | |||
84 | /** |
||
85 | * CacheCleaner constructor. |
||
86 | * @param VarnishUrlRegeneratorFactory $varnishUrlRegeneratorFactory |
||
87 | * @param VarnishUrlPurgerFactory $varnishUrlPurgerFactory |
||
88 | * @param LockInterface $lockHandler |
||
89 | * @param ScopeConfigInterface $scopeConfig |
||
90 | * @param ProductUrlProviderInterface $productUrlProvider |
||
91 | * @param CategoryUrlProviderInterface $categoryUrlProvider |
||
92 | */ |
||
93 | public function __construct( |
||
113 | |||
114 | /** |
||
115 | * @param int $storeViewId |
||
116 | */ |
||
117 | public function setStoreViewId(int $storeViewId) |
||
121 | |||
122 | /** |
||
123 | * Purge * |
||
124 | * Regen homepage, categories, products |
||
125 | * @return void |
||
126 | */ |
||
127 | public function purgeWildcard(): void |
||
139 | |||
140 | /** |
||
141 | * Purge * without any regeneration |
||
142 | * Pass through lock |
||
143 | * @return void |
||
144 | */ |
||
145 | public function purgeWildcardWithoutRegen(): void |
||
151 | |||
152 | /** |
||
153 | * Purge homepage, categories, products |
||
154 | * Regen homepage, categories, products |
||
155 | * @return void |
||
156 | */ |
||
157 | public function purgeAll(): void |
||
169 | |||
170 | /** |
||
171 | * Purge homepage, categories |
||
172 | * Regen homepage, categories |
||
173 | * @return void |
||
174 | */ |
||
175 | public function purgeGeneral(): void |
||
186 | |||
187 | /** |
||
188 | * Purge homepage |
||
189 | * Regen homepage |
||
190 | * @return void |
||
191 | */ |
||
192 | View Code Duplication | public function purgeHomepage(): void |
|
202 | |||
203 | /** |
||
204 | * @param string $url |
||
205 | * @return void |
||
206 | */ |
||
207 | public function purgeAndRegenerateUrl(string $url): void |
||
215 | |||
216 | /** |
||
217 | * @param $product |
||
218 | * @return void |
||
219 | */ |
||
220 | public function purgeProduct($product): void |
||
229 | |||
230 | /** |
||
231 | * @return void |
||
232 | */ |
||
233 | View Code Duplication | public function purgeAndRegenerateProducts(): void |
|
242 | |||
243 | /** |
||
244 | * @return bool |
||
245 | */ |
||
246 | public function isLocked(): bool |
||
250 | |||
251 | /** |
||
252 | * @return string |
||
253 | */ |
||
254 | public function getLockMessage(): string |
||
258 | |||
259 | /** |
||
260 | * @param $relativeUrl |
||
261 | * @param bool $autoRegenerate |
||
262 | * @return void |
||
263 | */ |
||
264 | private function addUrlToPurge($relativeUrl, $autoRegenerate = false): void |
||
272 | |||
273 | /** |
||
274 | * @param $relativeUrl |
||
275 | * @return void |
||
276 | */ |
||
277 | private function addUrlToRegenerate($relativeUrl): void |
||
282 | |||
283 | /** |
||
284 | * @return void |
||
285 | */ |
||
286 | private function regenerateCategories(): void |
||
293 | |||
294 | /** |
||
295 | * @return void |
||
296 | */ |
||
297 | private function processCategoriesPurgeAndRegenerate(): void |
||
304 | |||
305 | /** |
||
306 | * @return void |
||
307 | */ |
||
308 | private function processProductsRegenerate(): void |
||
315 | |||
316 | /** |
||
317 | * @return void |
||
318 | */ |
||
319 | private function processProductsPurgeAndRegenerate(): void |
||
326 | |||
327 | /** |
||
328 | * @return array |
||
329 | */ |
||
330 | private function getAllProductsUrls(): array |
||
334 | |||
335 | /** |
||
336 | * @param $productId |
||
337 | * @return array |
||
338 | */ |
||
339 | private function getProductUrls($productId): array |
||
343 | |||
344 | /** |
||
345 | * @return array |
||
346 | */ |
||
347 | private function getCategories(): array |
||
351 | |||
352 | /** |
||
353 | * @return void |
||
354 | */ |
||
355 | private function lock(): void |
||
359 | |||
360 | /** |
||
361 | * @return void |
||
362 | */ |
||
363 | private function unlock(): void |
||
367 | |||
368 | /** |
||
369 | * @return void |
||
370 | */ |
||
371 | private function setPurgeBaseUrl(): void |
||
387 | |||
388 | /** |
||
389 | * @return void |
||
390 | */ |
||
391 | private function setRegenBaseUrl(): void |
||
403 | |||
404 | /** |
||
405 | * @return string |
||
406 | */ |
||
407 | private function getPurgeBaseUrl() |
||
414 | |||
415 | /** |
||
416 | * @return string |
||
417 | */ |
||
418 | private function getRegenBaseUrl() |
||
425 | } |
||
426 |
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.