Complex classes like Product 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 Product, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Product implements ProductInterface |
||
27 | { |
||
28 | use TimestampableTrait, ToggleableTrait; |
||
29 | use TranslatableTrait { |
||
30 | __construct as private initializeTranslationsCollection; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @var mixed |
||
35 | */ |
||
36 | protected $id; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $code; |
||
42 | |||
43 | /** |
||
44 | * @var \DateTime |
||
45 | */ |
||
46 | protected $availableOn; |
||
47 | |||
48 | /** |
||
49 | * @var \DateTime |
||
50 | */ |
||
51 | protected $availableUntil; |
||
52 | |||
53 | /** |
||
54 | * @var Collection|AttributeValueInterface[] |
||
55 | */ |
||
56 | protected $attributes; |
||
57 | |||
58 | /** |
||
59 | * @var Collection|ProductVariantInterface[] |
||
60 | */ |
||
61 | protected $variants; |
||
62 | |||
63 | /** |
||
64 | * @var Collection|ProductOptionInterface[] |
||
65 | */ |
||
66 | protected $options; |
||
67 | |||
68 | /** |
||
69 | * @var Collection|ProductAssociationInterface[] |
||
70 | */ |
||
71 | protected $associations; |
||
72 | |||
73 | public function __construct() |
||
74 | { |
||
75 | $this->initializeTranslationsCollection(); |
||
76 | |||
77 | $this->createdAt = new \DateTime(); |
||
78 | $this->availableOn = new \DateTime(); |
||
79 | $this->attributes = new ArrayCollection(); |
||
80 | $this->associations = new ArrayCollection(); |
||
81 | $this->variants = new ArrayCollection(); |
||
82 | $this->options = new ArrayCollection(); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return string |
||
87 | */ |
||
88 | public function __toString() |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function getId() |
||
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | */ |
||
104 | public function getCode() |
||
108 | |||
109 | /** |
||
110 | * @param string $code |
||
111 | */ |
||
112 | public function setCode($code) |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function getName() |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function setName($name) |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function getSlug() |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function setSlug($slug = null) |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function getDescription() |
||
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | public function setDescription($description) |
||
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | public function getMetaKeywords() |
||
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | public function setMetaKeywords($metaKeywords) |
||
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | public function getMetaDescription() |
||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | public function setMetaDescription($metaDescription) |
||
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | public function isAvailable() |
||
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | public function getAvailableOn() |
||
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | public function setAvailableOn(\DateTime $availableOn = null) |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | public function getAvailableUntil() |
||
228 | |||
229 | /** |
||
230 | * {@inheritdoc} |
||
231 | */ |
||
232 | public function setAvailableUntil(\DateTime $availableUntil = null) |
||
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | */ |
||
240 | public function getAttributes() |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | public function getAttributesByLocale($localeCode, $fallbackLocaleCode) |
||
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | public function addAttribute(AttributeValueInterface $attribute) |
||
280 | |||
281 | /** |
||
282 | * {@inheritdoc} |
||
283 | */ |
||
284 | public function removeAttribute(AttributeValueInterface $attribute) |
||
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | public function hasAttribute(AttributeValueInterface $attribute) |
||
305 | |||
306 | /** |
||
307 | * {@inheritdoc} |
||
308 | */ |
||
309 | public function hasAttributeByCodeAndLocale($attributeCode, $localeCode = null) |
||
324 | |||
325 | /** |
||
326 | * {@inheritdoc} |
||
327 | */ |
||
328 | public function getAttributeByCodeAndLocale($attributeCode, $localeCode = null) |
||
343 | |||
344 | /** |
||
345 | * {@inheritdoc} |
||
346 | */ |
||
347 | public function hasVariants() |
||
351 | |||
352 | /** |
||
353 | * {@inheritdoc} |
||
354 | */ |
||
355 | public function getVariants() |
||
359 | |||
360 | /** |
||
361 | * {@inheritdoc} |
||
362 | */ |
||
363 | public function getAvailableVariants() |
||
369 | |||
370 | /** |
||
371 | * {@inheritdoc} |
||
372 | */ |
||
373 | public function addVariant(ProductVariantInterface $variant) |
||
380 | |||
381 | /** |
||
382 | * {@inheritdoc} |
||
383 | */ |
||
384 | public function removeVariant(ProductVariantInterface $variant) |
||
391 | |||
392 | /** |
||
393 | * {@inheritdoc} |
||
394 | */ |
||
395 | public function hasVariant(ProductVariantInterface $variant) |
||
399 | |||
400 | /** |
||
401 | * {@inheritdoc} |
||
402 | */ |
||
403 | public function hasOptions() |
||
407 | |||
408 | /** |
||
409 | * {@inheritdoc} |
||
410 | */ |
||
411 | public function getOptions() |
||
415 | |||
416 | /** |
||
417 | * {@inheritdoc} |
||
418 | */ |
||
419 | public function addOption(ProductOptionInterface $option) |
||
425 | |||
426 | /** |
||
427 | * {@inheritdoc} |
||
428 | */ |
||
429 | public function removeOption(ProductOptionInterface $option) |
||
435 | |||
436 | /** |
||
437 | * {@inheritdoc} |
||
438 | */ |
||
439 | public function hasOption(ProductOptionInterface $option) |
||
443 | |||
444 | /** |
||
445 | * {@inheritdoc} |
||
446 | */ |
||
447 | public function getAssociations() |
||
451 | |||
452 | /** |
||
453 | * {@inheritdoc} |
||
454 | */ |
||
455 | public function addAssociation(ProductAssociationInterface $association) |
||
462 | |||
463 | /** |
||
464 | * {@inheritdoc} |
||
465 | */ |
||
466 | public function removeAssociation(ProductAssociationInterface $association) |
||
473 | |||
474 | /** |
||
475 | * {@inheritdoc} |
||
476 | */ |
||
477 | public function hasAssociation(ProductAssociationInterface $association) |
||
481 | |||
482 | /** |
||
483 | * {@inheritdoc} |
||
484 | */ |
||
485 | public function isSimple() |
||
489 | |||
490 | /** |
||
491 | * {@inheritdoc} |
||
492 | */ |
||
493 | public function isConfigurable() |
||
497 | |||
498 | /** |
||
499 | * {@inheritdoc} |
||
500 | */ |
||
501 | protected function createTranslation() |
||
505 | |||
506 | /** |
||
507 | * {@inheritdoc} |
||
508 | */ |
||
509 | private function getAttributeInDifferentLocale(ProductAttributeValueInterface $attributeValue, $localeCode) |
||
523 | } |
||
524 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: