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 |
||
| 29 | class Product extends AbstractTranslatable implements ProductInterface |
||
| 30 | { |
||
| 31 | use SoftDeletableTrait, TimestampableTrait, ToggleableTrait; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var mixed |
||
| 35 | */ |
||
| 36 | protected $id; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var null|BaseArchetypeInterface |
||
| 40 | */ |
||
| 41 | protected $archetype; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \DateTime |
||
| 45 | */ |
||
| 46 | protected $availableOn; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var \DateTime |
||
| 50 | */ |
||
| 51 | protected $availableUntil; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var Collection|BaseAttributeValueInterface[] |
||
| 55 | */ |
||
| 56 | protected $attributes; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var Collection|BaseVariantInterface[] |
||
| 60 | */ |
||
| 61 | protected $variants; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var Collection|BaseOptionInterface[] |
||
| 65 | */ |
||
| 66 | protected $options; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var Collection|ProductAssociationInterface[] |
||
| 70 | */ |
||
| 71 | protected $associations; |
||
| 72 | |||
| 73 | public function __construct() |
||
| 74 | { |
||
| 75 | parent::__construct(); |
||
| 76 | |||
| 77 | $this->availableOn = new \DateTime(); |
||
| 78 | $this->attributes = new ArrayCollection(); |
||
| 79 | $this->associations = new ArrayCollection(); |
||
| 80 | $this->variants = new ArrayCollection(); |
||
| 81 | $this->options = new ArrayCollection(); |
||
| 82 | $this->createdAt = new \DateTime(); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public function __toString() |
||
| 89 | { |
||
| 90 | return $this->getName(); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * {@inheritdoc} |
||
| 95 | */ |
||
| 96 | public function getId() |
||
| 97 | { |
||
| 98 | return $this->id; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * {@inheritdoc} |
||
| 103 | */ |
||
| 104 | public function getArchetype() |
||
| 105 | { |
||
| 106 | return $this->archetype; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function setArchetype(BaseArchetypeInterface $archetype = null) |
||
| 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() |
||
| 201 | { |
||
| 202 | return (new DateRange($this->availableOn, $this->availableUntil))->isInRange(); |
||
| 203 | } |
||
| 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() |
||
| 225 | { |
||
| 226 | return $this->availableUntil; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * {@inheritdoc} |
||
| 231 | */ |
||
| 232 | public function setAvailableUntil(\DateTime $availableUntil = null) |
||
| 233 | { |
||
| 234 | $this->availableUntil = $availableUntil; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | */ |
||
| 240 | public function getAttributes() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * {@inheritdoc} |
||
| 247 | */ |
||
| 248 | public function setAttributes(Collection $attributes) |
||
| 249 | { |
||
| 250 | foreach ($attributes as $attribute) { |
||
| 251 | $this->addAttribute($attribute); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * {@inheritdoc} |
||
| 257 | */ |
||
| 258 | public function addAttribute(BaseAttributeValueInterface $attribute) |
||
| 259 | { |
||
| 260 | if (!$this->hasAttribute($attribute)) { |
||
| 261 | $attribute->setProduct($this); |
||
| 262 | $this->attributes->add($attribute); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritdoc} |
||
| 268 | */ |
||
| 269 | public function removeAttribute(BaseAttributeValueInterface $attribute) |
||
| 270 | { |
||
| 271 | if ($this->hasAttribute($attribute)) { |
||
| 272 | $this->attributes->removeElement($attribute); |
||
| 273 | $attribute->setProduct(null); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritdoc} |
||
| 279 | */ |
||
| 280 | public function hasAttribute(BaseAttributeValueInterface $attribute) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | public function hasAttributeByCode($attributeCode) |
||
| 289 | { |
||
| 290 | foreach ($this->attributes as $attribute) { |
||
| 291 | if ($attribute->getAttribute()->getCode() === $attributeCode) { |
||
| 292 | return true; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | return false; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * {@inheritdoc} |
||
| 301 | */ |
||
| 302 | public function getAttributeByCode($attributeCode) |
||
| 303 | { |
||
| 304 | foreach ($this->attributes as $attribute) { |
||
| 305 | if ($attributeCode === $attribute->getAttribute()->getCode()) { |
||
| 306 | return $attribute; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | return null; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * {@inheritdoc} |
||
| 315 | */ |
||
| 316 | public function getMasterVariant() |
||
| 317 | { |
||
| 318 | foreach ($this->variants as $variant) { |
||
| 319 | if ($variant->isMaster()) { |
||
| 320 | return $variant; |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | return null; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * {@inheritdoc} |
||
| 329 | */ |
||
| 330 | public function setMasterVariant(BaseVariantInterface $masterVariant) |
||
| 331 | { |
||
| 332 | $masterVariant->setMaster(true); |
||
| 333 | |||
| 334 | if (!$this->variants->contains($masterVariant)) { |
||
| 335 | $masterVariant->setProduct($this); |
||
| 336 | $this->variants->add($masterVariant); |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * {@inheritdoc} |
||
| 342 | */ |
||
| 343 | public function hasVariants() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * {@inheritdoc} |
||
| 350 | */ |
||
| 351 | public function getVariants() |
||
| 352 | { |
||
| 353 | return $this->variants->filter(function (BaseVariantInterface $variant) { |
||
| 354 | return !$variant->isDeleted() && !$variant->isMaster(); |
||
| 355 | }); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * {@inheritdoc} |
||
| 360 | */ |
||
| 361 | public function getAvailableVariants() |
||
| 362 | { |
||
| 363 | return $this->variants->filter(function (BaseVariantInterface $variant) { |
||
| 364 | return !$variant->isDeleted() && !$variant->isMaster() && $variant->isAvailable(); |
||
| 365 | }); |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * {@inheritdoc} |
||
| 370 | */ |
||
| 371 | public function setVariants(Collection $variants) |
||
| 372 | { |
||
| 373 | $this->variants->clear(); |
||
| 374 | |||
| 375 | foreach ($variants as $variant) { |
||
| 376 | $this->addVariant($variant); |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * {@inheritdoc} |
||
| 382 | */ |
||
| 383 | public function addVariant(BaseVariantInterface $variant) |
||
| 384 | { |
||
| 385 | if (!$this->hasVariant($variant)) { |
||
| 386 | $variant->setProduct($this); |
||
| 387 | $this->variants->add($variant); |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * {@inheritdoc} |
||
| 393 | */ |
||
| 394 | public function removeVariant(BaseVariantInterface $variant) |
||
| 395 | { |
||
| 396 | if ($this->hasVariant($variant)) { |
||
| 397 | $variant->setProduct(null); |
||
| 398 | $this->variants->removeElement($variant); |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * {@inheritdoc} |
||
| 404 | */ |
||
| 405 | public function hasVariant(BaseVariantInterface $variant) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * {@inheritdoc} |
||
| 412 | */ |
||
| 413 | public function hasOptions() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * {@inheritdoc} |
||
| 420 | */ |
||
| 421 | public function getOptions() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * {@inheritdoc} |
||
| 428 | */ |
||
| 429 | public function setOptions(Collection $options) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * {@inheritdoc} |
||
| 436 | */ |
||
| 437 | public function addOption(BaseOptionInterface $option) |
||
| 438 | { |
||
| 439 | if (!$this->hasOption($option)) { |
||
| 440 | $this->options->add($option); |
||
| 441 | } |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * {@inheritdoc} |
||
| 446 | */ |
||
| 447 | public function removeOption(BaseOptionInterface $option) |
||
| 448 | { |
||
| 449 | if ($this->hasOption($option)) { |
||
| 450 | $this->options->removeElement($option); |
||
| 451 | } |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * {@inheritdoc} |
||
| 456 | */ |
||
| 457 | public function hasOption(BaseOptionInterface $option) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * {@inheritdoc} |
||
| 464 | */ |
||
| 465 | public function setDeletedAt(\DateTime $deletedAt = null) |
||
| 466 | { |
||
| 467 | if (null === $deletedAt) { |
||
| 468 | foreach ($this->variants as $variant) { |
||
| 469 | $variant->setDeletedAt(null); |
||
| 470 | } |
||
| 471 | } |
||
| 472 | |||
| 473 | $this->deletedAt = $deletedAt; |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * {@inheritdoc} |
||
| 478 | */ |
||
| 479 | public function getAssociations() |
||
| 480 | { |
||
| 481 | return $this->associations; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * {@inheritdoc} |
||
| 486 | */ |
||
| 487 | public function addAssociation(ProductAssociationInterface $association) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * {@inheritdoc} |
||
| 497 | */ |
||
| 498 | public function removeAssociation(ProductAssociationInterface $association) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * {@inheritdoc} |
||
| 508 | */ |
||
| 509 | public function hasAssociation(ProductAssociationInterface $association) |
||
| 513 | } |
||
| 514 |