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 | * @return int |
||
| 120 | */ |
||
| 121 | public function getFulfillableQuantity() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param int $fulfillableQuantity |
||
| 128 | * |
||
| 129 | * @return LineItem |
||
| 130 | */ |
||
| 131 | public function setFulfillableQuantity($fulfillableQuantity) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public function getFulfillmentService() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $fulfillmentService |
||
| 148 | * |
||
| 149 | * @return LineItem |
||
| 150 | */ |
||
| 151 | public function setFulfillmentService($fulfillmentService) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getFulfillmentStatus() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $fulfillmentStatus |
||
| 168 | * |
||
| 169 | * @return LineItem |
||
| 170 | */ |
||
| 171 | public function setFulfillmentStatus($fulfillmentStatus) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return int |
||
| 180 | */ |
||
| 181 | public function getGrams() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param int $grams |
||
| 188 | * |
||
| 189 | * @return LineItem |
||
| 190 | */ |
||
| 191 | public function setGrams($grams) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function getPrice() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $price |
||
| 208 | * |
||
| 209 | * @return LineItem |
||
| 210 | */ |
||
| 211 | public function setPrice($price) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return int |
||
| 220 | */ |
||
| 221 | public function getProductId() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param int $productId |
||
| 228 | * |
||
| 229 | * @return LineItem |
||
| 230 | */ |
||
| 231 | public function setProductId($productId) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return int |
||
| 240 | */ |
||
| 241 | public function getQuantity() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param int $quantity |
||
| 248 | * |
||
| 249 | * @return LineItem |
||
| 250 | */ |
||
| 251 | public function setQuantity($quantity) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return bool |
||
| 260 | */ |
||
| 261 | public function isRequiresShipping() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param bool $requiresShipping |
||
| 268 | * |
||
| 269 | * @return LineItem |
||
| 270 | */ |
||
| 271 | public function setRequiresShipping($requiresShipping) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public function getSku() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $sku |
||
| 288 | * |
||
| 289 | * @return LineItem |
||
| 290 | */ |
||
| 291 | public function setSku($sku) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | public function isTitle() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param bool $title |
||
| 308 | * |
||
| 309 | * @return LineItem |
||
| 310 | */ |
||
| 311 | public function setTitle($title) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return int |
||
| 320 | */ |
||
| 321 | public function getVariantId() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param int $variantId |
||
| 328 | * |
||
| 329 | * @return LineItem |
||
| 330 | */ |
||
| 331 | public function setVariantId($variantId) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public function getVariantTitle() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param string $variantTitle |
||
| 348 | * |
||
| 349 | * @return LineItem |
||
| 350 | */ |
||
| 351 | public function setVariantTitle($variantTitle) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | public function getVendor() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $vendor |
||
| 368 | * |
||
| 369 | * @return LineItem |
||
| 370 | */ |
||
| 371 | public function setVendor($vendor) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | public function getName() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param string $name |
||
| 388 | * |
||
| 389 | * @return LineItem |
||
| 390 | */ |
||
| 391 | public function setName($name) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @return bool |
||
| 400 | */ |
||
| 401 | public function isGiftCard() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param bool $giftCard |
||
| 408 | * |
||
| 409 | * @return LineItem |
||
| 410 | */ |
||
| 411 | public function setGiftCard($giftCard) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | public function getProperties() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param array $properties |
||
| 428 | * |
||
| 429 | * @return LineItem |
||
| 430 | */ |
||
| 431 | public function setProperties($properties) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @return bool |
||
| 440 | */ |
||
| 441 | public function isTaxable() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @param bool $taxable |
||
| 448 | * |
||
| 449 | * @return LineItem |
||
| 450 | */ |
||
| 451 | public function setTaxable($taxable) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @return TaxLine[] |
||
| 460 | */ |
||
| 461 | public function getTaxLines() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param TaxLine[] $taxLines |
||
| 468 | * |
||
| 469 | * @return LineItem |
||
| 470 | */ |
||
| 471 | public function setTaxLines($taxLines) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @return float |
||
| 480 | */ |
||
| 481 | public function getTotalDiscount() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param float $totalDiscount |
||
| 488 | * |
||
| 489 | * @return LineItem |
||
| 490 | */ |
||
| 491 | public function setTotalDiscount($totalDiscount) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @return DiscountAllocation[] |
||
| 500 | */ |
||
| 501 | public function getDiscountAllocations() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param DiscountAllocation[] $discountAllocations |
||
| 508 | * |
||
| 509 | * @return LineItem |
||
| 510 | */ |
||
| 511 | public function setDiscountAllocations($discountAllocations) |
||
| 517 | } |
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.