Complex classes like VarnishActionManager 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 VarnishActionManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class VarnishActionManager implements VarnishActionManagerInterface |
||
39 | { |
||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | private const DEFAULT_FRONTEND_STORE_VIEW_ID = 1; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | private $purgeBaseUrls; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $regenBaseUrl; |
||
54 | |||
55 | /** |
||
56 | * @var int |
||
57 | */ |
||
58 | private $storeViewId; |
||
59 | |||
60 | /** |
||
61 | * @var bool |
||
62 | */ |
||
63 | private $isStoreCodeUsedInUrls; |
||
64 | |||
65 | /** |
||
66 | * @var StoreInterface |
||
67 | */ |
||
68 | private $currentStoreView; |
||
69 | |||
70 | /** |
||
71 | * @var VarnishUrlRegeneratorInterface |
||
72 | */ |
||
73 | private $varnishUrlRegenerator; |
||
74 | |||
75 | /** |
||
76 | * @var VarnishUrlPurgerInterface |
||
77 | */ |
||
78 | private $varnishUrlPurger; |
||
79 | |||
80 | /** |
||
81 | * @var LockInterface |
||
82 | */ |
||
83 | private $lockHandler; |
||
84 | |||
85 | /** |
||
86 | * @var ScopeConfigInterface |
||
87 | */ |
||
88 | private $scopeConfig; |
||
89 | |||
90 | /** |
||
91 | * @var ProductUrlProviderInterface |
||
92 | */ |
||
93 | private $productUrlProvider; |
||
94 | |||
95 | /** |
||
96 | * @var CategoryUrlProviderInterface |
||
97 | */ |
||
98 | private $categoryUrlProvider; |
||
99 | |||
100 | /** |
||
101 | * @var PurgingConfigProviderInterface |
||
102 | */ |
||
103 | private $purgingConfigProvider; |
||
104 | |||
105 | /** |
||
106 | * @var StoreManagerInterface |
||
107 | */ |
||
108 | private $storeManager; |
||
109 | |||
110 | /** |
||
111 | * VarnishActionManager constructor. |
||
112 | * @param VarnishUrlRegeneratorFactory $varnishUrlRegeneratorFactory |
||
113 | * @param VarnishUrlPurgerFactory $varnishUrlPurgerFactory |
||
114 | * @param LockInterface $lockHandler |
||
115 | * @param ScopeConfigInterface $scopeConfig |
||
116 | * @param ProductUrlProviderInterface $productUrlProvider |
||
117 | * @param CategoryUrlProviderInterface $categoryUrlProvider |
||
118 | * @param PurgingConfigProviderInterface $purgingConfigProvider |
||
119 | * @param StoreManagerInterface $storeManager |
||
120 | * @SuppressWarnings(PHPMD.LongVariable) |
||
121 | * @throws NoSuchEntityException |
||
122 | */ |
||
123 | public function __construct( |
||
147 | |||
148 | /** |
||
149 | * Purge * |
||
150 | * Regen homepage, categories, products |
||
151 | * @return void |
||
152 | * @throws NoSuchEntityException |
||
153 | */ |
||
154 | public function purgeWildcard(): void |
||
165 | |||
166 | /** |
||
167 | * Purge * without any regeneration |
||
168 | * Pass through lock |
||
169 | * @return void |
||
170 | * @throws NoSuchEntityException |
||
171 | */ |
||
172 | public function purgeWildcardWithoutRegen(): void |
||
177 | |||
178 | /** |
||
179 | * Purge homepage, categories, products |
||
180 | * Regen homepage, categories, products |
||
181 | * @return void |
||
182 | * @throws NoSuchEntityException |
||
183 | */ |
||
184 | public function purgeAll(): void |
||
195 | |||
196 | /** |
||
197 | * Purge homepage, categories |
||
198 | * Regen homepage, categories |
||
199 | * @return void |
||
200 | * @throws NoSuchEntityException |
||
201 | */ |
||
202 | public function purgeGeneral(): void |
||
212 | |||
213 | /** |
||
214 | * Purge homepage |
||
215 | * Regen homepage |
||
216 | * @return void |
||
217 | * @throws NoSuchEntityException |
||
218 | */ |
||
219 | public function purgeHomepage(): void |
||
228 | |||
229 | /** |
||
230 | * @return void |
||
231 | * @throws NoSuchEntityException |
||
232 | */ |
||
233 | public function purgeAndRegenerateProducts(): void |
||
241 | |||
242 | /** |
||
243 | * @param string $url |
||
244 | * @return void |
||
245 | * @throws NoSuchEntityException |
||
246 | */ |
||
247 | public function purgeAndRegenerateUrl(string $url): void |
||
254 | |||
255 | /** |
||
256 | * @param ProductInterface $product |
||
257 | * @return void |
||
258 | * @throws NoSuchEntityException |
||
259 | */ |
||
260 | public function purgeProduct(ProductInterface $product): void |
||
271 | |||
272 | /** |
||
273 | * @param int $storeViewId |
||
274 | */ |
||
275 | public function setStoreViewId(int $storeViewId): void |
||
279 | |||
280 | /** |
||
281 | * @return bool |
||
282 | */ |
||
283 | public function isLocked(): bool |
||
287 | |||
288 | /** |
||
289 | * @return string |
||
290 | */ |
||
291 | public function getLockMessage(): string |
||
295 | |||
296 | /** |
||
297 | * @return int |
||
298 | * @throws NoSuchEntityException |
||
299 | */ |
||
300 | private function getDefaultStoreViewId(): int |
||
307 | |||
308 | /** |
||
309 | * @return bool |
||
310 | */ |
||
311 | private function isStoreCodeUsedInUrls(): bool |
||
319 | |||
320 | /** |
||
321 | * @param $relativeUrl |
||
322 | * @param bool $autoRegenerate |
||
323 | * @return void |
||
324 | * @throws NoSuchEntityException |
||
325 | */ |
||
326 | private function addUrlToPurge(string $relativeUrl, bool $autoRegenerate = false): void |
||
337 | |||
338 | /** |
||
339 | * @param string $relativeUrl |
||
340 | * @return void |
||
341 | * @throws NoSuchEntityException |
||
342 | */ |
||
343 | private function addUrlToRegenerate(string $relativeUrl): void |
||
348 | |||
349 | /** |
||
350 | * @return string |
||
351 | * @throws NoSuchEntityException |
||
352 | */ |
||
353 | private function buildStoreCodePartIfUsed(): string |
||
361 | |||
362 | /** |
||
363 | * @return string |
||
364 | * @throws NoSuchEntityException |
||
365 | */ |
||
366 | private function getStoreCode(): string |
||
371 | |||
372 | /** |
||
373 | * @return StoreInterface |
||
374 | * @throws NoSuchEntityException |
||
375 | */ |
||
376 | private function getCurrentStoreView(): StoreInterface |
||
384 | |||
385 | /** |
||
386 | * @return void |
||
387 | * @throws NoSuchEntityException |
||
388 | */ |
||
389 | private function regenerateCategories(): void |
||
397 | |||
398 | /** |
||
399 | * @return void |
||
400 | * @throws NoSuchEntityException |
||
401 | */ |
||
402 | private function processCategoriesPurgeAndRegenerate(): void |
||
410 | |||
411 | /** |
||
412 | * @return void |
||
413 | * @throws NoSuchEntityException |
||
414 | */ |
||
415 | private function processProductsRegenerate(): void |
||
423 | |||
424 | /** |
||
425 | * @return void |
||
426 | * @throws NoSuchEntityException |
||
427 | */ |
||
428 | private function processProductsPurgeAndRegenerate(): void |
||
436 | |||
437 | /** |
||
438 | * @return array |
||
439 | */ |
||
440 | private function getAllProductsUrls(): array |
||
444 | |||
445 | /** |
||
446 | * @param $productId |
||
447 | * @return array |
||
448 | */ |
||
449 | private function getProductUrls(int $productId): array |
||
453 | |||
454 | /** |
||
455 | * @return array |
||
456 | */ |
||
457 | private function getCategories(): array |
||
461 | |||
462 | /** |
||
463 | * @return void |
||
464 | */ |
||
465 | private function lock(): void |
||
469 | |||
470 | /** |
||
471 | * @return void |
||
472 | */ |
||
473 | private function unlock(): void |
||
477 | |||
478 | /** |
||
479 | * @return void |
||
480 | */ |
||
481 | private function setPurgeBaseUrls(): void |
||
500 | |||
501 | /** |
||
502 | * @return void |
||
503 | */ |
||
504 | private function setRegenBaseUrl(): void |
||
516 | |||
517 | /** |
||
518 | * @return array |
||
519 | */ |
||
520 | private function getPurgeBaseUrls(): array |
||
528 | |||
529 | /** |
||
530 | * @return string |
||
531 | */ |
||
532 | private function getRegenBaseUrl(): string |
||
540 | } |
||
541 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.