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 |
||
| 17 | class PriceRule extends Model |
||
| 18 | { |
||
| 19 | use AdminGraphqlApiId; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $allocationMethod; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var \DateTimeInterface |
||
| 28 | */ |
||
| 29 | protected $createdAt; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $customerSelection; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \DateTimeInterface |
||
| 38 | */ |
||
| 39 | protected $endsAt; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $entitledCollectionIds; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $entitledCountryIds; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $entitledProductIds; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $entitledVariantIds; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $oncePerCustomer; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $prerequisiteCustomerIds; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $prerequisiteQuantityRange; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $prerequisiteSavedSearchIds; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $prerequisiteShippingPriceRange; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected $prerequisiteSubtotalRange; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | protected $startsAt; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | protected $targetSelection; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | protected $targetType; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var string |
||
| 108 | */ |
||
| 109 | protected $title; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | protected $usageLimit; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var array |
||
| 118 | */ |
||
| 119 | protected $prerequisiteProductIds; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | protected $prerequisiteVariantIds; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | protected $prerequisiteCollectionIds; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var int |
||
| 133 | */ |
||
| 134 | protected $value; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var string |
||
| 138 | */ |
||
| 139 | protected $valueType; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var array |
||
| 143 | */ |
||
| 144 | protected $prerequisiteToEntitlementQuantityRatio; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | public function getAllocationMethod() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param string $allocationMethod |
||
| 156 | * |
||
| 157 | * @return PriceRule |
||
| 158 | */ |
||
| 159 | public function setAllocationMethod($allocationMethod) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return \DateTimeInterface |
||
| 168 | */ |
||
| 169 | public function getCreatedAt() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param \DateTimeInterface $createdAt |
||
| 176 | * |
||
| 177 | * @return PriceRule |
||
| 178 | */ |
||
| 179 | public function setCreatedAt($createdAt) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function getCustomerSelection() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $customerSelection |
||
| 196 | * |
||
| 197 | * @return PriceRule |
||
| 198 | */ |
||
| 199 | public function setCustomerSelection($customerSelection) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return \DateTimeInterface |
||
| 208 | */ |
||
| 209 | public function getEndsAt() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param \DateTimeInterface $endsAt |
||
| 216 | * |
||
| 217 | * @return PriceRule |
||
| 218 | */ |
||
| 219 | public function setEndsAt($endsAt) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | public function getEntitledCollectionIds() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param array $entitledCollectionIds |
||
| 236 | * |
||
| 237 | * @return PriceRule |
||
| 238 | */ |
||
| 239 | public function setEntitledCollectionIds($entitledCollectionIds) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | public function getEntitledCountryIds() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param array $entitledCountryIds |
||
| 256 | * |
||
| 257 | * @return PriceRule |
||
| 258 | */ |
||
| 259 | public function setEntitledCountryIds($entitledCountryIds) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | public function getEntitledProductIds() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param array $entitledProductIds |
||
| 276 | * |
||
| 277 | * @return PriceRule |
||
| 278 | */ |
||
| 279 | public function setEntitledProductIds($entitledProductIds) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public function getEntitledVariantIds() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param array $entitledVariantIds |
||
| 296 | * |
||
| 297 | * @return PriceRule |
||
| 298 | */ |
||
| 299 | public function setEntitledVariantIds($entitledVariantIds) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function getOncePerCustomer() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param boolean $oncePerCustomer |
||
| 316 | * |
||
| 317 | * @return PriceRule |
||
| 318 | */ |
||
| 319 | public function setOncePerCustomer($oncePerCustomer) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | public function getPrerequisiteCustomerIds() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param array $prerequisiteCustomerIds |
||
| 336 | * |
||
| 337 | * @return PriceRule |
||
| 338 | */ |
||
| 339 | public function setPrerequisiteCustomerIds($prerequisiteCustomerIds) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | public function getPrerequisiteQuantityRange() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param array $prerequisiteQuantityRange |
||
| 356 | * |
||
| 357 | * @return PriceRule |
||
| 358 | */ |
||
| 359 | public function setPrerequisiteQuantityRange($prerequisiteQuantityRange) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @return array |
||
| 368 | */ |
||
| 369 | public function getPrerequisiteSavedSearchIds() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param array $prerequisiteSavedSearchIds |
||
| 376 | * |
||
| 377 | * @return PriceRule |
||
| 378 | */ |
||
| 379 | public function setPrerequisiteSavedSearchIds($prerequisiteSavedSearchIds) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | public function getPrerequisiteShippingPriceRange() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param array $prerequisiteShippingPriceRange |
||
| 396 | * |
||
| 397 | * @return PriceRule |
||
| 398 | */ |
||
| 399 | public function setPrerequisiteShippingPriceRange($prerequisiteShippingPriceRange) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | public function getPrerequisiteSubtotalRange() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @param array $prerequisiteSubtotalRange |
||
| 416 | * |
||
| 417 | * @return PriceRule |
||
| 418 | */ |
||
| 419 | public function setPrerequisiteSubtotalRange($prerequisiteSubtotalRange) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function getStartsAt() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $startsAt |
||
| 436 | * |
||
| 437 | * @return PriceRule |
||
| 438 | */ |
||
| 439 | public function setStartsAt($startsAt) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | public function getTargetSelection() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $targetSelection |
||
| 456 | * |
||
| 457 | * @return PriceRule |
||
| 458 | */ |
||
| 459 | public function setTargetSelection($targetSelection) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public function getTargetType() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param string $targetType |
||
| 476 | * |
||
| 477 | * @return PriceRule |
||
| 478 | */ |
||
| 479 | public function setTargetType($targetType) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | public function getTitle() |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param string $title |
||
| 496 | * |
||
| 497 | * @return PriceRule |
||
| 498 | */ |
||
| 499 | public function setTitle($title) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | public function getUsageLimit() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param string $usageLimit |
||
| 516 | * |
||
| 517 | * @return PriceRule |
||
| 518 | */ |
||
| 519 | public function setUsageLimit($usageLimit) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | public function getPrerequisiteProductIds() |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @param array $prerequisiteProductIds |
||
| 536 | * |
||
| 537 | * @return PriceRule |
||
| 538 | */ |
||
| 539 | public function setPrerequisiteProductIds($prerequisiteProductIds) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @return array |
||
| 548 | */ |
||
| 549 | public function getPrerequisiteVariantIds() |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param array $prerequisiteVariantIds |
||
| 556 | * |
||
| 557 | * @return PriceRule |
||
| 558 | */ |
||
| 559 | public function setPrerequisiteVariantIds($prerequisiteVariantIds) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @return array |
||
| 568 | */ |
||
| 569 | public function getPrerequisiteCollectionIds() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param array $prerequisiteCollectionIds |
||
| 576 | * |
||
| 577 | * @return PriceRule |
||
| 578 | */ |
||
| 579 | public function setPrerequisiteCollectionIds($prerequisiteCollectionIds) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * @return int |
||
| 588 | */ |
||
| 589 | public function getValue() |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @param int $value |
||
| 596 | * |
||
| 597 | * @return PriceRule |
||
| 598 | */ |
||
| 599 | public function setValue($value) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * @return string |
||
| 608 | */ |
||
| 609 | public function getValueType() |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @param string $valueType |
||
| 616 | * |
||
| 617 | * @return PriceRule |
||
| 618 | */ |
||
| 619 | public function setValueType($valueType) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @return array |
||
| 628 | */ |
||
| 629 | public function getPrerequisiteToEntitlementQuantityRatio() |
||
| 633 | |||
| 634 | /** |
||
| 635 | * @param array $prerequisiteToEntitlementQuantityRatio |
||
| 636 | * |
||
| 637 | * @return PriceRule |
||
| 638 | */ |
||
| 639 | public function setPrerequisiteToEntitlementQuantityRatio($prerequisiteToEntitlementQuantityRatio) |
||
| 645 | } |
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.