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 |
||
25 | class Product implements ProductInterface |
||
26 | { |
||
27 | use TimestampableTrait, ToggleableTrait; |
||
28 | use TranslatableTrait { |
||
29 | __construct as private initializeTranslationsCollection; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @var mixed |
||
34 | */ |
||
35 | protected $id; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $code; |
||
41 | |||
42 | /** |
||
43 | * @var \DateTime |
||
44 | */ |
||
45 | protected $availableOn; |
||
46 | |||
47 | /** |
||
48 | * @var \DateTime |
||
49 | */ |
||
50 | protected $availableUntil; |
||
51 | |||
52 | /** |
||
53 | * @var Collection|AttributeValueInterface[] |
||
54 | */ |
||
55 | protected $attributes; |
||
56 | |||
57 | /** |
||
58 | * @var Collection|ProductVariantInterface[] |
||
59 | */ |
||
60 | protected $variants; |
||
61 | |||
62 | /** |
||
63 | * @var Collection|ProductOptionInterface[] |
||
64 | */ |
||
65 | protected $options; |
||
66 | |||
67 | /** |
||
68 | * @var Collection|ProductAssociationInterface[] |
||
69 | */ |
||
70 | protected $associations; |
||
71 | |||
72 | public function __construct() |
||
83 | |||
84 | /** |
||
85 | * @return string |
||
86 | */ |
||
87 | public function __toString() |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function getId() |
||
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | public function getCode() |
||
107 | |||
108 | /** |
||
109 | * @param string $code |
||
110 | */ |
||
111 | public function setCode($code) |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function getName() |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | public function setName($name) |
||
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | public function getSlug() |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function setSlug($slug = null) |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | public function getDescription() |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | public function setDescription($description) |
||
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | public function getMetaKeywords() |
||
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | public function setMetaKeywords($metaKeywords) |
||
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | public function getMetaDescription() |
||
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | public function setMetaDescription($metaDescription) |
||
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | public function isAvailable() |
||
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | public function getAvailableOn() |
||
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | public function setAvailableOn(\DateTimeInterface $availableOn = null) |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | public function getAvailableUntil() |
||
228 | |||
229 | /** |
||
230 | * {@inheritdoc} |
||
231 | */ |
||
232 | public function setAvailableUntil(\DateTimeInterface $availableUntil = null) |
||
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | */ |
||
240 | public function getAttributes() |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | public function addAttribute(AttributeValueInterface $attribute) |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | public function removeAttribute(AttributeValueInterface $attribute) |
||
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | */ |
||
270 | public function hasAttribute(AttributeValueInterface $attribute) |
||
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | public function hasAttributeByCode($attributeCode) |
||
288 | |||
289 | /** |
||
290 | * {@inheritdoc} |
||
291 | */ |
||
292 | public function getAttributeByCode($attributeCode) |
||
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | */ |
||
306 | public function hasVariants() |
||
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | public function getVariants() |
||
318 | |||
319 | /** |
||
320 | * {@inheritdoc} |
||
321 | */ |
||
322 | public function getAvailableVariants() |
||
328 | |||
329 | /** |
||
330 | * {@inheritdoc} |
||
331 | */ |
||
332 | public function addVariant(ProductVariantInterface $variant) |
||
339 | |||
340 | /** |
||
341 | * {@inheritdoc} |
||
342 | */ |
||
343 | public function removeVariant(ProductVariantInterface $variant) |
||
350 | |||
351 | /** |
||
352 | * {@inheritdoc} |
||
353 | */ |
||
354 | public function hasVariant(ProductVariantInterface $variant) |
||
358 | |||
359 | /** |
||
360 | * {@inheritdoc} |
||
361 | */ |
||
362 | public function hasOptions() |
||
366 | |||
367 | /** |
||
368 | * {@inheritdoc} |
||
369 | */ |
||
370 | public function getOptions() |
||
374 | |||
375 | /** |
||
376 | * {@inheritdoc} |
||
377 | */ |
||
378 | public function addOption(ProductOptionInterface $option) |
||
384 | |||
385 | /** |
||
386 | * {@inheritdoc} |
||
387 | */ |
||
388 | public function removeOption(ProductOptionInterface $option) |
||
394 | |||
395 | /** |
||
396 | * {@inheritdoc} |
||
397 | */ |
||
398 | public function hasOption(ProductOptionInterface $option) |
||
402 | |||
403 | /** |
||
404 | * {@inheritdoc} |
||
405 | */ |
||
406 | public function getAssociations() |
||
410 | |||
411 | /** |
||
412 | * {@inheritdoc} |
||
413 | */ |
||
414 | public function addAssociation(ProductAssociationInterface $association) |
||
421 | |||
422 | /** |
||
423 | * {@inheritdoc} |
||
424 | */ |
||
425 | public function removeAssociation(ProductAssociationInterface $association) |
||
432 | |||
433 | /** |
||
434 | * {@inheritdoc} |
||
435 | */ |
||
436 | public function hasAssociation(ProductAssociationInterface $association) |
||
440 | |||
441 | /** |
||
442 | * {@inheritdoc} |
||
443 | */ |
||
444 | public function isSimple() |
||
448 | |||
449 | /** |
||
450 | * {@inheritdoc} |
||
451 | */ |
||
452 | public function isConfigurable() |
||
456 | } |
||
457 |
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.