Complex classes like PriceRule 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 PriceRule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class PriceRule extends Model |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $allocationMethod; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $createdAt; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $customerSelection; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $endsAt; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $entitledCollectionIds; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $entitledCountryIds; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $entitledProductIds; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $entitledVariantIds; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $oncePerCustomer; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $prerequisiteCustomerIds; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $prerequisiteQuantityRange; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $prerequisiteSavedSearchIds; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $prerequisiteShippingPriceRange; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $prerequisiteSubtotalRange; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $startsAt; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | protected $targetSelection; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | protected $targetType; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | protected $title; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var string |
||
| 110 | */ |
||
| 111 | protected $usageLimit; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | protected $prerequisiteProductIds; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | protected $prerequisiteVariantIds; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var array |
||
| 125 | */ |
||
| 126 | protected $prerequisiteCollectionIds; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var int |
||
| 130 | */ |
||
| 131 | protected $value; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string |
||
| 135 | */ |
||
| 136 | protected $valueType; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | protected $prerequisiteToEntitlementQuantityRatio; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | public function getAllocationMethod() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param string $allocationMethod |
||
| 153 | * |
||
| 154 | * @return PriceRule |
||
| 155 | */ |
||
| 156 | public function setAllocationMethod($allocationMethod) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getCreatedAt() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $createdAt |
||
| 173 | * |
||
| 174 | * @return PriceRule |
||
| 175 | */ |
||
| 176 | public function setCreatedAt($createdAt) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getCustomerSelection() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $customerSelection |
||
| 193 | * |
||
| 194 | * @return PriceRule |
||
| 195 | */ |
||
| 196 | public function setCustomerSelection($customerSelection) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getEndsAt() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $endsAt |
||
| 213 | * |
||
| 214 | * @return PriceRule |
||
| 215 | */ |
||
| 216 | public function setEndsAt($endsAt) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function getEntitledCollectionIds() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param array $entitledCollectionIds |
||
| 233 | * |
||
| 234 | * @return PriceRule |
||
| 235 | */ |
||
| 236 | public function setEntitledCollectionIds($entitledCollectionIds) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | public function getEntitledCountryIds() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param array $entitledCountryIds |
||
| 253 | * |
||
| 254 | * @return PriceRule |
||
| 255 | */ |
||
| 256 | public function setEntitledCountryIds($entitledCountryIds) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | public function getEntitledProductIds() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param array $entitledProductIds |
||
| 273 | * |
||
| 274 | * @return PriceRule |
||
| 275 | */ |
||
| 276 | public function setEntitledProductIds($entitledProductIds) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function getEntitledVariantIds() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param array $entitledVariantIds |
||
| 293 | * |
||
| 294 | * @return PriceRule |
||
| 295 | */ |
||
| 296 | public function setEntitledVariantIds($entitledVariantIds) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return boolean |
||
| 305 | */ |
||
| 306 | public function getOncePerCustomer() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param boolean $oncePerCustomer |
||
| 313 | * |
||
| 314 | * @return PriceRule |
||
| 315 | */ |
||
| 316 | public function setOncePerCustomer($oncePerCustomer) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | public function getPrerequisiteCustomerIds() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param array $prerequisiteCustomerIds |
||
| 333 | * |
||
| 334 | * @return PriceRule |
||
| 335 | */ |
||
| 336 | public function setPrerequisiteCustomerIds($prerequisiteCustomerIds) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | public function getPrerequisiteQuantityRange() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param array $prerequisiteQuantityRange |
||
| 353 | * |
||
| 354 | * @return PriceRule |
||
| 355 | */ |
||
| 356 | public function setPrerequisiteQuantityRange($prerequisiteQuantityRange) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | public function getPrerequisiteSavedSearchIds() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param array $prerequisiteSavedSearchIds |
||
| 373 | * |
||
| 374 | * @return PriceRule |
||
| 375 | */ |
||
| 376 | public function setPrerequisiteSavedSearchIds($prerequisiteSavedSearchIds) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return array |
||
| 385 | */ |
||
| 386 | public function getPrerequisiteShippingPriceRange() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param array $prerequisiteShippingPriceRange |
||
| 393 | * |
||
| 394 | * @return PriceRule |
||
| 395 | */ |
||
| 396 | public function setPrerequisiteShippingPriceRange($prerequisiteShippingPriceRange) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | public function getPrerequisiteSubtotalRange() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param array $prerequisiteSubtotalRange |
||
| 413 | * |
||
| 414 | * @return PriceRule |
||
| 415 | */ |
||
| 416 | public function setPrerequisiteSubtotalRange($prerequisiteSubtotalRange) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function getStartsAt() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param string $startsAt |
||
| 433 | * |
||
| 434 | * @return PriceRule |
||
| 435 | */ |
||
| 436 | public function setStartsAt($startsAt) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | public function getTargetSelection() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param string $targetSelection |
||
| 453 | * |
||
| 454 | * @return PriceRule |
||
| 455 | */ |
||
| 456 | public function setTargetSelection($targetSelection) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @return string |
||
| 465 | */ |
||
| 466 | public function getTargetType() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @param string $targetType |
||
| 473 | * |
||
| 474 | * @return PriceRule |
||
| 475 | */ |
||
| 476 | public function setTargetType($targetType) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | public function getTitle() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @param string $title |
||
| 493 | * |
||
| 494 | * @return PriceRule |
||
| 495 | */ |
||
| 496 | public function setTitle($title) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | public function getUsageLimit() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @param string $usageLimit |
||
| 513 | * |
||
| 514 | * @return PriceRule |
||
| 515 | */ |
||
| 516 | public function setUsageLimit($usageLimit) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | public function getPrerequisiteProductIds() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @param array $prerequisiteProductIds |
||
| 533 | * |
||
| 534 | * @return PriceRule |
||
| 535 | */ |
||
| 536 | public function setPrerequisiteProductIds($prerequisiteProductIds) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @return array |
||
| 545 | */ |
||
| 546 | public function getPrerequisiteVariantIds() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @param array $prerequisiteVariantIds |
||
| 553 | * |
||
| 554 | * @return PriceRule |
||
| 555 | */ |
||
| 556 | public function setPrerequisiteVariantIds($prerequisiteVariantIds) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @return array |
||
| 565 | */ |
||
| 566 | public function getPrerequisiteCollectionIds() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @param array $prerequisiteCollectionIds |
||
| 573 | * |
||
| 574 | * @return PriceRule |
||
| 575 | */ |
||
| 576 | public function setPrerequisiteCollectionIds($prerequisiteCollectionIds) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @return int |
||
| 585 | */ |
||
| 586 | public function getValue() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @param int $value |
||
| 593 | * |
||
| 594 | * @return PriceRule |
||
| 595 | */ |
||
| 596 | public function setValue($value) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @return string |
||
| 605 | */ |
||
| 606 | public function getValueType() |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @param string $valueType |
||
| 613 | * |
||
| 614 | * @return PriceRule |
||
| 615 | */ |
||
| 616 | public function setValueType($valueType) |
||
| 622 | |||
| 623 | /** |
||
| 624 | * @return array |
||
| 625 | */ |
||
| 626 | public function getPrerequisiteToEntitlementQuantityRatio() |
||
| 630 | |||
| 631 | /** |
||
| 632 | * @param array $prerequisiteToEntitlementQuantityRatio |
||
| 633 | * |
||
| 634 | * @return PriceRule |
||
| 635 | */ |
||
| 636 | public function setPrerequisiteToEntitlementQuantityRatio($prerequisiteToEntitlementQuantityRatio) |
||
| 642 | } |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.