Complex classes like LineItem 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 LineItem, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class LineItem extends Model |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | protected $fulfillableQuantity; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $fulfillmentService; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $fulfillmentStatus; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | protected $grams; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var float |
||
| 40 | */ |
||
| 41 | protected $price; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | protected $productId; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | protected $quantity; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $requiresShipping; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $sku; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $title; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | protected $variantId; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $variantTitle; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $vendor; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $name; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | protected $giftCard; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $properties; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var bool |
||
| 100 | */ |
||
| 101 | protected $taxable; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var TaxLine[] |
||
| 105 | */ |
||
| 106 | protected $taxLines; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var float |
||
| 110 | */ |
||
| 111 | protected $totalDiscount; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var DiscountAllocation[] |
||
| 115 | */ |
||
| 116 | protected $discountAllocations; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | protected $appliedDiscount; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var bool |
||
| 125 | */ |
||
| 126 | protected $custom; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | public function isCustom() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param bool $custom |
||
| 138 | * |
||
| 139 | * @return LineItem |
||
| 140 | */ |
||
| 141 | public function setCustom($custom) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public function getAppliedDiscount() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param array $appliedDiscount |
||
| 158 | */ |
||
| 159 | public function setAppliedDiscount($appliedDiscount) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return int |
||
| 166 | */ |
||
| 167 | public function getFulfillableQuantity() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param int $fulfillableQuantity |
||
| 174 | * |
||
| 175 | * @return LineItem |
||
| 176 | */ |
||
| 177 | public function setFulfillableQuantity($fulfillableQuantity) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getFulfillmentService() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $fulfillmentService |
||
| 194 | * |
||
| 195 | * @return LineItem |
||
| 196 | */ |
||
| 197 | public function setFulfillmentService($fulfillmentService) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function getFulfillmentStatus() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $fulfillmentStatus |
||
| 214 | * |
||
| 215 | * @return LineItem |
||
| 216 | */ |
||
| 217 | public function setFulfillmentStatus($fulfillmentStatus) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return int |
||
| 226 | */ |
||
| 227 | public function getGrams() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param int $grams |
||
| 234 | * |
||
| 235 | * @return LineItem |
||
| 236 | */ |
||
| 237 | public function setGrams($grams) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return double |
||
| 246 | */ |
||
| 247 | public function getPrice() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $price |
||
| 254 | * |
||
| 255 | * @return LineItem |
||
| 256 | */ |
||
| 257 | public function setPrice($price) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return int |
||
| 266 | */ |
||
| 267 | public function getProductId() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param int $productId |
||
| 274 | * |
||
| 275 | * @return LineItem |
||
| 276 | */ |
||
| 277 | public function setProductId($productId) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return int |
||
| 286 | */ |
||
| 287 | public function getQuantity() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param int $quantity |
||
| 294 | * |
||
| 295 | * @return LineItem |
||
| 296 | */ |
||
| 297 | public function setQuantity($quantity) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | public function isRequiresShipping() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param bool $requiresShipping |
||
| 314 | * |
||
| 315 | * @return LineItem |
||
| 316 | */ |
||
| 317 | public function setRequiresShipping($requiresShipping) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function getSku() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $sku |
||
| 334 | * |
||
| 335 | * @return LineItem |
||
| 336 | */ |
||
| 337 | public function setSku($sku) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function getTitle() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $title |
||
| 354 | * |
||
| 355 | * @return LineItem |
||
| 356 | */ |
||
| 357 | public function setTitle($title) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return int |
||
| 366 | */ |
||
| 367 | public function getVariantId() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param int $variantId |
||
| 374 | * |
||
| 375 | * @return LineItem |
||
| 376 | */ |
||
| 377 | public function setVariantId($variantId) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | public function getVariantTitle() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param string $variantTitle |
||
| 394 | * |
||
| 395 | * @return LineItem |
||
| 396 | */ |
||
| 397 | public function setVariantTitle($variantTitle) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | public function getVendor() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param string $vendor |
||
| 414 | * |
||
| 415 | * @return LineItem |
||
| 416 | */ |
||
| 417 | public function setVendor($vendor) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | public function getName() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param string $name |
||
| 434 | * |
||
| 435 | * @return LineItem |
||
| 436 | */ |
||
| 437 | public function setName($name) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return bool |
||
| 446 | */ |
||
| 447 | public function isGiftCard() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param bool $giftCard |
||
| 454 | * |
||
| 455 | * @return LineItem |
||
| 456 | */ |
||
| 457 | public function setGiftCard($giftCard) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return array |
||
| 466 | */ |
||
| 467 | public function getProperties() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param array $properties |
||
| 474 | * |
||
| 475 | * @return LineItem |
||
| 476 | */ |
||
| 477 | public function setProperties($properties) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @return bool |
||
| 486 | */ |
||
| 487 | public function isTaxable() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param bool $taxable |
||
| 494 | * |
||
| 495 | * @return LineItem |
||
| 496 | */ |
||
| 497 | public function setTaxable($taxable) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @return TaxLine[] |
||
| 506 | */ |
||
| 507 | public function getTaxLines() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param TaxLine[] $taxLines |
||
| 514 | * |
||
| 515 | * @return LineItem |
||
| 516 | */ |
||
| 517 | public function setTaxLines($taxLines) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @return float |
||
| 526 | */ |
||
| 527 | public function getTotalDiscount() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @param float $totalDiscount |
||
| 534 | * |
||
| 535 | * @return LineItem |
||
| 536 | */ |
||
| 537 | public function setTotalDiscount($totalDiscount) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @return DiscountAllocation[] |
||
| 546 | */ |
||
| 547 | public function getDiscountAllocations() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @param DiscountAllocation[] $discountAllocations |
||
| 554 | * |
||
| 555 | * @return LineItem |
||
| 556 | */ |
||
| 557 | public function setDiscountAllocations($discountAllocations) |
||
| 563 | } |
||
| 564 |
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.