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 Collection|AttributeValueInterface[] |
||
| 45 | */ |
||
| 46 | protected $attributes; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var Collection|ProductVariantInterface[] |
||
| 50 | */ |
||
| 51 | protected $variants; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var Collection|ProductOptionInterface[] |
||
| 55 | */ |
||
| 56 | protected $options; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var Collection|ProductAssociationInterface[] |
||
| 60 | */ |
||
| 61 | protected $associations; |
||
| 62 | |||
| 63 | public function __construct() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | public function __toString() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | public function getId() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | public function getCode() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $code |
||
| 100 | */ |
||
| 101 | public function setCode($code) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function getName() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | public function setName($name) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | public function getSlug() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | public function setSlug($slug = null) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritdoc} |
||
| 140 | */ |
||
| 141 | public function getDescription() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * {@inheritdoc} |
||
| 148 | */ |
||
| 149 | public function setDescription($description) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | */ |
||
| 157 | public function getMetaKeywords() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritdoc} |
||
| 164 | */ |
||
| 165 | public function setMetaKeywords($metaKeywords) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritdoc} |
||
| 172 | */ |
||
| 173 | public function getMetaDescription() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | public function setMetaDescription($metaDescription) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritdoc} |
||
| 188 | */ |
||
| 189 | public function getAttributes() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | public function getAttributesByLocale($localeCode, $fallbackLocaleCode) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritdoc} |
||
| 215 | */ |
||
| 216 | public function addAttribute(AttributeValueInterface $attribute) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | */ |
||
| 233 | public function removeAttribute(AttributeValueInterface $attribute) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * {@inheritdoc} |
||
| 249 | */ |
||
| 250 | public function hasAttribute(AttributeValueInterface $attribute) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * {@inheritdoc} |
||
| 257 | */ |
||
| 258 | public function hasAttributeByCodeAndLocale($attributeCode, $localeCode = null) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function getAttributeByCodeAndLocale($attributeCode, $localeCode = null) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * {@inheritdoc} |
||
| 293 | */ |
||
| 294 | public function hasVariants() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * {@inheritdoc} |
||
| 301 | */ |
||
| 302 | public function getVariants() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * {@inheritdoc} |
||
| 309 | */ |
||
| 310 | public function addVariant(ProductVariantInterface $variant) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * {@inheritdoc} |
||
| 320 | */ |
||
| 321 | public function removeVariant(ProductVariantInterface $variant) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * {@inheritdoc} |
||
| 331 | */ |
||
| 332 | public function hasVariant(ProductVariantInterface $variant) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * {@inheritdoc} |
||
| 339 | */ |
||
| 340 | public function hasOptions() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * {@inheritdoc} |
||
| 347 | */ |
||
| 348 | public function getOptions() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * {@inheritdoc} |
||
| 355 | */ |
||
| 356 | public function addOption(ProductOptionInterface $option) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * {@inheritdoc} |
||
| 365 | */ |
||
| 366 | public function removeOption(ProductOptionInterface $option) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * {@inheritdoc} |
||
| 375 | */ |
||
| 376 | public function hasOption(ProductOptionInterface $option) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * {@inheritdoc} |
||
| 383 | */ |
||
| 384 | public function getAssociations() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * {@inheritdoc} |
||
| 391 | */ |
||
| 392 | public function addAssociation(ProductAssociationInterface $association) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * {@inheritdoc} |
||
| 402 | */ |
||
| 403 | public function removeAssociation(ProductAssociationInterface $association) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * {@inheritdoc} |
||
| 413 | */ |
||
| 414 | public function hasAssociation(ProductAssociationInterface $association) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * {@inheritdoc} |
||
| 421 | */ |
||
| 422 | public function isSimple() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * {@inheritdoc} |
||
| 429 | */ |
||
| 430 | public function isConfigurable() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * {@inheritdoc} |
||
| 437 | */ |
||
| 438 | protected function createTranslation() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * {@inheritdoc} |
||
| 445 | */ |
||
| 446 | private function getAttributeInDifferentLocale(ProductAttributeValueInterface $attributeValue, $localeCode) |
||
| 460 | } |
||
| 461 |
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: