Complex classes like UrlRewriteObserver 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 UrlRewriteObserver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class UrlRewriteObserver extends AbstractProductImportObserver |
||
41 | { |
||
42 | |||
43 | /** |
||
44 | * The entity type to load the URL rewrites for. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | const ENTITY_TYPE = 'product'; |
||
49 | |||
50 | /** |
||
51 | * The key for the category in the metadata. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | const CATEGORY_ID = 'category_id'; |
||
56 | |||
57 | /** |
||
58 | * The URL key from the CSV file column that has to be processed by the observer. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $urlKey; |
||
63 | |||
64 | /** |
||
65 | * The actual category ID to process. |
||
66 | * |
||
67 | * @var integer |
||
68 | */ |
||
69 | protected $categoryId; |
||
70 | |||
71 | /** |
||
72 | * The actual entity ID to process. |
||
73 | * |
||
74 | * @var integer |
||
75 | */ |
||
76 | protected $entityId; |
||
77 | |||
78 | /** |
||
79 | * The ID of the recently created URL rewrite. |
||
80 | * |
||
81 | * @var integer |
||
82 | */ |
||
83 | protected $urlRewriteId; |
||
84 | |||
85 | /** |
||
86 | * The array with the URL rewrites that has to be created. |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | protected $urlRewrites = array(); |
||
91 | |||
92 | /** |
||
93 | * The array with the category IDs related with the product. |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $productCategoryIds = array(); |
||
98 | |||
99 | /** |
||
100 | * The product bunch processor instance. |
||
101 | * |
||
102 | * @var \TechDivision\Import\Product\UrlRewrite\Services\ProductUrlRewriteProcessorInterface |
||
103 | */ |
||
104 | protected $productUrlRewriteProcessor; |
||
105 | |||
106 | /** |
||
107 | * Initialize the observer with the passed product URL rewrite processor instance. |
||
108 | * |
||
109 | * @param \TechDivision\Import\Product\UrlRewrite\Services\ProductUrlRewriteProcessorInterface $productUrlRewriteProcessor The product URL rewrite processor instance |
||
110 | */ |
||
111 | public function __construct(ProductUrlRewriteProcessorInterface $productUrlRewriteProcessor) |
||
115 | |||
116 | /** |
||
117 | * Return's the product bunch processor instance. |
||
118 | * |
||
119 | * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance |
||
120 | */ |
||
121 | protected function getProductUrlRewriteProcessor() |
||
125 | |||
126 | /** |
||
127 | * Process the observer's business logic. |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | protected function process() |
||
226 | |||
227 | /** |
||
228 | * Initialize the category product with the passed attributes and returns an instance. |
||
229 | * |
||
230 | * @param array $attr The category product attributes |
||
231 | * |
||
232 | * @return array The initialized category product |
||
233 | */ |
||
234 | protected function initializeUrlRewrite(array $attr) |
||
238 | |||
239 | /** |
||
240 | * Initialize the URL rewrite product => category relation with the passed attributes |
||
241 | * and returns an instance. |
||
242 | * |
||
243 | * @param array $attr The URL rewrite product => category relation attributes |
||
244 | * |
||
245 | * @return array The initialized URL rewrite product => category relation |
||
246 | */ |
||
247 | protected function initializeUrlRewriteProductCategory($attr) |
||
251 | |||
252 | /** |
||
253 | * Prepare's the URL rewrites that has to be created/updated. |
||
254 | * |
||
255 | * @return void |
||
256 | */ |
||
257 | protected function prepareUrlRewrites() |
||
288 | |||
289 | /** |
||
290 | * Resolve's the parent categories of the category with the passed ID and relate's |
||
291 | * it with the product with the passed ID, if the category is top level OR has the |
||
292 | * anchor flag set. |
||
293 | * |
||
294 | * @param integer $categoryId The ID of the category to resolve the parents |
||
295 | * @param integer $entityId The ID of the product entity to relate the category with |
||
296 | * @param boolean $topLevel TRUE if the passed category has top level, else FALSE |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | protected function resolveCategoryIds($categoryId, $entityId, $topLevel = false) |
||
334 | |||
335 | /** |
||
336 | * Prepare the attributes of the entity that has to be persisted. |
||
337 | * |
||
338 | * @return array The prepared attributes |
||
339 | */ |
||
340 | protected function prepareAttributes() |
||
369 | |||
370 | /** |
||
371 | * Prepare's the URL rewrite product => category relation attributes. |
||
372 | * |
||
373 | * @return array The prepared attributes |
||
374 | */ |
||
375 | protected function prepareUrlRewriteProductCategoryAttributes() |
||
387 | |||
388 | /** |
||
389 | * Prepare's the target path for a URL rewrite. |
||
390 | * |
||
391 | * @param array $category The categroy with the URL path |
||
392 | * |
||
393 | * @return string The target path |
||
394 | */ |
||
395 | protected function prepareTargetPath(array $category) |
||
411 | |||
412 | /** |
||
413 | * Prepare's the request path for a URL rewrite or the target path for a 301 redirect. |
||
414 | * |
||
415 | * @param array $category The categroy with the URL path |
||
416 | * |
||
417 | * @return string The request path |
||
418 | * @throws \RuntimeException Is thrown, if the passed category has no or an empty value for attribute "url_path" |
||
419 | */ |
||
420 | protected function prepareRequestPath(array $category) |
||
445 | |||
446 | /** |
||
447 | * Prepare's the URL rewrite's metadata with the passed category values. |
||
448 | * |
||
449 | * @param array $category The category used for preparation |
||
450 | * |
||
451 | * @return array The metadata |
||
452 | */ |
||
453 | protected function prepareMetadata(array $category) |
||
470 | |||
471 | /** |
||
472 | * Query whether or not the actual entity is visible or not. |
||
473 | * |
||
474 | * @return boolean TRUE if the entity is NOT visible, else FALSE |
||
475 | */ |
||
476 | protected function isNotVisible() |
||
480 | |||
481 | /** |
||
482 | * Return's the visibility for the passed entity ID, if it already has been mapped. The mapping will be created |
||
483 | * by calling <code>\TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue</code> which will |
||
484 | * be done by the <code>\TechDivision\Import\Product\Callbacks\VisibilityCallback</code>. |
||
485 | * |
||
486 | * @return integer The visibility ID |
||
487 | * @throws \Exception Is thrown, if the entity ID has not been mapped |
||
488 | * @see \TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue() |
||
489 | */ |
||
490 | protected function getEntityIdVisibilityIdMapping() |
||
494 | |||
495 | /** |
||
496 | * Return's the root category for the actual view store. |
||
497 | * |
||
498 | * @return array The store's root category |
||
499 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
500 | */ |
||
501 | protected function getRootCategory() |
||
505 | |||
506 | /** |
||
507 | * Return's TRUE if the passed category IS the root category, else FALSE. |
||
508 | * |
||
509 | * @param array $category The category to query |
||
510 | * |
||
511 | * @return boolean TRUE if the passed category IS the root category |
||
512 | */ |
||
513 | protected function isRootCategory(array $category) |
||
522 | |||
523 | /** |
||
524 | * Return's the list with category IDs the product is related with. |
||
525 | * |
||
526 | * @return array The product's category IDs |
||
527 | */ |
||
528 | protected function getProductCategoryIds() |
||
532 | |||
533 | /** |
||
534 | * Return's the category with the passed ID. |
||
535 | * |
||
536 | * @param integer $categoryId The ID of the category to return |
||
537 | * |
||
538 | * @return array The category data |
||
539 | */ |
||
540 | protected function getCategory($categoryId) |
||
544 | |||
545 | /** |
||
546 | * Persist's the URL rewrite with the passed data. |
||
547 | * |
||
548 | * @param array $row The URL rewrite to persist |
||
549 | * |
||
550 | * @return string The ID of the persisted entity |
||
551 | */ |
||
552 | protected function persistUrlRewrite($row) |
||
556 | |||
557 | /** |
||
558 | * Persist's the URL rewrite product => category relation with the passed data. |
||
559 | * |
||
560 | * @param array $row The URL rewrite product => category relation to persist |
||
561 | * |
||
562 | * @return void |
||
563 | */ |
||
564 | protected function persistUrlRewriteProductCategory($row) |
||
568 | |||
569 | /** |
||
570 | * Return's the column name that contains the primary key. |
||
571 | * |
||
572 | * @return string the column name that contains the primary key |
||
573 | */ |
||
574 | protected function getPrimaryKeyColumnName() |
||
578 | |||
579 | /** |
||
580 | * Queries whether or not the passed SKU and store view code has already been processed. |
||
581 | * |
||
582 | * @param string $sku The SKU to check been processed |
||
583 | * @param string $storeViewCode The store view code to check been processed |
||
584 | * |
||
585 | * @return boolean TRUE if the SKU and store view code has been processed, else FALSE |
||
586 | */ |
||
587 | protected function storeViewHasBeenProcessed($sku, $storeViewCode) |
||
591 | |||
592 | /** |
||
593 | * Add the entity ID => visibility mapping for the actual entity ID. |
||
594 | * |
||
595 | * @param string $visibility The visibility of the actual entity to map |
||
596 | * |
||
597 | * @return void |
||
598 | */ |
||
599 | protected function addEntityIdVisibilityIdMapping($visibility) |
||
603 | } |
||
604 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.